C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
what is the use of memset ? Posted by ssvasan on 29 Aug 2004 at 6:58 AM
Hi all,

Please any one give best use of memset() function..

thanks
- sri

Report
Re: what is the use of memset ? Posted by stober on 29 Aug 2004 at 8:13 AM
: Hi all,
:
: Please any one give best use of memset() function..
:
: thanks
: - sri
:
:

initialize a buffer to all known values -- normally 0.
char buf[255];
memset(buf,0,sizeof(buf));

Report
Re: what is the use of memset ? Posted by ssvasan on 30 Aug 2004 at 1:07 AM
: : Hi all,
: :
: : Please any one give best use of memset() function..
: :
: : thanks
: : - sri
: :
: :
:
: initialize a buffer to all known values -- normally 0.
:
: char buf[255];
: memset(buf,0,sizeof(buf));
: 

:

Thanks for the reply.

Is there any other way available for initializing the buffer ?



Report
Re: what is the use of memset ? Posted by stober on 30 Aug 2004 at 2:19 AM
: Is there any other way available for initializing the buffer ?

certainly. here are some
char buffer[BUFSIZ];
// initialize it
for(int i = 0; i < BUFSIZE; i++)
  buffer[i] = 0;

or

char buffer[] = "Hello World";

or
char buffer[255] = {0} // only 1st byte set to 0

Report
Re: what is the use of memset ? Posted by ssvasan on 30 Aug 2004 at 7:34 AM
I managed to use the memset()... but what the arguments signifies?
memset(buf, 0, sizeof(buf)) : why the 0 ? can we any other integer ? what happens when we use any other integer ?

- sri



: : Is there any other way available for initializing the buffer ?
:
: certainly. here are some
:
: char buffer[BUFSIZ];
: // initialize it
: for(int i = 0; i < BUFSIZE; i++)
:   buffer[i] = 0;
: 
: or
: 
: char buffer[] = "Hello World";
: 
: or
: char buffer[255] = {0} // only 1st byte set to 0
: 

:

Report
Re: what is the use of memset ? Posted by stober on 30 Aug 2004 at 7:43 AM
: I managed to use the memset()... but what the arguments signifies?
: memset(buf, 0, sizeof(buf)) : why the 0 ? can we any other integer ? what happens when we use any other integer ?
:
: - sri

you could use other integers, but 0 is the normal one, and is used by all standard c and c++ libraries to indicate "no data" or "end of string". You can use any value between 0 and 255, but their significance would only be in your program. You can't use values outside that range because a char data type will not hold them.
Report
Re: what is the use of memset ? Posted by ssvasan on 30 Aug 2004 at 7:56 AM
Whether the 0 is used for filling all the elements in the buffer ? or it specifies the starting element from which we have to initialize the memory ? This memset applicable only for the character buffers or can we use it for any pointer ?

thanks for the reply.

- sri



: : I managed to use the memset()... but what the arguments signifies?
: : memset(buf, 0, sizeof(buf)) : why the 0 ? can we any other integer ? what happens when we use any other integer ?
: :
: : - sri
:
: you could use other integers, but 0 is the normal one, and is used by all standard c and c++ libraries to indicate "no data" or "end of string". You can use any value between 0 and 255, but their significance would only be in your program. You can't use values outside that range because a char data type will not hold them.
:

Report
Re: what is the use of memset ? Posted by stober on 30 Aug 2004 at 10:01 AM
: Whether the 0 is used for filling all the elements in the buffer ? or it specifies the starting element from which we have to initialize the memory ? This memset applicable only for the character buffers or can we use it for any pointer ?
:

memset() can be used to initialize arrays of any type -- just don't use it on c++ classes! Using values other than 0 will not work if you attempt to initialize an int array with memset(). If you want it initialized with some value other than 0 you will have to do it the hard way with a loop.
int array[255];
// initialize all elements to 0
memset((char *)array,0,sizeof(array));


Report
Re: what is the use of memset ? Posted by mutkanna on 15 Feb 2012 at 11:19 PM
memset(*destination, value_stored_in_destination, Size_of_value);

for eg:

char buf[100];
memset(buf, 5 , 5);

Ans : buf[0], buf[1], buf[2], buf[3], buf[4] all are initiallized to Zero.

I hope that memset is a function that will did...

KNDLY TELL ANY OTHER SUGGESTIONS .. PLS
Muthukumar Pavel



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.