473,804 Members | 3,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what about memset?

When I want to clear memory space this is what I typically do myself,

char a[100];
int i;
for (i=0;i != 100;++i)
a[i]='\0';

Now with the function memset I could do the same thing and it would be
portable. But would it always work?

memset( a, '\0', sizeof a);

Bill
Oct 16 '08 #1
38 3659
Bill Cunningham said:
When I want to clear memory space this is what I typically do myself,

char a[100];
int i;
for (i=0;i != 100;++i)
a[i]='\0';

Now with the function memset I could do the same thing and it would be
portable. But would it always work?

memset( a, '\0', sizeof a);
Yes, for integer types (including char types).

If you only need to do it once, you can do this:

char a[100] = {0};

This is because of default initialisation rules for partial
initialisations .

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 16 '08 #2

"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:Mb******** *************** *******@bt.com. ..
If you only need to do it once, you can do this:

char a[100] = {0};

This is because of default initialisation rules for partial
initialisations .
So char a[100]={0}; sets all to zero? But that's not exactly \0 is it,
as far as C is concerned?

Bill
Oct 16 '08 #3
Bill Cunningham wrote:
"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:Mb******** *************** *******@bt.com. ..
>If you only need to do it once, you can do this:

char a[100] = {0};

This is because of default initialisation rules for partial
initialisation s.

So char a[100]={0}; sets all to zero? But that's not exactly \0 is it,
as far as C is concerned?
If that's the case, what is \0?

--
Ian Collins
Oct 16 '08 #4

"Ian Collins" <ia******@hotma il.comwrote in message
news:6l******** *****@mid.indiv idual.net...
If that's the case, what is \0?
It's supposed to mean null in addition to the string terminator.

Bill
Oct 16 '08 #5

"Ian Collins" <ia******@hotma il.comwrote in message
news:6l******** *****@mid.indiv idual.net...
If that's the case, what is \0?
When I set all elements of the a array to zero, zero is printed on my
screen 0. When I set all elements to \0 nothing is printed to my screen. No
characters are visible so I am assuming the null character is set.

Bill
Oct 17 '08 #6
Bill Cunningham wrote:
"Ian Collins" <ia******@hotma il.comwrote in message
news:6l******** *****@mid.indiv idual.net...
>If that's the case, what is \0?
When I set all elements of the a array to zero, zero is printed on my
screen 0. When I set all elements to \0 nothing is printed to my screen. No
characters are visible so I am assuming the null character is set.
Set to 0 or '0'?

--
Ian Collins
Oct 17 '08 #7
"Bill Cunningham" <no****@nspam.i nvalidwrites:
"Ian Collins" <ia******@hotma il.comwrote in message
news:6l******** *****@mid.indiv idual.net...
>If that's the case, what is \0?
When I set all elements of the a array to zero, zero is printed on my
screen 0.
The best explanation for this is that you set the elements to '0' not
zero. '0' is some number (most likely 48) that represents a character
in the source character set used by your compiler.
When I set all elements to \0 nothing is printed to my screen. No
characters are visible so I am assuming the null character is set.
\0 is not a number in C so I presume you mean '\0'. '\0' is a integer
constant expression with value 0. '\0' is 0 in almost every way --
the only difference is the way they are written. 0x0 and 000 are
other ways to write the same thing.

--
Ben.
Oct 17 '08 #8

"Ben Bacarisse" <be********@bsb .me.ukwrote in message
news:87******** ****@bsb.me.uk. ..
\0 is not a number in C so I presume you mean '\0'. '\0' is a integer
constant expression with value 0. '\0' is 0 in almost every way --
the only difference is the way they are written. 0x0 and 000 are
other ways to write the same thing.
I am pretty sure there's an ascii difference in '\0' that I use and '0'
which would print a bunch of zeros.

Bill
Oct 17 '08 #9
Bill Cunningham wrote:
"Ben Bacarisse" <be********@bsb .me.ukwrote in message
news:87******** ****@bsb.me.uk. ..
>\0 is not a number in C so I presume you mean '\0'. '\0' is a integer
constant expression with value 0. '\0' is 0 in almost every way --
the only difference is the way they are written. 0x0 and 000 are
other ways to write the same thing.
I am pretty sure there's an ascii difference in '\0' that I use and '0'
which would print a bunch of zeros.
What part of '\0' == 0 and '0' != 0 don't you understand?

--
Ian Collins
Oct 17 '08 #10

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

Similar topics

11
2068
by: Aing | last post by:
Anyone knows what can be cause of this problem? //////////////////////////////////////////////////////////// typedef struct _date_struct { int date,month,year; }date_struct; Class Date { private : date_struct m_data;
12
1936
by: Sacha Schär | last post by:
Suppose I have an array of Foo's: class Foo { public: int SomeMethode(void); int i; char c; char * p;
6
8266
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
26
26236
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
22
3165
by: srivatsan_b | last post by:
Hi, Can somebody explain whether an explicit typecast is mandatory while calling memset function for a structure? like in the following code snapshot..... struct some_structure x; memset((some_structure*)&x,0,sizeof(some_structure)); Will memset(&x,0,sizeof(some_structure)); cause some issues? Thanks in advance
14
8484
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; };
27
5238
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:
18
11991
by: dykeinthebox | last post by:
Consider the following program: #include <stdlib.h> #include <string.h> int main( void ) { void *p = malloc( 4 ); if ( p ) {
18
783
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?.
0
9708
marktang
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...
0
9587
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,...
1
10324
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
10085
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
6857
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
5527
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...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
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
3
2998
bsmnconsultancy
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...

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.