Link to home
Start Free TrialLog in
Avatar of sprockston
sprockston

asked on

Malloc fails in C using a void int type? (Easy question apparently)

Why is the following printed when executing the below code:

      int *data;
                     int size = 100;
      printf("Data size: %d\n",sizeof(data));      
      if ( (data = (int *) malloc(sizeof(int) * size)) == 0) {
            printf("Memory request failed.\n");
            exit(1);
      }
      printf("Data size: %d\n",sizeof(data));

Output:
Data size: 4
Data size: 4

Shouldn't it be...
Data size: 4
Data size: 400

?
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you want to somehow get the size of the allocated memory, you will have to store it somewhere.
You already do (in the size value), so just change your code like this :
int *data;
int size = 100;
printf("Data size: %d\n",sizeof(data));      
if ( (data = (int *) malloc(sizeof(int) * size)) == 0) {
    printf("Memory request failed.\n");
    exit(1);
}
printf("Data size: %d\n", sizeof(data[0]) * size);

Open in new window

no
sizeof(data) == sizeof(int *)
Size of only returns the size of the pointer (4 bytes). This is calculated at compile time and not runtime. It would work if this was a static array, which is sized at compile time;

int data[400];
size_t nSize1 = sizeof(data); // This will give you the size of this array in terms of bytes
size_t nSize2 = sizeof(data)/sizeof(data[0])  // This will give you the size of this array in terms of elements
>> size of the pointer (4 bytes).
I should point out this is based upon your results, there is no size for a pointer set in the standards doc.
Avatar of sprockston
sprockston

ASKER

I understand that, the reason I asked was to solve the following goal. I have a file, fp, that has data such as in this format:
4
5
10
2

I would like to store it in the value:
      int *data;
                     FILE *fp;
      int size = 4; (constant, the size of the file does not need to be found)

Into data.
I already allocated the data pointer using:
( (data = (int *) malloc(sizeof(int) * size)) == 0)

How can I store the file contents, fp, into the data file? I tried:

                      int i = 0;
      while (i < size)
      {
      // Next step: read the data from the file into the data array.
      fscanf(fp, "%d", data+(i*4));        // Get numentries identifier and size
      printf("%d\n", *data+(i*4));          // and print them out, just for debugging purposes.
      i++;
      }

So I figured it was something involving an improper malloc.
>>       printf("%d\n", *data+(i*4));          // and print them out, just for debugging purposes.

Make that :

      printf("%d\n", *(data+(i*4)));          // and print them out, just for debugging purposes.

or why not simply :

      printf("%d\n", data[i]);          // and print them out, just for debugging purposes.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> And the *4 is not needed

That's because the *4 is automatically added by the compiler for you ;)
Thank you very much! :D