Hi,
Simple question...
If i declare a pointer and later on create an array that this pointer points to, there is no possible way for me to use sizeof the get the size of the entire array, right?
ex:
VERTEX *pVertices;
pVertices = new VERTEX[20];
Is there anyway to find out the size on the array that "pVertices" is pointing to or do I have to keep track of that in another array?
*tired*
JohanK
AFAIK, applying the sizeof operator to a pointer returns the size, in bytes, of the pointer itself. Your best bet would be to keep track of the array size yourself.
I might be wrong since I''m just learning C++.
I might be wrong since I''m just learning C++.
that wont work on a pointer, since a pointer on any 32 bit platform is 4 bytes, no matter how much data it represents
There is a function called _msize (I''m not sure how standardized it is) that given a pointer will return the size of the data-area it points to.
You probably don''t want to use it though, since it returns not only the size of the ''usable'' data area, but also any padding bytes and debug-markers, etc.
Your best bet is to keep track of the array''s size manually.
You probably don''t want to use it though, since it returns not only the size of the ''usable'' data area, but also any padding bytes and debug-markers, etc.
Your best bet is to keep track of the array''s size manually.
quote:Original post by fallenang3l
AFAIK, applying the sizeof operator to a pointer returns the size, in bytes, of the pointer itself. Your best bet would be to keep track of the array size yourself.
I might be wrong since I'm just learning C++.
You are right it does return the size of the pointer. This is one of those things that can really mess you up especially when writing data to files since you have to keep track of the length of the string pointed to.
There is a way, I think, to increment the pointer and access each character in the string pointed to. You can then do a loop and keep incrementing until you reach the null terminator and the number of the incrementation at the end is the length... but that is the long way.
I would recommend do a sizeof("string") and assigning the return value to a variable before assigning the string to the pointer... or as they have said before keep track of it yourself.
[edited by - ju2wheels on March 22, 2002 9:20:20 PM]
quote:Original post by barazor
that wont work on a pointer, since a pointer on any 32 bit platform is 4 bytes, no matter how much data it represents
of course it "works on a pointer." sizeof returns the size of whatever you use it on. So if you use it on a pointer it gives you the size of the pointer. If you don''t want the size of the pointer then ask for the sizeof the thing pointed to, not the pointer. It gives you back exactly what you asked for. Garbage In Garbage Out.
This is why we have std::vector. Unless you have constraints that prevent you from using it (like using C, learning purposes or "orders from above").
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
no, there isnt a standard way to get the length of an array.
--
it depends what kind of pointer your talking about.
compilers allow you to work with pointers in different ways.
some pointers are called fixed-length arrays -sizeof may be used here.
the difference is compiler specific.
you may be able to write your own pointer class. to indicate the end of the array, you could use NULL. then you write your own SizeOf method, which would scan your array for NULL.
[edited by - evilcrap on March 23, 2002 1:19:34 AM]
--
it depends what kind of pointer your talking about.
compilers allow you to work with pointers in different ways.
some pointers are called fixed-length arrays -sizeof may be used here.
char fixedArray[50];cout << sizeof(fixedArray); //50char pArray[];cout << sizeof(pArray); //4
the difference is compiler specific.
you may be able to write your own pointer class. to indicate the end of the array, you could use NULL. then you write your own SizeOf method, which would scan your array for NULL.
[edited by - evilcrap on March 23, 2002 1:19:34 AM]
VERTEX *myArray;int numberOfElements;...myArray = new VERTEX [numberOfElements];std::cout << "Size of array contents: " << sizeof (VERTEX) * numberOfElements << std::endl;
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement