int distance[MAX_X][MAX_Y];
memset(distance ,-2,MAX_X*MAX_Y*s izeof(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. 6 2500
Vincent SHAO wrote:
int distance[MAX_X][MAX_Y];
memset(distance ,-2,MAX_X*MAX_Y*s izeof(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?
--
Ian Collins.
* Vincent SHAO:
int distance[MAX_X][MAX_Y];
memset(distance ,-2,MAX_X*MAX_Y*s izeof(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"
why?
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
On 4ÔÂ16ÈÕ, ÏÂÎç2ʱ06·Ö, "Alf P. Steinbach" <al...@start.no wrote:
*VincentSHAO:
int distance[MAX_X][MAX_Y];
memset(distance ,-2,MAX_X*MAX_Y*s izeof(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"
why?
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?
* Vincent SHAO:
On 4?16?, ??2?06?, "Alf P. Steinbach" <al...@start.no wrote:
>*VincentSHAO :
>>int distance[MAX_X][MAX_Y]; memset(distan ce,-2,MAX_X*MAX_Y*s izeof(int)); Academicall y, 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" why?
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?
No.
The standard library has standard algorithms and even standard containers.
Perhaps the most straightforward (but see earlier advice) is
#include <vector>
int main()
{
using namespace std;
int const maxX = 2;
int const maxY = 3;
vector< vector<int distance( maxX, vector<int>( maxY, -2 ) );
// ...
distance[1][2] = 12345;
}
Cheers, & hth.,
- Alf
Vincent SHAO wrote:
>>int distance[MAX_X][MAX_Y]; memset(distan ce,-2,MAX_X*MAX_Y*s izeof(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 )[i] = -2;
However, that's ugly code and not recommended.
Juha Nieminen wrote:
Vincent SHAO wrote:
>>>int distance[MAX_X][MAX_Y]; memset(dista nce,-2,MAX_X*MAX_Y*s izeof(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 )[i] = -2;
However, that's ugly code and not recommended.
It is also interesting to see what the compiler does for the
"unoptimize d" version with a double for loop:
; 8 : for (int i = 0; i != 1000; ++i)
; 9 : for (int j = 0; j != 1000; ++j)
; 10 : distance[i][j] = -2;
00001 mov eax, -2 ; fffffffeH
00006 mov ecx, 1000000 ; 000f4240H
0000b mov edi, OFFSET ?distance@@3PAY 0DOI@HA ; distance
00010 rep stosd
This code is
- easier to read
- faster than memset
- correct
Bo Persson This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Mantorok Redgormor |
last post by:
When is it that memset caused problems?
I recall from posts in the past where someone
used memset in their code that invoked undefined
behavior.
What about the following?
char the_array;
memset(the_array, 'A', sizeof the_array);
|
by: bob_jenkins |
last post by:
{
const void *p;
(void)memset((void *)p, ' ', (size_t)10);
}
Should this call to memset() be legal? Memset is of type
void *memset(void *, unsigned char, size_t)
Also, (void *) is the generic pointer type.
My real question is, is (void *) such a generic pointer type that it
|
by: 69dbb24b2db3daad932c457cccfd6 |
last post by:
Hello,
I have to initialize all elements of a very big float point array to
zero. It seems memset(a, 0, len) is faster than a simple loop. I just
want to know whether it is safe to do so, since I know it's danger to
initialize NULL pointers this way. But how about floats?
Zhang Le
|
by: jacob navia |
last post by:
Many compilers check printf for errors, lcc-win32 too. But there are
other functions that would be worth to check, specially memset.
Memset is used mainly to clear a memory zone, receiving a pointer to
the start, the value (most of the time zero) and the size of the
memory array to clear.
Problems appear when the size given is not the size of the object
given as its first argument. For instance
void fn(void)
|
by: divya_rathore_ |
last post by:
Consider the dynamic allocation of a 2D array:
int **temp;
temp = new int*;
for (y=0; y<height; y++) temp = new int;
I have 2 Questions:
Does new initializes memory to 0?
| |
by: Patrick Kowalzick |
last post by:
Dear all,
I have an existing piece of code with a struct with some PODs.
struct A
{
int x;
int y;
};
|
by: volunteers |
last post by:
I met a question about memset and have no idea right now. Could anybody
give a clue? Thanks
memset is sometimes used to initialize data in a constructor like the
example below. What is the benefit of initializing this way? Does it
work in this example? Does it work in general ? Is it a good idea in
general?
class A {
public:
|
by: AndersWang |
last post by:
Hi,
dose anybody here explain to me why memset would be faster than a
simple loop. I doubt about it!
In an int array scenario:
int array;
for(int i=0;i<10;i++) //ten loops
|
by: dykeinthebox |
last post by:
Consider the following program:
#include <stdlib.h>
#include <string.h>
int main( void )
{
void *p = malloc( 4 );
if ( p )
{
|
by: Gaijinco |
last post by:
I'm having a headache using memset()
Given:
int v;
memset((void*)v, 1, sizeof(v));
Can I be 100% positive than v = 1 for i 0, or there is something
else I have to do?.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |