# sizeof an array problem

This is a discussion on sizeof an array problem within the C Programming forums, part of the General Programming Boards category; Hi All I have an array like this Code: cl_device_id *devices ; devices = (cl_device_id*)malloc(sizeof(cl_device_id) * 2) Now if I ...

1. ## sizeof an array problem

Hi All

I have an array like this

Code:
```cl_device_id *devices ;
devices = (cl_device_id*)malloc(sizeof(cl_device_id) * 2)```
Now if I want to determine its length like

Code:
`int len = sizeof( devices ) / sizeof( cl_device_id ) ;`
and I get 1 (it should be 2). So I do something wrong, any suggestions ?

Cheers
LuCa

2. If you use sizeof on a pointer, it will return the size of the pointer, not the size of whatever the pointer points to. You're going to just have to keep track of how many devices you allocated memory for.

3. but why does this work:

Code:
```int array[6]= { 1, 2, 3, 4, 5, 6 };
int len=sizeof(array)/sizeof(int);```
array is a pointer too!

4. Arrays decay to pointer, so....

How to find the last value in an array

int len = *((*(&array+1))-1);

5. Originally Posted by jeanluca
but why does this work:

Code:
```int array[6]= { 1, 2, 3, 4, 5, 6 };
int len=sizeof(array)/sizeof(int);```
array is a pointer too!
No, array is not a pointer! It's an array.

Check this out

6. Originally Posted by jeanluca
but why does this work:

Code:
```int array[6]= { 1, 2, 3, 4, 5, 6 };
int len=sizeof(array)/sizeof(int);```
array is a pointer too!
An array is not a pointer.

The wrinkle is that, in some contexts, the name of an array can be used as if it is a pointer.

If you do this;
Code:
```#include <stdio.h>
int main()
{
int array[] ={ 1, 2, 3, 4, 5, 6 };
int *pointer = array;                    /* in this context array is a pointer */

fprintf(stdout, "sizeof(array)/sizeof(int) %u\n", sizeof(array)/sizeof(int));   /* will print 6 */

fprintf(stdout, "sizeof(pointer)/sizeof(int) %u\n", sizeof(pointer)/sizeof(int));   /* will print something else */

}```
The size of a pointer (which is simply a variable that contains a memory address) is compiler-dependent. The size of a pointer doesn't change if the pointer happens to contain the address of the first element of an array.

7. Anywhere a pointer to T can be used, an array of T can be used. The result is a pointer to the first element of the array. This doesn't mean that an array is a pointer.

The original question is, given a pointer to some malloc'd array, how do you tell how long it is? The answer is you can't. Unless you stash the information away yourself, it's lost.

8. So in a few words the
Code:
`length = sizeof(array) / sizeof(type);`
doesn't work when you dynammically allocate arrays. For the reasons already said.

In C you just have to be a bit more creative. With proper naming you can solve this kind of things. Just add the line:
Code:
```cl_device_id *devices ;
devices = (cl_device_id*)malloc(sizeof(cl_device_id) * 2)
int devices_len = 2;```
and you the _len moto whenever you want the length of a variable.

If you want an alternative solution, you can combin the devices and devices_len into a struct. Then you simply make your malloc for the struct like:
Code:
```cl_device_o* createDev(int length)
{
dev* = malloc(lenght);
dev->len = length;
return dev;
}```
where cl_device_o your new struct. With a few "replace-all"s you could modify your existing code and be able to find the length of your dynamically allocated pointerish arrays.

Of course the first solution is easier and more safe.

9. ok, thats all clear!. Thanks a lot for your answers!!

cheers
LuCa

Popular pages Recent additions