|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
getting the size of a dynamic array
okay, i am having problems getting the size of a dynamic array... with a static array i could just do this: sizeof(Array); but when you are working with a dynamic array, it just returns the size of the pointer. Is there a function for this, or should i just write a class that has the array size and index stored in it?
|
|
#2
|
|||
|
|||
|
There's no (ansi) way to get the (memory) size of an array pointer. The common way is to create a struct around it with e.g. a size_t size field.
|
|
#3
|
|||
|
|||
|
why not store the size in a variable, and change its value whenever you resize the array?
Y'know, like: new_size = 500; ... array = malloc(new_size*sizeof(array_type)); size = new_size; |
|
#4
|
|||
|
|||
|
that's what i did at first, but i thought there would be a better way... i guess i will just write a class that automatically sets the size variable everytime the array is resized. thanks anyway guys.
|
|
#5
|
||||
|
||||
|
Quote:
If you created an array you must already have known its size! The problem that dynamic allocation returns a pointer. A pointer is not the same as an array. An array, as you know retains its size information, a pointer knows only the size of itself, not the data it points to. An array, like a reference, cannot be moved to reference different data, whereas a pointer may be reassigned. When an array is passed as a function parameter, it is converted to a pointer, so within that function, you loose the array behaviour such as size information. However, you are right that having two separate variables (a size and a pointer) is inconvenient and potentially error prone. However, you have exactly described the better way that you are seeking - write your own class to encapsulate the functionality you require. If you make it a template class, you can create arrays of any desired type, have functions to obtain the size in bytes and the also the size in terms of number of elements. By overloading the [] operator, you can make it behave syntactically just line an array. Clifford |
|
#6
|
|||
|
|||
|
that's what i did. As for me already knowing the size of the array... I am using several different functions for this little program. I am not passing the size of the array to each one. It is much easier than that to just write my own dynamic array class.
|
|
#7
|
||||
|
||||
|
Quote:
Unfortunately that is what you have to do, (even for arrays, because they are converted to pointers when passed into a function). Take a look at the standard C library for example, functions like memcpy() and memset() require a size parameter. Hiding all the detail in a class is as elegant as it gets - which is actually pretty elegant, and if well designed need only be done once. You might also consider using the STL classes, such as vector, which does exactly more or less what I described earlier. Clifford. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > getting the size of a dynamic array |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|