0

I don't know why I am getting warnings for the following code.

#include<stdio.h>
#include<malloc.h>
int main()
{
    int **p;
    int i,j;
    for(i=0;i<5;i++)
    {
        if(i==0)
            p=(int*)malloc(1*sizeof(int));
        else
            p=(int*)realloc(p,(i+1)*sizeof(int));
        p[i]=(int*)malloc(5*sizeof(int));

        for(j=0;j<5;j++)
        {
           p[i][j]=j;
        }
    }
    for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++)
            printf("%5d",p[i][j]);
        printf("\n");
    }

    return 0;
}  

Is there any other way to allocate memory dynamically for a double pointer.

krsteeve
  • 1,794
  • 4
  • 19
  • 29
  • 1
    Can you show the warning messages? – krsteeve Aug 30 '13 at 02:15
  • @Sashank, what are you trying to do with this code? – Eric Z Aug 30 '13 at 02:32
  • why are you using dynamic allocation of `p` to begin with? Why not `int *p[5];`? – Ben Voigt Aug 30 '13 at 03:46
  • @EricZ I am trying to implement a demo program by inserting values into 2d array which is created by allocating memory dynamically. – Sashank Alladi Aug 30 '13 at 17:01
  • @BenVoigt, I am trying to use double pointer, why double pointer because the fragment in which you specified int *p[5] you have to specify a value i.e, the number of rows , what if you don't know how many rows you are going to create. – Sashank Alladi Aug 30 '13 at 17:05
  • @SashankAlladi: Well, `5` was hard-coded in your program, so I thought you knew it in advance. If you don't know until after a few iterations, then I agree. – Ben Voigt Aug 30 '13 at 18:08
  • @BenVoigt I got you, The program is just a demo, well its my fault I have to explicitly specify it. – Sashank Alladi Aug 31 '13 at 12:34

3 Answers3

3

You get warnings because p is an int ** and you are allocating memory as if it was an int * in your if..else lines. Simply replace your lines as follows:

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

p=(int **)realloc(p,(i+1)*sizeof(int *));
trogdor
  • 1,626
  • 14
  • 17
0

This compiles with no warning

  int **p;
  p = (int **) malloc(sizeof(void *));
stark
  • 12,615
  • 3
  • 33
  • 50
  • What compiler will return a different size for a void* than an int**? – stark Aug 31 '13 at 02:25
  • Those examples are C++ – stark Aug 31 '13 at 03:03
  • So? C++ provides the same guarantees on primitive types as C. It's the platform, not the language, that determines pointer sizes. (You're welcome to pull a rule out of the C standard that says otherwise... but I don't think any exists) – Ben Voigt Aug 31 '13 at 03:21
  • Just checked the C standard. Section 6.2.5/26 is the relevant rule, and explicitly says that `void*` doesn't have to be the same size as any other pointer except `char*`. – Ben Voigt Aug 31 '13 at 03:25
  • malloc returns an untyped void * and you cast it to the pointer type that you want. How would that work with different pointer sizes? – stark Aug 31 '13 at 04:39
  • Try actually reading the explanations I linked to. Short version: `void*` needs to be able to contain any address, `int*` only needs to be able to contain aligned addresses, which means less information. – Ben Voigt Aug 31 '13 at 18:23
  • Since you've been given ample opportunity to correct your answer but persist in spreading wrong code, you've earned a -1 from me. – Ben Voigt Aug 31 '13 at 18:24
0

You're getting warnings because you're (a) casting the return value of malloc() and realloc(), and (b) casting it to the wrong type. In C it's generally considered best not to cast the return value of malloc() and realloc() at all, because void* will automatically convert to any other pointer type, and putting in the unnecessary cast can lead to errors (such as yours, for example).

This isn't my real name
  • 4,869
  • 3
  • 17
  • 30