qwerty
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]
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);
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);
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.
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.
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.
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?
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
memset(array, 1, sizeof(sint)*100);
0x01010101 0x01010101 0x01010101 ...
--------------------------------
int array[100];
memset(array, 1, sizeof(int)*100);
0x00000001 0x00000001 0x00000001
-me
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.
"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?
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?
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement