malloc

Are some allocators lazy?

Hello, I wrote a C program in Linux that mallocs memory, ran it in a loop, and TOP didn't show any memory consumption. then I've done something with that memory, and TOP did show memory consumption. When I malloc, do I really "get memory", or is there a "lazy" memory management, that only gives me the memory if/when I use it? (There...

allocating and freeing a char * in c++

Hey everyone, I am getting a heap corruption error I cannot figure out. char * c = (char *) malloc(1); // main loop _gcvt_s(c, 100, ball->get_X_Direction(), 10); if(pushFont(c, (SCREEN_WIDTH - 30), (SCREEN_HEIGHT - 40), message, screen, font, textColor) == false) { //return 1; // error rendering text. } // end main loop f...

Does malloc lazily create the backing pages for an allocation on Linux (and other platforms)?

On Linux if I were to malloc(1024 * 1024 * 1024), what does malloc actually do? I'm sure it assigns a virtual address to the allocation (by walking the free list and creating a new mapping if necessary), but does it actually create 1 GiB worth of swap pages? Or does it mprotect the address range and create the pages when you actually to...

how can i override malloc(), calloc(), free() etc under OS X?

Assuming the latest XCode and GCC, what is the proper way to override the memory allocation functions (I guess operator new/delete as well). The debugging memory allocators are too slow for a game, I just need some basic stats I can do myself with minimal impact. I know its easy in Linux due to the hooks, and this was trivial under code...

Recursively freeing C structs

I have a struct that only contains pointers to memory that I've allocated. Is there a way to recursively free each element that is a pointer rather than calling free on each one? For example, let's say I have this layout: typedef struct { ... } vertex; typedef struct { ... } normal; typedef struct { ... } texture_coord; typedef struct...

char * str = (char*) malloc( ) Crash help!

Hello guys, I have this code snippet below and it crashes during the assignment in 'str', a dynamic allocation. char *str; int file_size; FILE *fptr; if (!(fptr = fopen(filename, "r"))) goto error1; if ((fseek(fptr, 0L, SEEK_END) != 0)) goto error2; if (!(file_size=ftell(fptr))) goto error2; if ((fseek(fptr, 0L, SEEK_SET)...

How to find the cause of a malloc "double free" error?

Hello I'm programming an application in Objective-C and I'm getting this error: MyApp(2121,0xb0185000) malloc: *** error for object 0x1068310: double free *** set a breakpoint in malloc_error_break to debug It is happening when I release an NSAutoreleasePool and I can't figure out what object I'm releasing twice. How do I set his br...

About thread safety in malloc and free

Possible Duplicate: Malloc thread-safe? I heard that glibc malloc() was not thread safe, since several threads of a process calling malloc() simultaneously will lead to undefined behaviour. And my question is if a thread calls free() will another thread is calling malloc(), will this lead to undefined behaviour as well? ...

C malloc/free + fgets performance

As I loop through lines in file A, I am parsing the line and putting each string (char*) into a char**. At the end of a line, I then run a procedure that consists of opening file B, using fgets, fseek and fgetc to grab characters from that file. I then close file B. I repeat reopening and reclosing file B for each line. What I would ...

Allocate data through a function (ANSI C)

Hi all, I d love to know how I can allocate data through a function, and after the function is returned the data is still allocated. This is both for basic types (int, char**) and user defined types. Below are two snipsets of code. Both have the allocation within the function though after the return the allocation goes. int* nCheck = N...

Is it necessary to multiply by sizeof( char ) when manipulating memory?

When using malloc and doing similar memory manipulation can I rely on sizeof( char ) being always 1? For example I need to allocate memory for N elements of type char. Is multiplying by sizeof( char ) necessary: char* buffer = malloc( N * sizeof( char ) ); or can I rely on sizeof( char ) always being 1 and just skip the multiplicatio...

Why is alloca not considered good practice?

Alloca allocates memory from Stack rather then heap which is case in malloc. So, when I return from the routine the memory is freed. So, actually this solves my problem of freeing up of dynamically allocated memory . Freeing of memory allocated through malloc is a major headache and if somehow missed leads to all sorts memory problems. ...

How to set a char inside a struct in C?

How do I allocate memory for a char variable (not a char pointer) inside a struct? (Variable names are in portuguese, sorry if it's kinda confusing) I have this struct: typedef struct node{ char rotulo[10], instrucao[1][2][10], flag; int simplificado; struct node *referencias[2]; struct node **antecessores; int n...

Setting variable to NULL after free ...

In my company there is a coding rule that says, after freeing any memory, reset the variable to NULL. For example ... void some_func () { int *nPtr; nPtr = malloc (100); free (nPtr); nPtr = NULL; return; } I feel that, in cases like the code shown above, setting to NULL does not have any meaning. Or am I missin...

Checking the amount of available RAM within a running program

A friend of mine was asked, during a job interview, to write a program that measures the amount of available RAM. The expected answer was using malloc() in a binary-search manner: allocating larger and larger portions of memory until getting a failure message, reducing the portion size, and summing the amount of allocated memory. I beli...

Segmentation fault when malloc/free appear in loop in C

Hi there, I have a program that basically looks like: typedef struct cpl_def { int A; int B; int OK; struct cpls *link; }cpls; int main(void) { int n1, n2; int num = 300; /* say */ int *a; ...

Will this leak memory?

Hello. I made a small function to catenate strings and return the combined string. However since I assign memory to a third variable in the function, will the memory be freed when the function finishes or will it stay there, requiring me to free it later? and if I need to free it, what's the most stylish solution to do it? Here's the te...

Didn't allocate memory by using malloc

Hi all, I am facing a problem on malloc for allocating memory: ByteArr = (BYTE *)malloc(sizeof(SHORT) * 20); I m getting error like "CXX0030: Error: expression cannot be evaluated" But if i am taking 428 or 1024 instead of 20 than its allocating the memory.Can you please tell me where is the problem ...thanks. ...

zero size malloc

Very simple question, I made the following program : #include <stdlib.h> int main(int argc, char ** argv) { void * ptr; ptr = malloc(0); free(ptr); } And it does not segfault on my machine. Is it a portable behaviour of stdlib malloc and free, or am I looking for trouble ? Edit : What seems non portable is the value retur...

Why do I get different results when I dereference a pointer after freeing it?

I've a question about the memory management in C (and GCC 4.3.3 under Debian GNU/Linux). According to the C Programming Language Book by K&R, (chap. 7.8.5), when I free a pointer and then dereference it, is an error. But I've some doubts since I've noted that sometimes, as in the source I've pasted below, the compiler (?) seems to work...