malloc

double free error with pointer to array of mpz_t

Hi, I'm currently learning libgmp and to that end I'm writing a small program which find prime factors. My program calls a function which fills an array with a varying amount of mpz_t integers, prime factors of a given number, which I need to return. I'm planning on setting the last element to NULL, so I know how many mpz_t integers the...

Unit testing C library, memory management.

I am working on a quite large C library that doesn't have any tests now. As the API starts to be final, I'd like to start writing unit tests. Nearly all my functions acts on the first parameter (a structure). The naive approach to unit test is to have the pre function call structure in a known state, call the function, and then compare...

bring malloc() back to its initial state

Do you know if there is a way to bring back malloc in its initial state, as if the program was just starting ? reason : I am developing an embedded application with the nintendods devkitpro and I would like to be able to improve debugging support in case of software faults. I can already catch most errors and e.g. return to the console ...

c pointer as inputs

Hi, when I'm trying to push elements to a stack I get segmentation fault, but if I open address for stack(i marked them with "!!!") and it's symbols it accepts it. But this time in each push, it creates new address and doesn't increase top value. typedef struct { struct table **symbols; // array of the stack int top; //index of th...

Malloc call on delete[] showing up as memory leak in totalview

I am using HDF5 to read a string in to a char* allocated by new[]. I then use a string::assign() call to copy this data to where I actually want it. I then call delete[] on that char*. This is showing up as the source of a memory leak using totalview. It shows mangled calls in stdlibc++ under delete[] to replace_safe, mutate, create,...

How do free and malloc work in C?

I'm trying to figure out what would happened if I try to free a pointer "from the middle" for example, look at the following code: char *ptr = (char*)malloc(10*sizeof(char)); for (char i=0 ; i<10 ; ++i) { ptr[i] = i+10; } ++ptr; ++ptr; ++ptr; ++ptr; free(ptr); I get a crash with an Unhandled exception error msg. I want to understand...

How does free() know how much memory to deallocate?

Possible Duplicate: C programming : How does free know how much to free? When programming in C, I often usemalloc() to allocate memory and free() to release it: MyObject* objArr= (MyObject*) malloc(sizeof(MyObject)*numberOfObjects); /** Do stuff **/ free(objArr); How does free() know how much memory to deallocate? Does malloc...

is there a difference between malloced arrays and newed arrays

I'm normally programming in c++, but are using some clibrary functions for my char*. Some of the manpages like for 'getline', says that input should be a malloced array. Is it ok, to use 'new' instead? I can see for my small sample that it works, but could this at some point result in some strange undefined behavior? I know that a 'n...

Source of a malloc error when releasing a CGImage on iPhone OS 3.0?

I've identified a bug while developing against the iPhone OS 3.0 SDK. Basically, if I create a CGImage from a bitmap image context, I get the following error when I release it: malloc: *** error for object 0x1045000: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug Here's the relevant code: CG...

Best way to handle malloc failure in Cocoa

Although it won't happen often, there are a couple of cases where my Cocoa application will allocate very large amounts of memory, enough to make me worry about malloc failing. What is the best way to handle this sort of failure in a Cocoa application? I've heard that Exceptions are generally discouraged in this development environment...

c malloc for two dimensional array

hi i have the following c code int *a; size_t size = 2000*sizeof(int); a = (int *) malloc(size); which works fine... but now if i have the following char **b = malloc(2000*sizeof *b); where every element of b has different length... now, how is it possible to do the same thing for b as i did for a, i.e. is the below c...

c size of one array to another...

hi i have the following code... int *a, *b; int *d; int N = 2000; size_t size = N*sizeof(int); a = (int *) malloc(size); b = (int *) malloc(size); ... cudaMalloc((void **) &d, size); it works just fine... now assume i have the following char **t = malloc(2000* sizeof *t); for(...) { ... t[i] = (char *)mal...

Visual Studio : malloc() crashing in Release mode

Hey, I have a Project that runs fine in Debug Mode. But in Release Mode it crashs with every malloc(); Even Winmainetc(){ char* szTest; szTest = (char*)malloc(1); } fails... Under Debug Mode all works fine. The thing is that I selected Multitrheaded (/MT) in the linker, so it statically links the mscvr90.dll into the binary. If i ...

Which way to reserve memory for a string?

I have created a macro to make reserve memory for my strings in C. It looks like this: #define newString(size) (char*)malloc(sizeof(char) + size) So is there any reason I shouldn't use this macro in my own personal projects? I know I shouldn't do this in production code because it would require everyone to have that header file and ev...

Reducing stack load, memory allocation in C and easly casting malloc()'s return value

It's known that big local/global variables may cause to a stack overflow. I know that using pointers and allocating space in memory helps to overcome this problem. But is it the only option? What happens if I have (or need) too-many pointers in global scope? Regarding the stack space: Is a global struct-type variable takes space in the ...

Dynamic memory allocation + truncating a string issue

Hi. First post here. I've been fooling around with malloc, realloc and free in order to write some basic functions to operate on C strings (char*). I've encountered this weird issue when erasing the last character from a string. I wrote a function with such a prototype: int string_erase_end (char ** dst, size_t size); It's supposed to...

using glibc malloc hooks in a thread safe manner.

I would like to monitor the use of mallocs and frees in an application by using the malloc and free hooks. Here's the documentation http://www.gnu.org/s/libc/manual/html_node/Hooks-for-Malloc.html From the example page you can see that my_malloc_hook transiently switches the malloc hook off (or to the previous hook in the chain) before...

what's the point in malloc(0)?

Just saw this code: artist = (char*)malloc(0); and I was wondering why would one do this? ...

keeping track of how much memory malloc has allocated

After a quick scan of related questions on SO, I have deduced that there's no function that would check the amount of memory that malloc has allocated to a pointer. I'm trying to replicate some of std::string basic functionality (mainly dynamic size) using simple char*'s in C and don't want to call realloc all the time. I guess I'll need...

When to free memory inside a C code ?

Hello, When I alloc memory outside a while loop for example, is it okay to free it inside it ? Are these two codes equivalent ? int* memory = NULL; memory = malloc(sizeof(int)); if (memory != NULL) { memory=10; free(memory); } int* memory = NULL; memory = malloc(sizeof(int)); if (memory != NULL) { memory=10; } free(memory); ...