tags:

views:

129

answers:

6

In my program I am trying to resize array using malloc function.

#include <stdio.h>

int main(void)
{
    int list[5],i;
    int* ptr = &list;

    for(i = 0; i < 5; i++)
        list[i] = i;

    for(i = 0; i < 5; i++)
        printf("%d\n", list[i]);

    printf("----------------------------------------\n");

    ptr = malloc(10);

    for(i = 0; i < 10; i++)
        list[i] = i;

    for(i = 0; i < 10; i++)
        printf("%d\n", list[i]);
}

While compiling the program I get two warnings :

searock@searock-desktop:~/C$ cc malloc.c -o malloc
malloc.c: In function ‘main’:
malloc.c:6: warning: initialization from incompatible pointer type
malloc.c:16: warning: incompatible implicit declaration of built-in function ‘malloc’

My program is running fine. I can't understand why the compiler is giving me this errors?

Should I change my approach?

Edit 1 : And then how do I free the memory? should I use free(list); or free(ptr);

Edit 2 : Updated Code

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

int main(void)
{
    int list[5],i;
    int* ptr = malloc(5 * sizeof(int));   //&list;

    for(i = 0; i < 5; i++)
        ptr[i] = i;

    for(i = 0; i < 5; i++)
        printf("%d\n", ptr[i]);

    printf("----------------------------------------\n");

    ptr = realloc(ptr, 10 * sizeof(int));    //malloc(10);

    for(i = 0; i < 10; i++)
        ptr[i] = i;

    for(i = 0; i < 10; i++)
        printf("%d\n", ptr[i]);

        free(ptr);
}

Thanks.

+4  A: 

Yes, malloc allocates 10 bytes, but 10 integers are 10 * (sizeof integer) bytes.

Use malloc(10*sizeof(int));

Voulnet
DON'T use realloc here - you didn't get the original pointer from malloc!
bdonlan
Yes, you were totally right. Had he acquired the original pointer correctly, he should have used realloc, but he didn't really get the original pointer.
Voulnet
+3  A: 

The first warning is because you are trying to store a pointer to list, which is an array, in an int *. You need an int ** to hold a pointer to list.

The second warning is because you never included the header file which defines malloc. If you #include <stdlib.h> that warning will go away.

You've got several other problems here you need to address, it seems like maybe you need to read up a little more on C.

SoapBox
James McNellis
just do int *ptr = list; Though in the code here there's no need to initialize `ptr` to `list` since it isn't used.
nos
+5  A: 

You aren't actually reallocating list. list is still 5 bytes, while ptr points to the 10 byte array.

Do something like this instead:

int* list = malloc(5 * sizeof(int));
...
list = realloc(list, 10 * sizeof(int));
...

Now, when you say:

int* ptr = &list;

You aren't creating a "reference" to list; in this context (not in general), it's the same as:

int* ptr = list;

This means ptr[3] and list[3] are the same int, but making ptr point to a new buffer won't make list point to a new buffer as well. If this were C++, the syntax for declaring ptr the way you're thinking would be (I think):

int (&ptr)[5] = list;

In any case, you can't realloc an automatically-allocated buffer, anyway. This won't work:

int buffer[5];
buffer = realloc(buffer, 10 * sizeof(int));

The realloc would likely cause a segmentation fault, and assigning the new pointer to buffer isn't allowed by C.

Finally, stealing from the other answers, you need to #include <stdlib.h> to use malloc and friends; otherwise, it'll be implicitly declared to return an int rather than a pointer, yielding warnings and screwing up 64-bit compatibility for no good reason.

Joey Adams
+3  A: 

To explain the first warning:

int* ptr = &list;

ptr is of type int*, but &list is of type int (*)[5] (that is, a pointer to an array of five integers.

list and &list have the same pointer value, but they have different types. If you want to get a pointer to the initial element in the array, you should simply use the name of the array, list, which will decay to a pointer to its initial element in most circumstances (the exceptions being when & or sizeof are applied to the array).

As others have already explained, the second warning is because malloc is declared in <stdlib.h>, you need to allocate 10 * sizeof(int) instead of 10 bytes, and you need to be careful to use ptr when you want to use that allocated memory, and not list (since list still names the original array).

James McNellis
+1  A: 

The compiler gives you a warning on line 6 because you are assigning ptr to a pointer of an int array instead of a pointer to an int. Replacing line 6 with

int* ptr = list;

will do what you want.

gablin
+1  A: 

In my program I am trying to resize array using malloc function.

I dont think you can resize an array which is allocated on stack memory. When you say int list[5], you have allocated memory for 5 integers on the stack. Now whatever you do you can not make that 5 to 10. till your program dies that memory will still be there. You can not deallocate that memory.

Lets say you do something like

when you do ptr = malloc(), your list is still pointing to that stack memory where you have five continuous integers.

Lets say even if you do something like

list = malloc(10) [actually this should be list = (int *)malloc(10) * sizeof(int)] you have just changed the value of variable list, the original memory is still lying on your stack memory.

Manoj R