# Thread: size of allocated array

1. ## 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. 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`

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

4. Originally Posted by boxden
I figure that it just checks the size of the pointer and not of the array?
You figured correctly.

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);`
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.

5. 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. Originally Posted by laserlight
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.
Originally Posted by laserlight
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. 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?

8. Originally Posted by laserlight
Does MacBook Pro really have 8-byte ints?
8-byte pointers not ints, to be precise.

9. 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. 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*).

11. Originally Posted by laserlight
My guess is that you managed to misread sizeof(*a) as sizeof(int*).
Yep! got that backwards