Advertisement

Retrieving maximum array index in a dynamic array??

Started by January 04, 2002 07:18 PM
6 comments, last by Wavarian 23 years, 1 month ago
ok, i have a multidimentional dynamic array defined as: float** array; and then i proceed to set its size as: int vertexnum = 3; array = new float*[vertexnum]; for (int count = 0; count < vertexnum; count++) ... array[count] = new float[3]; // x,y & z co-ords now i was hoping that sizeof(array) would return vertexnum, but for the strangest reason it _always_ returns 4 no matter what the vertexnum variable is. So what im wondering: is there any other function i can use on the array to retrieve the maximum array index? Any ideas that could help me out here? Thanks again
Sizeof is resolved at compile time. The size of the pointer is 4 bytes. There''s no standard way to determine the size of a dynamically allocated array, so you must save it yourself. Secondly, what you''re doing is horrible . It''s much better to do one of these:
  float *array;int vertexnum = 3;array = new float[vertexnum*3];  

Or (hopefully I don''t screw this one up):
  float *(array[3]);int vertexnum = 3;array = new float[vertexnum][3];  


[Resist Windows XP''s Invasive Production Activation Technology!]
Advertisement
=P hehe, well thanks for the coding advice mate, i think i''ll have to work around this one yet again..
work around what? you have the varible. just keep track of it you lazy excuse for a programmer.
Just use an std::vector , so you can use the size () function:

  #include "vector"typedef std::vector <float> FLOATLIST;FLOATLIST array;array.resize (vertexnum * 3);cout << "Array is of size " << array.size () << "\n";  
Kippesoep
i searched MSDN and i found:

int i=_msize( postime )/sizeof(double);

where postime is: double *postime
hey, life has to go on
Advertisement
I think you'll find _msize only works when the block has been allocated by *alloc, not by new.



Edited by - Kippesoep on January 5, 2002 6:46:56 AM
Kippesoep
The _msize function kills your standard compliance and portability, don''t use it in any code that isn''t completely temporary. For example: It may be a good idea to use in a hackish memory leak detector, but it would be a bad idea to use in a string length function.
quote:
Original post by Kippesoep
I think you''ll find _msize only works when the block has been allocated by *alloc, not by new.

It will work with new, but it won''t work the way he expects. It may factor in things like the vTable pointer (if the class has one) and other such things that skew the results . So, basically, you''re correct .

[Resist Windows XP''s Invasive Production Activation Technology!]

This topic is closed to new replies.

Advertisement