Jump to content
  • Advertisement

Archived

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

wah_on_2

how to use sizeof(..)?

This topic is 6025 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

Advertisement
sizeof return the size of the pointer, which is 4. To return 10, you need to keep track of how big the array is in your own variable, or look into the STL, and use std::vector
(then you would do cout<

Share this post


Link to post
Share on other sites
Thank you, i know how to use vector on MSDN now.

However, beside the vector, do i have another way to know the size of the **num?

why it can work?

int num[10]={0};
sizeof(num); //it display 40

Share this post


Link to post
Share on other sites
None that you'd want to use, short of writing your own memory allocation routines.

Edit: I amend that. You can allocate one extra pointer, set it to NULL, then walk and count trough your int* array until you reach NULL. strlen() works similarly.

Edited by - Fruny on February 16, 2002 2:28:35 AM

Share this post


Link to post
Share on other sites
For dynamic pointers, sizeof returns 4, always. For static pointers, it returns the total size of the array.

short Static [10];
sizeof(Static) == 20

short* Dynamic = new short [10];
sizeof(Dynamic) == 4


~CGameProgrammer( );

Share this post


Link to post
Share on other sites
quote:
Original post by CGameProgrammer
For dynamic pointers, sizeof returns 4, always. For static pointers, it returns the total size of the array.

short Static [10];
sizeof(Static) == 20

short* Dynamic = new short [10];
sizeof(Dynamic) == 4



And

short* Foo = Static;
sizeof(Foo) == 4

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!