-1

I need to build an array of pointers to int, all by dynamic memory allocation. I start with declaring:

  int** queue = (int**)malloc(sizeof(int*));

and than (size =1)

queue[*size-1] = (int*)calloc(1,sizeof(int));

I scan an integer:

printf("Enter item value to add\n");
    scanf("%d",queue[*size-1]);
    printf("Item %d added\n",*(queue[*size-1]));

All these code is in the same function and works fine. but when I try to print something from this queue in another function or to free the memory by:

for(i = 0;i<size;i++)
    {
        free(queue[i]);
    }
    free(queue);

the program crashes. I would love for some help. Thanks in advance!

Henk Holterman
  • 250,905
  • 30
  • 306
  • 490
  • 2
    is `*size-1` supposed to be `(*size)-1` or `*(size-1)`? (See [operator precedence](http://en.cppreference.com/w/c/language/operator_precedence)). Also [don't cast the return of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – UnholySheep Dec 31 '16 at 20:26
  • 3
    Also what is `size` declared as? From your explanation it seems to be a single integer value, in which case I don't understand why it is accessed via a pointer – UnholySheep Dec 31 '16 at 20:29
  • its supposed to be (*size)-1 – Ori Netanel Ben-Zaken Dec 31 '16 at 20:31
  • 1
    @UnholySheep Maybe the code that uses `*size` is in a function that receives the size by pointer, but the `for` loop is in the caller. – Barmar Dec 31 '16 at 20:31
  • and size is declared in another function as : `int size = 1` – Ori Netanel Ben-Zaken Dec 31 '16 at 20:32
  • 2
    You need to provide a [MCVE](http://stackoverflow.com/help/mcve). There's not enough here to know what you're doing wrong. – Barmar Dec 31 '16 at 20:33
  • yup, just like Barmar said – Ori Netanel Ben-Zaken Dec 31 '16 at 20:33
  • Post the actual code, this doesn't show exactly where you allocate `queue[(*size)-1]`. There is nothing magic about allocation, you are basically free-ing something that isn't allocated. – Lou Dec 31 '16 at 20:33
  • Look at the code as a whole. You have 1 malloc(), but multiple loop-nested free()'s . That can't be right. – Henk Holterman Dec 31 '16 at 20:34
  • And I concur that *size looks weird and you don't provide its declaration. It can't be 'from another function'. Enough for a -1. Really do read that help page about [MCVE] – Henk Holterman Dec 31 '16 at 20:35
  • Why are you dereferencing `size` as in `*size`. It is not a pointer. You have stated that it is just an ordinary `int` variable. – VHS Dec 31 '16 at 20:41

1 Answers1

1
int** queue = (int**)malloc(sizeof(int*));

will only allocate sufficient memory to hold one element, so if size is 2 or higher you will address memeory in the queue that you have not allocated and hence you get undifenied behaviour (e.g. crash).

If you want your queue of allocated memory to be for example, 10 you need to allocate sufficient memory;

int** queue = (int**)malloc(sizeof(int*) * 10);

enter image description here

You need to make the red part sufficiently long to hold all your elements, and queue pointing to the beginning of that element.

If you don't want to make the queue a fixed length, you can use realloc, like

int** queue = (int**)realloc(queue, sizeof(int*) * ((*size)+1)); // resize red part
queue[(*size)-1] = (int*)calloc(1,sizeof(int)); // Creating the green part

And here I am assuming from the remainder of your code that size is a int *size

Soren
  • 14,094
  • 4
  • 38
  • 66