malloc

Generating 2-dimensional vla ends in segmentation fault

Hi, further developing the code from yesterday (seg fault caused by malloc and sscanf in a function), I tried with the help of some tutorials I found on the net to generate a 2-dim vla. But I get a segmentation fault at (*data)[i][j]=atof(p);. The program is supposed to read a matrix out of a text file and load it into a 2d array (cols ...

In C, as free() knows an array size, why isn't there a function that gets the array size?

Possible Duplicate: If free() knows the length of my array, why cant I ask for it in my own code? Searching around (including here at stackoverflow), I got that malloc() allocates an array and also creates a header to control the array info. In this header, there's also the array size. free() use such information to know how ...

In C, is it possible do free only an array first or last position?

Hi there! I've an array, but I don't need its first (or last) position. So I point a new variable to the rest of the array, but I should free the array first/last position. For instance: p = read_csv_file(); q = p + 1; // I don't need the first CSV file field // Here I'd like to free only the first position of p return q; Otherwise I...

Problem with using malloc in linked lists

I've been working on this program for five months now. Its a real time application of a sensor network. I create several linked lists during the life of the program and Im using malloc for creating a new node in the link. What happens is that the program suddenly stops or goes crazy and restarts. Im using AVR and the microcontroller is A...

Another dynamic memory allocation bug.

I'm trying to allocate memory for a multidimensional array (8 rows, 3 columns). Here's the code for the allocation (I'm sure the error is clear for you) char **ptr = (char **) malloc( sizeof(char) * 8); for (i = 0; i < 3; i++) ptr[i] = (char *) malloc( sizeof(char) * 3); The crash happens when I reference this: ptr[3][0]; ...

Allocating memory to char* C language

Hi, Is it the correct way of allocating memory to a char*. char* sides ="5"; char* tempSides; tempSides = (char*)malloc(strlen(inSides) * sizeof(char)); ...

C malloc assertion help

I am implementing a divide and conquer polynomial algorithm so i can bench it against an opencl implementation, but i can't seem to get malloc to work. When I run the program it allocates a bunch of stuff, checks some things, then sends the size/2 to the algorithm. Then when I hit the malloc line again it spits out this: malloc.c:309...

Cocoa & Cocoa Touch. How do I create an NSData object from a plain ole pointer?

I have malloc'd a whole mess of data in an NSOperation instance. I have a pointer: data = malloc(humungous_amounts_of_god_knows_what); uint8_t* data; How do I package this up as an NSData instance and return it to the main thread? I am assuming that after conversion to an NSData instance I can simply call: free(data); Yes? Also, b...

"Use of uninitialised value" despite of memset

Hi there, I allocate a 2d array and use memset to fill it with zeros. #include<stdio.h> #include<string.h> #include<stdlib.h> void main() { int m=10; int n =10; int **array_2d; array_2d = (int**) malloc(m*sizeof(int*)); if(array_2d==NULL) { printf("\n Could not malloc 2d array \n"); exit(1); } ...

malloc hangs in Linux

I am using SUSE 10 Linux on a machine with 16 G ram and 2 quad core CPUs. There are 8 processes which are doing some work (CPU intensive/network i/o). Out of which 4 have a memory leak (These are test conditions so no problem in having leaks here). Total space is occupied by all processes is around 15.4 G only 200 MB is free in system. ...

Check if a pointer points to allocated memory on the heap.

Ok, I know this question seems to have been asked many times on stackoverflow. but please read Well the answer for any address is "No you can't" but the question here is to know if a pointer points to a piece of memory allocated with malloc/new. Actually I think it could be easily implemented overriding malloc/free and keeping track of ...

Why no memory leak?

The following is designed to take a variable length constant char and print it out in a nice format for logging. I am certain readers will have suggestions on how this can be improved, and I'd welcome it. What puzzles me is that I expected it would be necessary to free() the returned static char each time ToHexString() is called. Inst...

How does free() function collect the info about the no. of bytes to be freed.

Possible Duplicate: C programming : How does free know how much to free? free() is called to deallocate the memory allocated by malloc() function call. From where does the free() find the information about the no. of bytes allocated by the malloc() function. I.e., how do you conform the no. of bytes allocated by the malloc() a...

does free always (portably) frees & reserve memory for the process or returns to the OS

I have read that free() "generally" does not return memory to the OS. Can we portably make use of this feature of free(). For example,is this portable? /* Assume I know i would need memory equivalent to 10000 integers at max during the lifetime of the process */ unsigned int* p = malloc(sizeof(unsigned int) * 10000); if ( p == ...

Segfaults in malloc() and malloc_consolidate()

My application segfaults sometimes and mainly in malloc() and malloc_consolidate() when I look at the backtrace in gdb. I verified that the machine has enough memory available, it didn't even start swapping. I checked ulimits for data segement and max memory size and both are set to 'unlimited'. I also ran the application under valgrind...

malloc.c:3074 + Valgrind output

I am getting the infamous malloc.c:3074 error when running my code (compiles without issue). I compiled using the -g option. I used Valgrind to determine where the memory allocation issue is happening but the results aren't helping a whole lot. Here is the Valgrind output: ==2710== Invalid write of size 8 ==2710== at 0x400FC8: gene...

using malloc for block of structs

I am trying to allocate a block of memory, and store a list of structures without using multiple mallocs for each... this is just a generic example, I don't have the original code I was working with earlier, but this is the general idea, but my problem was that I was getting heap corruption when other parts of my code executed after the ...

how to create a linked list of tables, a table represents a group of elements

what I am trying to do is represented in my other question with code. Basically I need to keep in memory a table of elements (structs), there's no fixed number of elements that can exist, but it is small, but I still can't use an array. And I don't want to use a linked list of elements because I don't want to keep adding and deletin...

Allocate executable ram in c on linux

I want to make a simple just-in-time compiler with c on Linux. How can I allocate memory such that I can write out raw x86 code to it and execute it as any other function? ...

Efficient memory reallocation question

Let's say I have a program(C++, for example) that allocates multiple objects, never bigger than a given size(let's call it MAX_OBJECT_SIZE). I also have a region(I'll call it a "page") on the heap(allocated with, say, malloc(REGION_SIZE), where REGION_SIZE >= MAX_OBJECT_SIZE). I keep reserving space in that page until the filled space e...