memset vs calloc


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users memset vs calloc
# 8  
Old 07-17-2008
Not to disagree, but

I wanted to make some mention here regarding coding -

calloc() takes two parameters, a count and a size. It simply multiplies the two values, requests that much from the heap and then does a bzero() on the result, and then returns the beginning address of the array. The result is in fact no different from a malloc() of that same product followed by a bzero() other than the typing used for the calloc() call and for the variable to which it's return value is stored. The size value can be anything from a "char" (one byte), an int (like four bytes on a 32bit machine), or a structure (size specific to the struct declaration). Sometimes used for what we used to call a "dope vector", that is, the address of the start of an array of addresses, for example. like this:
char **cpp = (char **) calloc( 10, sizeof(char *) );
in a very simple example. The variable "cpp" then becomes the address of an array of character pointers. Check K&R's "shell sort" as a good example of how one can use such a construct.

memset() is used to set a pre-allocated block of memory to a specific value. A very different kind of thing. Useful, but to be honest I don't use it much.

These days, C++ gives us "new" and we use constructors which may clear values. One thing to remember - if you compile a "C" program with the debug flag set, variables are set to zero, even the "automatic" ones. Remove the debug flag and the automatic values are no longer set. I saw one poor fellow loose an entire day trying to figure out why his program broke with no changes when he compiled it "in production". He had failed to initialize one of his automatic variables.

Last edited by fsahog; 07-17-2008 at 08:23 PM.. Reason: To make it better
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Emergency UNIX and Linux Support

Memset fails on Solaris

Hi, memset call is failing on solaris for me. I wrote below code and that also fails. Any hints? void *memset(void *dst, int c, size_t n) { if (n) { char *d = dst; do { *d++ = c; } while (--n); } return dst; } (2 Replies)
Discussion started by: skyineyes
2 Replies

2. Programming

calloc fails: 'Cannot allocate memory'

Hi , experts. I work on Linux station (RedHat 5.7), regular user, but have root password. %> uname -a Linux ran1log06 2.6.18-238.1.1.el5 #1 SMP Tue Jan 4 13:32:19 EST 2011 x86_64 x86_64 x86_64 GNU/Linux %> cat /etc/issue Red Hat Enterprise Linux Client release 5.7 (Tikanga) Kernel \r on... (5 Replies)
Discussion started by: baruchgu
5 Replies

3. Programming

Iterating and calloc questions.

Whilst creating the function readjust_descr I have stumble across what may be a problem or something that might just work. I was hoping someone could look at the code below and tell me if readjust_descr will clear all null pointers from the structure descr_list. struct descr descr_list =... (6 Replies)
Discussion started by: Errigour
6 Replies

4. Programming

Calloc C++

Hello all, I have a confusion with calloc function : wz. the difference between the following 2 statemnts: char *ptr; char = (char*)calloc(num, sizeof(char)); char = (char*)calloc(num, sizeof(char*)); Am really confused!!!!! ---------- Post updated at 09:32 AM... (1 Reply)
Discussion started by: mind@work
1 Replies

5. Programming

C bzero() to memset() issue

Hi guys, my tool works fine in gentoo, ubuntu now im trying to port it to windows but bzero/bcopy I read aren't working on windows and for better portability I should of use memset() so im trying to translate bzero(buffer,256);in printf("MAIL TO"); strcpy(buffer, rcp); ... (4 Replies)
Discussion started by: Jess83
4 Replies

6. Solaris

application Crashes on memset ?? any suggestions

Hi All, we have an application that is written in 'C' programming to connects to various servers in the organization. The bellow code establish a TCP connection to connect to the remote servers. the application works perfectly ok, but, after some time the entire process get's crashed and... (2 Replies)
Discussion started by: sudharma
2 Replies

7. UNIX for Dummies Questions & Answers

questions in memset

HI all , please find the piece of code below char *t; char *f; char buf; memset(buf,0,50); after that i am assigning memory for (i=0; i<100; i++) { t = buf+(i*6); f = "ARUN"; } my question .. 1) i have run this it is... (7 Replies)
Discussion started by: arunkumar_mca
7 Replies

8. Programming

about memset fuction

Dear all, In my code,i am planning to use memset function to re-initialise an array before populating it everytime. Will using memset function be an overload to the program? (3 Replies)
Discussion started by: ranj@chn
3 Replies
Login or Register to Ask a Question