Jump to content
  • Advertisement
Sign in to follow this  
V-man

qwerty

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

int array[100]; memset(array, 1, sizeof(sint)*100); instead of filling up array with 1, it fills up with 0x01010101 Is that normal and what would be a better function to use? edit : sorry about the title. I guess I forgot to change it. [Edited by - V-man on July 7, 2007 8:48:53 PM]

Share this post


Link to post
Share on other sites
Advertisement
That's how it works. It filled the array with the character sized variable 1 (0x0F or 00001111b).

The reason 0 works to fill it with all 0s is because it is made of all 0x itself (0x00 or 00000000b).

If you want to fill it up with all 1s, then use the number made of all 1s (11111111b = 0xFF = 255)

memset(array,255,sizeof(int)*100);

Share this post


Link to post
Share on other sites
Quote:
Original post by V-man
int array[100];
memset(array, 1, sizeof(sint)*100);

instead of filling up array with 1, it fills up with 0x01010101
Is that normal and what would be a better function to use?


Yes, this is normal -- memset only deals with the concept of bytes.

If this is C++, you should prefer it's standard library options when available over the legacy inherited from C (which memset is a part of). In this case, C++ has std::fill available:

int array[100];
std::fill( array, array+100, 1 );


std::fill is a template (and thus C++ only) function, which allows it to detect that array is a bunch of ints, and fill on a per-int basis instead of a per-byte basis. Note that you don't even need sizeof() anymore. It will also work with iterators if you're not using a raw array like you are in this example.

Share this post


Link to post
Share on other sites
About std:fill, thanks, I didn't know about it until now.
I prefer to use "raw arrays" instead of std classes. It is a question of personal taste and memory alignment, although I use C++.
I was wondering if there is a good old fashion alternative.

Share this post


Link to post
Share on other sites
Quote:
Original post by V-man
About std:fill, thanks, I didn't know about it until now.
I prefer to use "raw arrays" instead of std classes. It is a question of personal taste, although I use C++.


So in other words you don't use C++.

Do you have some reason for this decision?

Share this post


Link to post
Share on other sites
int array[100];
memset(array, 1, sizeof(sint)*100);

0x01010101 0x01010101 0x01010101 ...


--------------------------------

int array[100];
memset(array, 1, sizeof(int)*100);

0x00000001 0x00000001 0x00000001


-me

Share this post


Link to post
Share on other sites
Quote:
Original post by V-man
Memory alignment I guess is the technical reason.


Meaning what?

Are you trying to ensure the memory allocated is contiguous? Because std::vector does that automatically.

If you're trying to ensure that your memory is 4 8 or 16 byte aligned for some reason you can probably do that too though I'll defer to the more expert members of the forum for the best way's to do so.

Share this post


Link to post
Share on other sites
"If you're trying to ensure that your memory is 4 8 or 16 byte aligned for some reason you can probably do that too though I'll defer to the more expert members of the forum for the best way's to do so."

yes

Also, it's difficult to watch during debugging. I used VC++6 and now .NET 2003
If I use the std::vector class and if I were to put my variable in the watch window like this

mything[0]

it gives an error message :
error : object "mything" doesn't have an indexer

I've never figure this out. Anyone know what's the matter with MS-VC?

Share this post


Link to post
Share on other sites
Quote:
Original post by V-man
Memory alignment I guess is the technical reason.

Using the default allocator, C++ containers will give you the same alignment guarantees as C-style arrays.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!