Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2008-06-23
10
283 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files 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.

861 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