Thread: sizeof on arrays

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    8

    Question sizeof on arrays

    What should i do to make this code return the size of my dynamic allocated array. it just says that its 4 bytes, but I want all of the array
    Code:
     
      floatarray = new float[7];
      for (k=0;k<7;k++) floatarray[k]=(k+1);
      cout << "size of floatarray" << sizeof(floatarray) << endl;

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    // cout << "size of floatarray" << strlen(floatarray) << endl;

    Kuphryn

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    I don't think that's possible with dynamic arrays (only static).
    The sizeof(floatarray) returns the size of the pointer, not the array itself.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    As Monster says, you can only do this on an array (not an extern of an array, or an array generated by calling new)

    Do this
    Code:
    int floatarraysize = 7;
    floatarray = new float[floatarraysize];
    cout << "size of floatarray" << floatarraysize << endl;
    Your implementation may provide a NON-Standard way of doing this, but you'd have to read the manuals to find it (assuming it was even provided).

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Interesting.

    I have never tried using strlen(pointertoArray).

    How about strlen(array[]) or strlen(array[0])?

    Kuphryn

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I have never tried using strlen(pointertoArray).
    Since its only valid to try this on char arrays which have a \0 terminator, it isn't really applicable.

    In addition
    char arr[80] = "hello";

    sizeof(arr) is 80
    strlen(arr) is 5

    So it doesn't really help that much even when you have strings

  7. #7
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    if you use C memory allocation routines like:
    malloc calloc,realloc you could use
    special function called msize wich returns the size of the mem

    like:
    Code:
    #include <malloc.h>
        ....
       float array = (float*)malloc(10 * sizeof(float));
       ...
       /// do something 
       ...
       int memsz =0;
    #ifdef WIN32
       memsz = _msize(array); //Win32 specific name (ANSI name is msize)
    #else
       memsz = msize(array);
    #endif
    
      //free the mem
      free(array);
    you can try this with NEW and DELETE
    but i don't know the result (wether it will be succesfull or portable)

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    367
    The size of an array is the size of the element multiplied with the number of places in the Array.

  9. #9
    Registered User
    Join Date
    Jun 2002
    Posts
    8

    Thumbs up

    thx guys and gals. I had to put in a counter which i increased every time i increased the size of the dynamic array.

  10. #10
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Therefore, sizeof(array)/sizeof(array[0]) will give you the size of each individual element. Couldn't you derefrence the array if it's dynamic to find out how large it is?

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    thx guys and gals. I had to put in a counter which i increased every time i increased the size of the dynamic array.
    Once you do this, your dynamic array is incurring the same overhead as a Vector. Why not just use a Vector?
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie question about 'sizeof' and arrays
    By Sharke in forum C Programming
    Replies: 27
    Last Post: 12-09-2009, 06:30 AM
  2. Malloc,calloc..Sscanf.
    By ozumsafa in forum C Programming
    Replies: 22
    Last Post: 07-26-2007, 01:09 AM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. finding size of empty char array
    By darsunt in forum C Programming
    Replies: 12
    Last Post: 05-30-2006, 07:23 PM
  5. Need help on pointer to VOID
    By dv007 in forum C Programming
    Replies: 19
    Last Post: 07-09-2002, 06:15 PM