malloc

Freeing all malloc()-created pointers with one command?

Is there a one-liner that will free the memory that is being taken by all pointers you created using mallocs? Or can this only be done manually by freeing every pointer separately? ...

How do I declare an array created using malloc to be volatile in c++

I presume that the following will give me 10 volatile ints volatile int foo[10]; However, I don't think the following will do the same thing. volatile int* foo; foo = malloc(sizeof(int)*10); Please correct me if I am wrong about this and how I can have a volatile array of items using malloc. Thanks. ...

free() not freeing up memory properly?

I'm trying to free up the memory I've allocated with malloc, but the free command doesn't seem to do its job properly according to Eclipse's debugger. How's this possible? Below is a screenshot of my debugger after it supposedly freed up seCurrent->student->year, which is clearly not the case. year was allocated using malloc. ...

What is a Memory Heap?

What is a memory heap ? ...

sYSMALLOc: Assertion failed - any ideas how to debug effectively ?

Hello, My server daemon works fine on most machines however on one I am getting: malloc.c:3074: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct mall...

C(++) malloc confusion

I'm just not getting any further with allocating memory for arrays in C and mainly C++. I've looked for examples but there aren't any useful ones for me out there, at least it seems so. So if I have a typedef here like this: typedef struct { int x; int y; } Coordinate; Coordinate* myList; And I have an array of the type Coordinate t...

C++ malloc - dynamic array

Assuming that I have a program that has an array of unknown lenght that consists of Customers. Here, a customer struct: struct Customer { char* lastname; char* firstname; int money; }; And here - an array: Customer* CustomerDB; Okay. But the thing is that I want to add and remove customers dynamically during runtime. I don't wa...

Allocating memory for a char array to concatenate a known piece of text and an integer

I want to concatenate a piece of text, for example "The answer is " with a signed integer, to give the output "The number is 42". I know how long the piece of text is (14 characters) but I don't know how many characters the string representation of the number will be. I assume the worst case scenario, the largest signed 16-bit integer ...

what happens when tried to free memory allocated by heap manager, which allocates more than asked for?

This question was asked to me in an interview. Suppose char *p=malloc(n) assigns more than n,say N bytes of memory are allocated and free(p) is used to free the memory allocated to p. can heap manager perform such faulty allocation ? what happens now, will n bytes are freed or N bytes are freed? is there any method to find how much m...

Weird behavior of malloc()

Trying to understand answers to my question http://stackoverflow.com/questions/2336345/what-happens-when-tried-to-free-memory-allocated-by-heap-manager-which-allocates I wrote this function and puzzled by its output int main(int argc,char **argv){ char *p,*q; p=malloc(1); strcpy(p,"01234556789abcdefghijklmnopqrstuvwxyz"); //si...

How to ensure a dynamically allocated array is private in openmp

I'm working in C with openMP using gcc on a linux machine. In an openmp parallel for loop, I can declare a statically allocated array as private. Consider the code fragment: int a[10]; #pragma omp parallel for shared(none) firstprivate(a) for(i=0;i<4;i++){ And everything works as expected. But if instead I allocate a dynamically, i...

Heap corruption issues

Inside my template function I have the following code: TypeName myFunction() { TypeName result; void * storage = malloc( sizeof( TypeName ) ); /*Magic code that stores a value in the space pointed to by storage*/ result = *(TypeName *)storage; free( storage ); return result; } This causes an "HEAP CORRU...

Is there an issue in C with malloc and MPI?

Hi all, Sorry I cannot post any source code... I have a code running a master/slave red-black algorithm for a G.S. solver. In the simple case, the matrix is split into 4 evenly sized computational pieces. The images 1-3 perform their part of the computation, and send back buffers with the results to image 0. The problem is this: I hav...

Using strdup into malloc reserved space

I've never used malloc to store more than values but I have to use strdup to order the lines of an input file and I dont get a way to make it work. I though using strdup() to get a pointer to each line and later, put each one into a space according to the number of lines reserved with malloc(). I dont know if I have to do it like reser...

Can you recommend a good debugging malloc library for linux?

Can you recommend a good debugging malloc library for linux? I know there are a lot of options out there, I just need to know which libraries people are actually using to solve real-life problems. Thanks! EDIT: I know about Valgrind, but sometimes the performance is really too low. ...

To malloc or not to malloc, that is the question!

Do I need to malloc when creating a file to write to? The file will be based on the contents of 2 others, so would I need to malloc space for the writeable file of sizeof( file a ) + sizeof( file b) + 1? Sorry if this makes no sense; if it doesn't then I guess I need to go read some more :D Essentially, I have 2 txt files and a stri...

C Linked List Memory Usage (Possible memory leak)

Hi, I'm having trouble with what should be a simple program. I've written a single linked list implementation in C using void* pointers. However, I have a problem, as there is a possible memory leak somewhere, however I checked the code using valgrind and it detected no such errors. But when all the memory is free'd there is still som...

A few questions about my modular code using void* as dynamic data type in C

Hi, A few days ago I posted this question and everyone suggested me to use void*, which I did. I think some of them also pointed a few things that I would need to take care of but I'm not sure what exactly were they. However, I'm having a few problems with this... I'm not going to post all my code where cause it's quite large, instead,...

fread/fwrite string in C

I have a binary file which contains records. The structure of the file is as such: Structure (see below) Name String Address String The structure in question: typedef struct{ char * name; char * address; short addressLength, nameLength; int phoneNumber; }employeeRecord; employeeRecord record; I get the name as such:...

Multidimensional array with unequal second dimension size using malloc()

Hello, I am playing around with multidimensional array of unequal second dimension size. Lets assume that I need the following data structure: [&ptr0]->[0][1][2][3][4][5][6][7][8][9] [&ptr1]->[0][1][2] [&ptr2]->[0][1][2][3][4] int main() { int *a[3]; int *b; int i; a[0] = (int *)malloc(10 * sizeof(int)); a[1] = (int *)malloc(2...