Hi, guys,I met a problem, Please look at the problem below:
int* bit = (int*)malloc(10 000*sizeof(int) );
memset(bit, 1, 10000*sizeof(in t));
printf("%d %d %d\n", bit[1],bit[2], bit[9999]);
Output: 16843009 16843009 16843009
Obviously I set the bit[0] to bit[9999] to 1, but it outputs are not
1's.
Why is this happen? Do I misused memset or sth else?
Any help is appreciated. Thanks! 17 6351
Frederick Ding said: Hi, guys,I met a problem, Please look at the problem below:
int* bit = (int*)malloc(10 000*sizeof(int) );
int *bit = malloc(10000 * sizeof *bit);
if(bit != NULL)
{
memset(bit, 1, 10000*sizeof(in t));
Oops - that won't do what you were hoping.
printf("%d %d %d\n", bit[1],bit[2], bit[9999]);
Output: 16843009 16843009 16843009
Obviously I set the bit[0] to bit[9999] to 1, but it outputs are not 1's.
Observe this magic:
unsigned char *p = (unsigned char *)bit;
while(p < (unsigned char *)(bit + 1))
{
printf("%d\n", *p++)
}
This prints every byte in your first int. Try it, and I think you'll
understand all about memset.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Frederick Ding wrote: Hi, guys,I met a problem, Please look at the problem below:
int* bit = (int*)malloc(10 000*sizeof(int) ); memset(bit, 1, 10000*sizeof(in t)); printf("%d %d %d\n", bit[1],bit[2], bit[9999]);
Output: 16843009 16843009 16843009
Obviously I set the bit[0] to bit[9999] to 1, but it outputs are not 1's.
Why is this happen? Do I misused memset or sth else?
Any help is appreciated. Thanks!
Your call to memset() sets each byte to 1. Your ints probably occupies 4
bytes, so you initialize each int with bit patterns 0x01010101, which
equals 16843009.
Bjørn
"Frederick Ding" <di*******@gmai l.com> writes: Hi, guys,I met a problem, Please look at the problem below:
int* bit = (int*)malloc(10 000*sizeof(int) );
Don't cast the result of malloc. It can mask errors such as failing
to include <stdlib.h> or compiling C code with a C++ compiler.
The recommended idiom is:
int *bit = malloc(10000 * sizeof *bit);
You should check whether the malloc() call succeeded.
memset(bit, 1, 10000*sizeof(in t));
This sets every byte of the array to 1.
printf("%d %d %d\n", bit[1],bit[2], bit[9999]);
Output: 16843009 16843009 16843009
Obviously I set the bit[0] to bit[9999] to 1, but it outputs are not 1's.
16843009 in hexadecimal is 0x01010101.
memset() sets every byte to a specified value. There's no equivalent
standard function to set every int to a specified value.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
"Frederick Ding" <di*******@gmai l.com> wrote Obviously I set the bit[0] to bit[9999] to 1, but it outputs are not 1's.
Why is this happen? Do I misused memset or sth else?
Any help is appreciated. Thanks!
memset isn't a very useful function.
It sets every byte to a given value. The only practical use, really, is to
set memory to zero, unless, as you say, you happen to want a string of
asterisks or something.
You cannot set integers to a non-zero value.
You can set pointers to NULL and floating point values to zero, but not with
complete portability (the NULL pointer isn't always all bits zero).
Your best bet is to ignore it and use a for loop, setting your array to the
values you want.
"Malcolm" <re*******@btin ternet.com> wrote in message
news:dm******** **@nwrdmz02.dmz .ncs.ea.ibs-infra.bt.com...
[snip] memset isn't a very useful function.
I agree.
It sets every byte to a given value. The only practical use, really, is to set memory to zero, unless, as you say, you happen to want a string of asterisks or something. You cannot set integers to a non-zero value.
AFAIK you can't portably set them to zero either, except possibly the
fixed-size ones added in C99 (uint32_t etc).
You can set pointers to NULL and floating point values to zero, but not with complete portability (the NULL pointer isn't always all bits zero).
Neither, as I'm sure you meant, is a floating point all-bits-zero
necessarily equal to zero.
Alex
"Alex Fraser" <me@privacy.net > wrote It sets every byte to a given value. The only practical use, really, is to set memory to zero, unless, as you say, you happen to want a string of asterisks or something. You cannot set integers to a non-zero value.
AFAIK you can't portably set them to zero either, except possibly the fixed-size ones added in C99 (uint32_t etc).
int array[100];
memset(array, 0, 100 * sizeof(int));
is guaranteed to produce an array to 100 integers of value 0, on an ANSI
system.
(However if technology changes so that for some reason it doesn't make sense
to have value zero all bits clear, the ANSI standard will be a dead letter).
Frederick Ding a écrit : Hi, guys,I met a problem, Please look at the problem below:
int* bit = (int*)malloc(10 000*sizeof(int) ); memset(bit, 1, 10000*sizeof(in t)); printf("%d %d %d\n", bit[1],bit[2], bit[9999]);
Output: 16843009 16843009 16843009
Many things are missing. It may work or not...
Obviously I set the bit[0] to bit[9999] to 1, but it outputs are not 1's.
Assuming the code is complete, yes there are. It will appears more
clearly like this:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main(void)
{
size_t const size = 10000;
int* bit = malloc (size * sizeof *bit);
if (bit != NULL)
{
memset(bit, 1, size * sizeof *bit);
printf("%X %X %X\n"
, (unsigned) bit[1]
, (unsigned) bit[2]
, (unsigned) bit[size-1]);
free (bit), bit = NULL;
}
return 0;
}
1010101 1010101 1010101
see the pattern ?
--
A+
Emmanuel Delahaye
Frederick Ding a écrit : Hi, guys,I met a problem, Please look at the problem below:
int* bit = (int*)malloc(10 000*sizeof(int) ); memset(bit, 1, 10000*sizeof(in t)); printf("%d %d %d\n", bit[1],bit[2], bit[9999]);
Output: 16843009 16843009 16843009
Many things are missing. It may work or not...
Please don't forget to free what have been allocated :
<<SYSALLOC Bloc 003D4A70 (40000 bytes) malloc'ed at line 14 of 'main.c'
not freed>>
Obviously I set the bit[0] to bit[9999] to 1, but it outputs are not 1's.
Assuming the code is complete, yes there are. It will appears more
clearly like this:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main(void)
{
size_t const size = 10000;
int* bit = malloc (size * sizeof *bit);
if (bit != NULL)
{
memset(bit, 1, size * sizeof *bit);
printf("%X %X %X\n"
, (unsigned) bit[1]
, (unsigned) bit[2]
, (unsigned) bit[size-1]);
free (bit), bit = NULL;
}
return 0;
}
1010101 1010101 1010101
see the pattern ?
--
A+
Emmanuel Delahaye
Thanks, guys!
I'm very happy that so many kind people help me.
I've learned a lot from you, THANKS!
Yet I still have two questions:
1. malloc(10000 * sizeof *bit);
^^^^^^^^^^^^
sizeof *bit what's this, can sizeof be used like this? wow,
I can not find this expression in all of my C books.
where can I find some reference to this?
I think sizeof is a function that calculate a type's size, so it
should be used with ( ),
and the * between "sizeof" and "bit" is a pointer or a multiply? I'm
really confused.
2. I once used memset like this, and it looked like works:
memset(semi,-1,sizeof(float) *16);
memset(final,-1,sizeof(float) *16);
memset(cup,-1,sizeof(float) *16);
printf("%f %f\n",semi[0],final[15]);
Output: -1.#QNAN0 -1.#QNAN0
It seemed that it was "-1" and I omitted the topic I just asked.
Now the question is "-1.#QNAN0" means what?
what is the #QNAN0?
Thank you for your patience and time! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: J. Campbell |
last post by:
From reading this forum, it is my understanding that C++ doesn't
require the compiler to keep code that does not manifest itself in any
way to the user. For example, in the following:
{
for(int i = 0; i < 10; ++i){
std::cout << i << std::endl;
for(int j = 0; j < 0x7fffffff; ++j){}
}
}
|
by: Magix |
last post by:
Hi,
Let say I have:
char szBuffer="";
1. Which one is the better option to reset the buffer ? and why?
strcpy(szBuffer,"");
memset(szBuffer,'\0',sizeof(szBuffer));
OR
|
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: Samuel R. Neff |
last post by:
Why would you cast two strings to objects to compare them? I saw code
in an MS sample on MSDN and don't get it.
if ( (object)name == (object)attr.name ) {
both "name" and "attr.name" are declared as string.
http://msdn.microsoft.com/XML/BuildingXML/XMLinNETFramework/default.aspx?pull=/library/en-us/dnxmlnet/html/XmlBkMkRead.asp
Thanks,
|
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: 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: silversurfer2025 |
last post by:
Hello everybdy,
I am a little confused for the following reason:
In my code I used a simple for-loop in order to initialize a 2D-array
of floats to zero. Because of efficiency reasons, I changed it to use
memset and I get totally different results.. How can this be?
Here is the example:
float gaborfilter;
|
by: nass |
last post by:
hello everyone, i have a bit of problem reading char * strings from a
buffer (a shared memory, pointed to by 'file_memory').
basically i have a structure in memory 'ShMem' that can be accessed by
2 applications (or will be at least when it is done).
the structure is declared in the procedure under the pointer infoVar.
members tXXL are integer lengths of the strings that as all saved
(concatenated) at address of oAndS_str, which is of type...
|
by: merrittr |
last post by:
I need some C advice I want to read in string commands from a user
when the user enters a \n
I want to push it on the stac. Then at some point , if the user enters
the word print pop off and print each word (or using another stack
pointer scan the stack printing each string). here is a stub of what i
want to do. (how do I implement this currently my code doesn't work
due to my lack of strings and pointers)
#include <stdio.h>
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |