• C

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

?
sprockstonAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Infinity08Connect With a Mentor Commented:
>> Shouldn't it be...

No, when you do sizeof(data), it will always return the size of the int pointer, no matter what that pointer is pointing to. The size of an int* on your system is apparently 4 bytes (32bit).
0
 
Infinity08Commented:
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

0
 
ozoCommented:
no
sizeof(data) == sizeof(int *)
0
KuppingerCole Reviews AlgoSec in Executive Report

Leading analyst firm, KuppingerCole reviews AlgoSec's Security Policy Management Solution, and the security challenges faced by companies today in their Executive View report.

 
evilrixSenior Software Engineer (Avast)Commented:
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
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> 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.
0
 
sprockstonAuthor Commented:
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.
0
 
Infinity08Commented:
>>       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.
0
 
Infinity08Connect With a Mentor Commented:
And the *4 is not needed, which I failed to leave out in my previous post, so :

      fscanf(fp, "%d", data+i);        // Get numentries identifier and size
      printf("%d\n", *(data+i));          // and print them out, just for debugging purposes.


or simpler :

      fscanf(fp, "%d", &data[i]);        // Get numentries identifier and size
      printf("%d\n", data[i]);          // and print them out, just for debugging purposes.

0
 
Infinity08Commented:
>> And the *4 is not needed

That's because the *4 is automatically added by the compiler for you ;)
0
 
sprockstonAuthor Commented:
Thank you very much! :D
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.