on memset

V

Vincent SHAO

int distance[MAX_X][MAX_Y];
memset(distance,-2,MAX_X*MAX_Y*sizeof(int));

Academically, the buffer "distance" should have been filled with -2
after these two line,
but, memset just fail to do the trick, they are still a garbage value
"-16843010"

why?

Thank you.
 
I

Ian Collins

Vincent said:
int distance[MAX_X][MAX_Y];
memset(distance,-2,MAX_X*MAX_Y*sizeof(int));

Academically, the buffer "distance" should have been filled with -2
after these two line,
but, memset just fail to do the trick, they are still a garbage value
"-16843010"
This isn't garbage, it's the correct result.

What units does memset work with?
 
V

Vincent SHAO

*VincentSHAO:
int distance[MAX_X][MAX_Y];
memset(distance,-2,MAX_X*MAX_Y*sizeof(int));
Academically, the buffer "distance" should have been filled with -2
after these two line,
No.

but, memset just fail to do the trick, they are still a garbage value
"-16843010"

It seems your compiler creates program that use 32 bit two's complement 'int'.

In that case -2 is represented as bit pattern 0xFFFFFFFE, and as signed char
value, 0xFE, and filling an 'int' with byte value 0xFE gives you 0xFEFEFEFE,
which represents -16843010.

See the FAQ for more safe ways to represent matrices (or go directly to Boost
library).

Cheers, & hth.,

- Alf

Thanks a lot.
And that is to say, i have to use double 'for' to init the buffer if i
want each elem in the buffer to be -2?
 
J

Juha Nieminen

Vincent said:
int distance[MAX_X][MAX_Y];
memset(distance,-2,MAX_X*MAX_Y*sizeof(int));
And that is to say, i have to use double 'for' to init the buffer if i
want each elem in the buffer to be -2?

Why is that a problem?

IMO it's a beginner's bad habit to try to make code as short as
possible (and achieving this by use of ugly tricks and hacks). Code
should be above all clear and well written. Shortness is completely
secondary (and usually irrelevant).

If you *really* want to use a "cool trick" to initialize that array
with one single loop, you can do this:

for(int i = 0; i < MAX_X*MAX_Y; ++i)
((int*)distance) = -2;

However, that's ugly code and not recommended.
 
B

Bo Persson

Juha said:
Vincent said:
int distance[MAX_X][MAX_Y];
memset(distance,-2,MAX_X*MAX_Y*sizeof(int));
And that is to say, i have to use double 'for' to init the buffer
if i want each elem in the buffer to be -2?

Why is that a problem?

IMO it's a beginner's bad habit to try to make code as short as
possible (and achieving this by use of ugly tricks and hacks). Code
should be above all clear and well written. Shortness is completely
secondary (and usually irrelevant).

If you *really* want to use a "cool trick" to initialize that array
with one single loop, you can do this:

for(int i = 0; i < MAX_X*MAX_Y; ++i)
((int*)distance) = -2;

However, that's ugly code and not recommended.


It is also interesting to see what the compiler does for the
"unoptimized" version with a double for loop:

; 8 : for (int i = 0; i != 1000; ++i)
; 9 : for (int j = 0; j != 1000; ++j)
; 10 : distance[j] = -2;

00001 mov eax, -2 ; fffffffeH
00006 mov ecx, 1000000 ; 000f4240H
0000b mov edi, OFFSET ?distance@@3PAY0DOI@HA ; distance
00010 rep stosd


This code is
- easier to read
- faster than memset
- correct


Bo Persson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top