473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

memset

7
why use memset and what does memset(a,0,sizeof(a)); represents?can some one make me clear abt memset with some examples?thanks..
Nov 30 '06 #1
3 5235
Manjiri
40
why use memset and what does memset(a,0,sizeof(a)); represents?can some one make me clear abt memset with some examples?thanks..

If you are going to declare say a array dianamically.... after allocating memory by using malloc or calloc... U can use the memset function to intialise the elements of array with null....


(a,0,sizeof(a));

It syas that a is array and you are going to initailse the elements with null....

For example

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int  main()
  3. {
  4. char *s;
  5. s=(char*) malloc(20);
  6. memset(s,'\0',20);
  7. printf("Enter your name\n");
  8. gets(s);
  9. printf("your name is\n");
  10. puts(s);
  11. return 0;
  12. }  
Hope you are satisfied with ans...
Nov 30 '06 #2
r035198x
13,262 8TB
"The Standard C library function memset( ) (in <cstring>) is used for convenience .... It sets all memory starting at a particular address (the first argument) to a particular value (the second argument) for n bytes past the starting address (n is the third argument). Of course, you could have simply used a loop to iterate through all the memory, but memset( ) is available, well-tested (so it’s less likely you’ll introduce an error), and probably more efficient than if you coded it by hand."


From Thinking in c++
Nov 30 '06 #3
rsr
25
thanks for all the replies
Dec 22 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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; ...
6
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...
26
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...
21
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...
9
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?
14
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
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...
23
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
18
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
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.