qwerty

Started by
20 comments, last by Malone 16 years, 9 months ago
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]
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
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);
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
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.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
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?
Memory alignment I guess is the technical reason.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
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
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?
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
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