Archived

This topic is now archived and is closed to further replies.

Sizeof

This topic is 5748 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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++.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.


    
char fixedArray[50];
cout << sizeof(fixedArray); //50

char 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]

Share this post


Link to post
Share on other sites

  

VERTEX *myArray;
int numberOfElements;
...
myArray = new VERTEX [numberOfElements];
std::cout << "Size of array contents: "
<< sizeof (VERTEX) * numberOfElements
<< std::endl;


Share this post


Link to post
Share on other sites
quote:
Original post by Oluseyi
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").



It is for learning purposes, I''ll be using the std::vector in my project.

Thanks all by the way, this forum is great, polite people and quick answers!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Why don''t some of you just try it?


  
void f(int* x)
{
std::cout << sizeof(x) << std::endl;
}

int main()
{
int x[50];
std::cout << sizeof(x) << std::endl;
f(x);

return 0;
}


If the array is declared locally, the size is known. If it isn''t, the size of the pointer is all you know.

Use vector though. No-one. Likes. C.

Share this post


Link to post
Share on other sites