malloc

c difference between malloc and calloc

What is the difference between doing: ptr = (char **) malloc (MAXELEMS * sizeof(char *)); // OR ptr = (char **) calloc (MAXELEMS, sizeof(char*)); ??? EDT: When is it a good idea to use calloc over malloc or vice versa? ...

Problems with 'Heap Buffer' Error in C

I get the following error in my C program: Writing to heap after end of help buffer Can you tell me what I'm missing? char * path_delimiter(char * path) { int i = 0, index = 0, size = 0, length = (int)strlen(path); char *tmp, *ans; for(; i < length; i++) { if(path[i] == PATH_DELIM[0]) { break; ...

I have a dynamic library, how to know if it calls malloc during run-time?

Hi, I need to do networking in uCsimm, Motorola Dragon Ball. As I'm running uClinux with RTAI patch, and I need real-time performance, therefore all malloc and its friends are undesirable. I have the following piece of code for socket dynamic library. How to know that it calls malloc during run-time? When I compiled in cygwin on Windows,...

tcmalloc: how can I get my malloc calls overridden when compiling statically?

When I use LD_PRELOAD=/usr/local/lib/libtcmalloc.so, all my calls to malloc become tcmalloc calls. However, when I link statically against libtcmalloc, I find that straight malloc is getting called unless I still use the LD_PRELOAD setting. So how can I statically compile against tcmalloc in such a way that my mallocs hook into tcmallo...

strange malloc behavior in C

I am trying to create a matrix with dynamic proportions and to initialize it here is the code I am using to allocate memory and initialization: int **matrix; //mem allocation matrix=(int*)malloc(sizeof(int*)*mat_w); for (i=0;i<mat_w;i++) matrix[i]=(int)malloc(sizeof(int)*mat_h); //init for (i=0;i<mat_w;i++) for (j=0;j<mat_h;j++)...

Divide large malloc-block into smaller "partitions"

Is there a way to use poiter arithmetic on a large malloc block, so you can assign multiple structs or primitive data types to that area already allocated? I'm writing something like this but it isnt working (trying to assign 200 structs to a 15000byte malloc area): char *primDataPtr = NULL; typedef struct Metadata METADATA; struct M...

Using strtok() on an allocated string?

Is there anything I should know about using strtok on a malloced string? In my code I have (in general terms) char* line=getline(); Parse(dest,line); free(line); where getline() is a function that returns a char * to some malloced memory. and Parse(dest, line) is a function that does parsing online, storing the results in dest, (whic...

malloc code in C

I have a code block that seems to be the code behind malloc. But as I go through the code, I get the feeling that parts of the code are missing. Does anyone know if there is a part of the function that's missing? Does malloc always combine adjacent chunks together? int heap[10000]; void* malloc(int size) { int sz = (size + 3) / 4; int ...

The C# compiler and memory management

Hello. I'm doing a compiler design class on the topic of memory management. I am reading about garbage collection and noticed that most of that low level stuff takes place with C/C++ code. I have a few questions about the c# compiler. Was .net framework part for memory management written in c/c++? How does .net manages memory? Does the...

Malloc/free algorithm in Suse

Which implementation of malloc/free is used in a specific Linux distribution (in my case Suse 9 and Suse 10) ? Has it change between both versions ? Is it the same algorithm for 32 bits and 64 bits versions ? ...

malloc zeroing out memory?

Given this C code compiled with gcc 4.3.3 #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { int * i; i = (int *) malloc(sizeof(int)); printf("%d\n", *i); return 0; } I would expect the output to be whatever was in the memory that malloc() returns, but instead the output is 0. Is malloc ze...

how does jemalloc work? what are the benefits?

Firefox 3 came with a new allocator: jemalloc I have heard at several places that this new allocator is better. The top Google results don't gave any further information though and I am interested in how exactly it works. ...

Having problems writing my own malloc, free, and realloc...

I'm having some trouble implementing my own dynamic memory allocator using an Implicit list to track free blocks in c. ****Specifically I'm having problems with implementing realloc and changing the code from find-fit/first-fit to next-fit. Best-fit would be ideal but lets just say next fit for now. I've got everything thing else down,...

ANSI C getc causes segfault on Linux but not OS X

I have some ANSI C code that I developed on my Mac, but when I tried running it on our school's Linux servers I get a segfault. The specific line that is causing me trouble is a getc from a file pointer. The file does exist. Here is the method in question: // inits lists with all data in fp file pointer // returns # of lines read int...

Why do we need to typecast what malloc returns?

int length = strlen(src); char *structSpace = malloc(sizeof(String) + length + 1); String *string = (String*) structSpace; int *string = (int*) structSpace; *I created a struct called String ...

c malloc questions (mem corruption)

When using malloc, if it produces a core dump with the error: malloc(): memory corruption: ....... *** Does this mean that malloc tried to allocate memory that was not free to allocate? IF so what are the causes of this? ...

I figured out how to write realloc, but I know the code is not right?

What I decided to do is call malloc copy the old block to the new block free the old block and return the pointer to the new block The code below is what I have so far...but I know it is not right...any help on fixing the code would be greatly appreciated... If you need more code than what I have provided I have a post previous to...

Memory allocation in C

Hi, the following is a very very simple version of malloc() and seems to allocate some space to me, but apart from the fact that there is no free() and I don't check if I've overrun the allocated space, how can I check that the code is correct? Any obvious blunders that "C" experts would slap me for? #include <stdio.h> #include <unistd...

What happens if I try to access memory beyond a malloc()'d region?

I've allocated a chuck of memory with char* memoryChunk = malloc ( 80* sizeof(char) + 1); What is keeping me from writing into the memory location beyond 81 units? What can I do to prevent that? void testStage2(void) { char c_str1[20] = "hello"; char* ut_str1; char* ut_str2; printf("Starting stage 2 tests\n"); strcat(c_str1, " wor...

C Memory Allocation: Why there is not enough memory(250K only)

Hi, I am having trouble figuring out the reason why my .c code is having trouble allocating ~250K of memory. Here is the allocation code: struct IMAGE { int width, height, maxval; char **data; }; void raiseError(char *msg) { printf("%s", msg); getch(); exit(1); } //... IMAGE readPGM() { IMAGE image; image....