Hi. Nice to meet you. I'd like to know if I'm doing the allocation of memory from data to new_data correctly and why new_data only has one value since the memory of data was copied before to new_data.

set size: 10
set[000] = 000
set[001] = 001
set[002] = 002
set[003] = 003
set[004] = 004
set[005] = 005
set[006] = 006
set[007] = 007
set[008] = 008
set[009] = 009
set size: 1
set[000] = 009
==3827== 
==3827== HEAP SUMMARY:
==3827==     in use at exit: 40 bytes in 1 blocks
==3827==   total heap usage: 22 allocs, 21 frees, 256 bytes allocated
==3827== 
==3827== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==3827==    at 0x4C2B3F8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3827==    by 0x400B4A: main (setapp.c:48)
==3827== 
==3827== LEAK SUMMARY:
==3827==    definitely lost: 40 bytes in 1 blocks
==3827==    indirectly lost: 0 bytes in 0 blocks
==3827==      possibly lost: 0 bytes in 0 blocks
==3827==    still reachable: 0 bytes in 0 blocks
==3827==         suppressed: 0 bytes in 0 blocks
==3827== 
==3827== For counts of detected and suppressed errors, rerun with: -v
==3827== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)



#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "set.h"

// compare two keys

static int match(const void* key1, const void* key2) {
    // attempt to return true
    return !memcmp(key1, key2, sizeof (int));
}

static void print_set(Set* set) {
    ListElmt* element;
    int* data;
    int i = 0;

    printf("set size: %d\n", set_size(set));
    for (element = list_head(set); element != NULL; element = list_next(element)) {
        data = list_data(element);
        printf("set[%03d] = %03d\n", i, *data);
        i++;
    }
}

int main(void) {
    Set set;
    int* data;
    int* new_data;
    int i;

    set_init(&set, match, free);
    for (i = 0; i < 10; i++) {
        if ((data = malloc(sizeof (int))) == NULL) {
            printf("not enough memory for alloc\n");
            return EXIT_FAILURE;
        }
        *data = i;
        if (set_insert(&set, data) != 0) {
            printf("unable to insert data into set\n");
            return EXIT_FAILURE;
        }
    }

    print_set(&set);

    if ((new_data = malloc(sizeof(int))) == NULL) {
        printf("not enough memory for alloc\n");
        return EXIT_FAILURE;
    }

    memcpy(new_data, data, sizeof(int));

    // destroy data 
    set_destroy(&set);

    // attempt to insert new_data 
    if (set_insert(&set, new_data) != 0) {
        printf("unable to insert data into the set\n");
        return EXIT_FAILURE;
    }

    // print the set again
    print_set(&set);

    set_destroy(&set);

    return EXIT_SUCCESS;
}

I'd like to know if I'm doing the allocation of memory from data to new_data correctly

Looks like it, at least if set is implemented the way I'd implement it using that interface.

why new_data only has one value since the memory of data was copied before to new_data.

new_data is a pointer to a single integer, why would there be more than one value? Your question suggests that my assumptions about how your set is implemented are wrong and you're potentially leaking memory.

okay, I think I fixed the malloc but I don't know how to fix the memcpy for that case.

if ((new_data = malloc(list_size(&set) * sizeof(int))) == NULL) {
        printf("not enough memory for alloc\n");
        return EXIT_FAILURE;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.