Thread: sizeof an array problem

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    72

    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. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    72
    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. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Arrays decay to pointer, so....

    How to find the last value in an array


    int len = *((*(&array+1))-1);
    Last edited by slingerland3g; 12-23-2009 at 04:32 PM.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by jeanluca View Post
    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
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by jeanluca View Post
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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. #9
    Registered User
    Join Date
    May 2009
    Posts
    72
    ok, thats all clear!. Thanks a lot for your answers!!

    cheers
    LuCa

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. solve this problem using array..
    By juncas17 in forum C Programming
    Replies: 4
    Last Post: 10-12-2009, 09:19 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Problem Putting INTs Into a CHAR Array
    By cram in forum C++ Programming
    Replies: 13
    Last Post: 10-13-2004, 07:53 AM
  4. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM
  5. From stream/file to a string array problem
    By dradsws in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 06:24 PM