4
typedef struct {
    int count;
    int *items;
}set;

set* set_alloc(set *src, int num);
int set_insert(set *s, int num);

int main() {
    set *A = NULL;
    A = set_alloc(A, 0);
    A = set_alloc(A, 1); //this and line below is part of inserting function
    A->items[0] = 2;
    system("pause");
}   

set* set_alloc(set *src, int num) {
    if (src == NULL && num == 0) {
            set *src = (set*)malloc(sizeof(set));
        src->count = 0;
        src->items = NULL;
    }
    else {
        src->count = num;
        src->items = (int*)realloc(src->items, num*sizeof(int));
    }
    return src;
}

Code above is able to allocate memory for the array of items inside the set and for the set itself, however, it fails to realloc that array of items.. I could set it a constant size, but I don't really wanna go around this problem because I've had it in previous projects.

Poody
  • 325
  • 3
  • 13
  • 2
    It is not clear how `realloc` 'fails' here. Please describe precisely what is happening. – SirDarius Apr 16 '14 at 09:13
  • 2
    [Don't cast the result of `malloc` (and family) in C](http://stackoverflow.com/a/605858/440558). – Some programmer dude Apr 16 '14 at 09:14
  • Also, be careful to not assign the result of `realloc` back to the pointer you're reallocating. What if `realloc` fails and returns `NULL`? Then you loose the original pointer and will have a memory leak. – Some programmer dude Apr 16 '14 at 09:16
  • 1
    @Poody: Are you not malloc-ing a local variable 'set *src', instead of the actual input argument 'src'?? – raj raj Apr 16 '14 at 09:19
  • @raj raj: I see it now, and in the answer below. I was switching the code back and forth. Solves it, but I'm still confused – Poody Apr 16 '14 at 09:25

2 Answers2

3

Here:

set *src = (set*)malloc(sizeof(set));

you are redeclaring src (in a block scope), you want:

src = malloc(sizeof(set));

I could set it a constant size, but I don't really wanna go around this problem because I've had it in previous projects.

An alternative to realloc when you don't know the size beforehand is a linked list.

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
2

Your function never returns the newly allocated "*src" from function set_alloc, see my comments below, please use the same *src for allocation, and your code should work.

    set* set_alloc(set *src, int num) {
    if (src == NULL && num == 0) {
        set *src = (set*)malloc(sizeof(set));  ***//<--- This pointer is local to if block.***
 *//Please Correct code as =>*            src = (set*)malloc(sizeof(set));

        src->count = 0;
        src->items = NULL;
    }
    else {
        src->count = num;
        src->items = (int*)realloc(src->items, num*sizeof(int));
    }
    return src;   ***// <-- This is returning the in parameter not the malloced pointer ***
}
  • Yes yes. The function originally created a new set, allocated memory and returned it, but I, incorrectly, upgraded it so it can realloc as well. Thanks – Poody Apr 16 '14 at 09:30