Solved

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

Posted on 2008-06-23
10
279 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 50 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 84

Expert Comment

by:ozo
ID: 21844240
no
sizeof(data) == sizeof(int *)
0
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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 50 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now