Sponsored Content
Full Discussion: memset vs calloc
Top Forums UNIX for Advanced & Expert Users memset vs calloc Post 302215726 by jim mcnamara on Thursday 17th of July 2008 05:11:10 AM
Old 07-17-2008
That is older C code (because modern malloc does not anbd should be cast).

I'm guessing:
Some older calloc implementations had problems actually zeroing out the allocated memory. So maybe the coder knew this was a problem.

Plus, calloc does call an equivalent of memset anyway - so it is up to you to choose which one to use: malloc + memset or calloc. I personally seldom use calloc.
 

8 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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
mapmalloc(3MALLOC)                                      Memory Allocation Library Functions                                     mapmalloc(3MALLOC)

NAME
mapmalloc - memory allocator SYNOPSIS
cc [ flag ... ] file ... -lmapmalloc [ library ... ] #include <stdlib.h> void *malloc(size_t size); void *calloc(size_t nelem, size_t elsize); void free(void * ptr); void *realloc(void *ptr, size_t size); DESCRIPTION
The collection of malloc functions in this library use mmap(2) instead of sbrk(2) for acquiring new heap space. The functions in this library are intended to be used only if necessary, when applications must call sbrk(), but need to call other library routines that might call malloc. The algorithms used by these functions are not sophisticated. There is no reclaiming of memory. The malloc() and free() functions provide a simple general-purpose memory allocation package. The malloc() function returns a pointer to a block of at least size bytes suitably aligned for any use. The argument to free() is a pointer to a block previously allocated by malloc(), calloc() or realloc(). If ptr is a NULL pointer, no action occurs. Undefined results will occur if the space assigned by malloc() is overrun or if some random number is handed to free(). The calloc() function allocates space for an array of nelem elements of size elsize. The space is initialized to zeros. The realloc() function changes the size of the block pointed to by ptr to size bytes and returns a pointer to the (possibly moved) block. The contents will be unchanged up to the lesser of the new and old sizes. If the new size of the block requires movement of the block, the space for the previous instantiation of the block is freed. If the new size is larger, the contents of the newly allocated portion of the block are unspecified. If ptr is NULL, realloc() behaves like malloc() for the specified size. If size is 0 and ptr is not a null pointer, the space pointed to is freed. Each of the allocation functions returns a pointer to space suitably aligned (after possible pointer coercion) for storage of any type of object. The malloc() and realloc() functions will fail if there is not enough available memory. Entry points for malloc_debug(), mallocmap(), mallopt(), mallinfo(), memalign(), and valloc() are empty routines, and are provided only to protect the user from mixing malloc() functions from different implementations. RETURN VALUES
If there is no available memory, malloc(), realloc(), and calloc() return a null pointer. When realloc() returns NULL, the block pointed to by ptr is left intact. If size, nelem, or elsize is 0, a unique pointer to the arena is returned. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |MT-Level |Safe | +-----------------------------+-----------------------------+ SEE ALSO
brk(2), getrlimit(2), mmap(2), realloc(3C), malloc(3MALLOC), attributes(5) SunOS 5.10 20 Feb 2004 mapmalloc(3MALLOC)
All times are GMT -4. The time now is 04:09 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy