Thread: size of allocated array

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    44

    size of allocated array

    When I try to check the size of an array that is allocated, it always tells me the size is 8, no matter how many bytes I allocate. When I check the size of an array that is declared the normal way, it gives me the correct size. I figure that it just checks the size of the pointer and not of the array? How can i fix this? Running on a macbook pro

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    int main()
    {
    	int *a,b[] = {1,2,3};
    	a = (int *)malloc(48);
    	a[0] = 1;
    	a[1] = 2;
    	a[2] = 3;
    	printf("size of b: %li\n", sizeof(b)); -> returns 12
    	printf("size of a: %li\n", sizeof(a)); -> returns 8
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Yep! that's the thing with dynamic storage; workaround would be to put a magic no. at the top so you don't need to calculate it everytime
    Code:
    #define SIZEOF_A 48

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Short answer: You can't.
    Long answer: The best thing to do is to keep track of the size. It's the only portable way.
    (Meaning there may be other ways, but they aren't portable.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by boxden
    I figure that it just checks the size of the pointer and not of the array?
    You figured correctly.

    Quote Originally Posted by boxden
    How can i fix this?
    You already know the size of the dynamic array in order to allocate space for it... trying to find out the size again is kind of like asking, "what's your name, Bob?"

    In this case, the value that you want is equal to 48 / sizeof(int). But you should not be writing:
    Code:
    a = (int *)malloc(48);
    instead, you should write, say:
    Code:
    size_t a_size = 12;
    a = malloc(a_size * sizeof(*a));
    Now you can write:
    Code:
    printf("size of a: %lu\n", a_size);
    EDIT:
    A drawback with itCbitC's suggestion is that you then might as well not use a dynamic array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    You can't get the size of an array directly using the sizeof() function in C. sizeof() returns the size of the data type you give it in bytes.

    There are macros you can define to get around this but they are not always useful. Best way to deal with this sort of thing is to define a variable to keep track to the length of arrays when you declare them.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by laserlight View Post
    instead, you should write, say:
    Code:
    size_t a_size = 12;
    a = malloc(a_size * sizeof(*a));
    Now you can write:
    Code:
    printf("size of a: %lu\n", a_size);
    Except it would return 96 instead of 48 on the o/p's machine.
    Quote Originally Posted by laserlight View Post
    A drawback with itCbitC's suggestion is that you then might as well not use a dynamic array.
    Yep! that's why it's a workaround

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by itCbitC
    Except it would return 96 instead of 48 on the o/p's machine.
    Maybe, maybe not. That is why I wrote "say".

    EDIT:
    That said, sizeof(int) == 8 seems rare at the moment. Does MacBook Pro really have 8-byte ints? What integer type corresponds to int32_t from C99's <stdint.h> for MacBook Pro?
    Last edited by laserlight; 06-28-2010 at 02:52 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by laserlight View Post
    Does MacBook Pro really have 8-byte ints?
    8-byte pointers not ints, to be precise.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    It's not so much whether a particular hardware supports 64-bit integers natively. As long as the compiler has generated code to do appropriate multiple-register math where needed, any architecture can support any size integer.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by itCbitC
    8-byte pointers not ints, to be precise.
    My guess is that you managed to misread sizeof(*a) as sizeof(int*).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by laserlight View Post
    My guess is that you managed to misread sizeof(*a) as sizeof(int*).
    Yep! got that backwards

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamically allocated size
    By maverickbu in forum C++ Programming
    Replies: 12
    Last Post: 06-26-2007, 01:16 PM
  2. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  3. Array size
    By tigrfire in forum C Programming
    Replies: 5
    Last Post: 11-14-2005, 08:45 PM
  4. templates, unresolved external error
    By silk.odyssey in forum C++ Programming
    Replies: 9
    Last Post: 06-09-2004, 04:39 PM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM