Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-06-23
10
Medium Priority
?
301 Views
Last Modified: 2010-04-21
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

?
0
Comment
Question by:sprockston
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 200 total points
ID: 21844230
>> 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
 
LVL 53

Expert Comment

by:Infinity08
ID: 21844239
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
 
LVL 85

Expert Comment

by:ozo
ID: 21844240
no
sizeof(data) == sizeof(int *)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 40

Expert Comment

by:evilrix
ID: 21844244
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
 
LVL 40

Expert Comment

by:evilrix
ID: 21844252
>> 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
 

Author Comment

by:sprockston
ID: 21844260
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 21844273
>>       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
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 200 total points
ID: 21844284
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 21844295
>> And the *4 is not needed

That's because the *4 is automatically added by the compiler for you ;)
0
 

Author Closing Comment

by:sprockston
ID: 31469652
Thank you very much! :D
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question