Retrieving maximum array index in a dynamic array??
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:
Or (hopefully I don''t screw this one up):
[Resist Windows XP''s Invasive Production Activation Technology!]

|
Or (hopefully I don''t screw this one up):
|
[Resist Windows XP''s Invasive Production Activation Technology!]
=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:
|
Kippesoep
i searched MSDN and i found:
int i=_msize( postime )/sizeof(double);
where postime is: double *postime
int i=_msize( postime )/sizeof(double);
where postime is: double *postime
hey, life has to go on
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

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.
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!]
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


[Resist Windows XP''s Invasive Production Activation Technology!]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement