473,748 Members | 11,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is memset used only for strings?

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!

Dec 4 '05
17 6361
Alex Fraser wrote:
AFAIK you can't portably set them to zero either, except possibly the
fixed-size ones added in C99 (uint32_t etc).


For character sets, a byte with all bits zero,
is a valid null character.

--
pete
Dec 4 '05 #11
On 4 Dec 2005 04:57:16 -0800, in comp.lang.c , "Frederick Ding"
<di*******@gmai l.com> wrote:
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?
sizeof is an operator, not a function. Its operand must be either a
type or an object. If the operand is an object, it doesn't need the
parens.
where can I find some reference to this?
The ISO C standard
6.5.3.4 The sizeof operator
Constraints
1 The sizeof operator shall not be applied to an expression that has
function type or an incomplete type, to the parenthesized name of such
a type, or to an expression that designates a bit-field member.
Semantics
2 The sizeof operator yields the size (in bytes) of its operand, which
may be an expression or the parenthesized name of a type.
I think sizeof is a function that calculate a type's size, so it
should be used with ( ),
No, its an operator
and the * between "sizeof" and "bit" is a pointer or a multiply?
No, its the indirection operator, same as in
int *bit;
2. I once used memset like this, and it looked like works:
memset(semi,-1,sizeof(float) *16);

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?


it means your number is Not A Number. Clearly setting all bytes of a
float to -1 on your system causes the float to be a non-number.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 4 '05 #12
Frederick Ding a écrit :
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.
It's a short way for sizeof p[0] that can be written *(p + 0), hence
*(p) hence finally *p.

It means 'the size of the first element of the pointed array'.
where can I find some reference to this?
Pointer arithmetics.
I think sizeof is a function that calculate a type's size, so it
Nope. sizeof is a unary operator. It works like this :

size_t size = sizeof object
size_t size = sizeof (type)
should be used with ( ),
It depends on the context. Note that an excess of parenthesis is
harmless (kinda belt and suspensers strategy !).
and the * between "sizeof" and "bit" is a pointer or a multiply?
None of them. It's the indirection operator (or dereference operator).
2. I once used memset like this, and it looked like works:
memset(semi,-1,sizeof(float) *16);
Don't use memset() on floating points. The result is implementation
dependent or more likely undefined.
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?


NAN means Not A Number. IOW, the internal format is undefined. Once
again, don't to that. What did you intent to do ?

--
A+

Emmanuel Delahaye
Dec 4 '05 #13
On Sun, 04 Dec 2005 14:51:21 +0100, in comp.lang.c , Emmanuel Delahaye
<em***@YOURBRAn oos.fr> wrote:
Frederick Ding a écrit :
memset(final,-1,sizeof(float) *16); Output: -1.#QNAN0 -1.#QNAN0
NAN means Not A Number. IOW, the internal format is undefined.


See for example:
http://stevehollasch.com/cgindex/coding/ieeefloat.html
if you examine the bitpattern your memset created, and compare it to
the table near the bottom, all will be revealed.
Once again, don't to that. What did you intent to do ?


I imagine he wante to set all his floats to value -1. To the OP:
memset does /exactly/ what it says on the tin - sets n bytes of memory
to value m. It does NOT set the value of the variable to n.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 4 '05 #14
"Malcolm" <re*******@btin ternet.com> wrote in message
news:dm******** *@nwrdmz02.dmz. ncs.ea.ibs-infra.bt.com...
"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.


"All bits zero" implies "all padding bits zero" and I don't see why that
couldn't be a trap representation for int.

Alex
Dec 4 '05 #15
"pete" <pf*****@mindsp ring.com> wrote in message
news:43******** ***@mindspring. com...
Alex Fraser wrote:
AFAIK you can't portably set [integers] to zero [with memset] either,
except possibly the fixed-size ones added in C99 (uint32_t etc).


For character sets, a byte with all bits zero,
is a valid null character.


Noted.

Alex
Dec 4 '05 #16
"Alex Fraser" <me@privacy.net > writes:
"Malcolm" <re*******@btin ternet.com> wrote in message
news:dm******** *@nwrdmz02.dmz. ncs.ea.ibs-infra.bt.com...
"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.


"All bits zero" implies "all padding bits zero" and I don't see why that
couldn't be a trap representation for int.


The response to Defect Report #263 says:

For any integer type, the object representation where all the bits
are zero shall be a representation of the value zero in that type.

This is published in Technical Corrigendum 2, and appears in n1124
(which includes the C99 standard with TC1 and TC2 merged in).

--
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.
Dec 4 '05 #17
"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
"Alex Fraser" <me@privacy.net > writes:
"Malcolm" <re*******@btin ternet.com> wrote in message
news:dm******** *@nwrdmz02.dmz. ncs.ea.ibs-infra.bt.com... [snip]
memset(array, 0, 100 * sizeof(int));

is guaranteed to produce an array to 100 integers of value 0, on an
ANSI system.


"All bits zero" implies "all padding bits zero" and I don't see why
that couldn't be a trap representation for int.


The response to Defect Report #263 says:

For any integer type, the object representation where all the bits
are zero shall be a representation of the value zero in that type.

This is published in Technical Corrigendum 2, and appears in n1124
(which includes the C99 standard with TC1 and TC2 merged in).


Thank you.

Alex
Dec 4 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2532
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){} } }
9
482
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
26
26215
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
14
2413
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,
21
8782
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)
27
5226
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:
22
26736
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;
4
3576
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...
20
5743
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>
0
8828
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,...
0
9367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9319
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,...
0
9243
tracyyun
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...
0
8241
agi2029
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...
0
6073
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();...
0
4599
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...
1
3309
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
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.