malloc

C 2D array Memory allocation

Greetings all, Is there any issue in the following logic for allocating 2D array: unsigned char ** Malloc2D_uchr(int ht, int wt, unsigned char initv) { int h, w; unsigned char **x; x = (unsigned char **) malloc(sizeof(void *) * ht); DEBUG_PRINT_MEMLOC_EXIT(x,"malloc failed (%s,%i)\n",sizeof(void *)*ht); x[0] = (uns...

Why doesn't strncpy work with a string allocated with malloc?

char* temp; temp = (char*) malloc (strlen(window->entry.value)+1); //strncpy( temp, window->entry.value, sizeof(temp) ); DOESN"T WORK memcpy (temp, window->entry.value, strlen(window->entry.value) + 1); //WORKS (where window->entry.value is a string.) Thanks. ...

Why does the malloc fail?

A pointer expected error occurs when the compiler it gets to the assignment at the at end of the function. Why? (Removed casts and index notation from code; they were there for "debugging" buy apparently cluttered my question.) int createArraySimple(int initialResetCount, int ***array) { int sourceTermIndex, driveCurrentIndex, preE...

How to deallocate memory in prefix tree? (ANSI C)

I tried to deallocate memory in dict_free() function, but it doesn't work and I don't no why. Am I missing something? Can't figure out, what's wrong. Edit: If I call free() in dict_free() I expect to see that free'd pointer points to NULL, but that's not happening. Here is my code: #include <stdio.h> #include <stdlib.h> #include <stri...

sprintf() with automatic memory allocation?

Hello! I'm searching for a sprintf()-like implementation of a function that automatically allocates required memory. So I want to say char* my_str = dynamic_sprintf( "Hello %s, this is a %.*s nice %05d string", a, b, c, d ); and my_str retrieves the adress of an allocated memory that holds the result of this sprintf(). In another fo...

C/C++ - overriding default functions

Hello! I have the following question: Does Microsoft Visual Studio (I'm using 2008 SP1) offer any way to override standart C functions such as malloc, memcpy? Suppose I have some externally built library, which contains malloc.obj and memcpy.obj. Library is called library.lib. How should I build my project so that the compiler uses m...

size_t or long for the size of a string containing a file?

Suppose I want to read an entire file in memory. I'd open it in binary mode, use fseek to get to the end of the file, then do ftell to get its size. Then I would allocate a string with the same size as the file, and just read it in, right? The problem is that ftell returns a long int, and malloc should receive a size_t argument. Now, si...

Getting a very annoying segmentation fault... mallocing 2d array in C.

Hi, I'm having a little trouble with this. First of all, this is for a simple graphics library that I was writing. What I'm doing is making it possible to declare multiple "virtual screens", with dimensions of the programmer's choice. For this, I need to dynamically allocate enough memory based on the number of pixels the screen will c...

aligned malloc() in GCC?

Hi. Is there any standardized function in GCC or glibc to allocate memory block at aligned pointer? Like _align_malloc() in MSVC? ...

Xcode leaks tool not showing object for a single massive leak made by realloc I'm guessing.

Hello. I have some code which has a memory leak. It's in development still so this is expected to happen but I can't find where the memory leak occurs. I try using the leaks tool and it shows an allocation like this (The 1.17MB steadily increases through the program): Category: Malloc 1.17MB Live/Overall Bytes: 1.17MB Obviously for on...

So much memory being malloc'd that I get "Killed" when running my program deep enough..

I have a program that goes n^2 layers deep of recursion and mallocs a bunch of memory to concatenate char*s together. With a large enough n, the process just gets killed by the server (since it is consuming too much memory). How can I release this memory and still have my data? It's mallocs look like test = (char *)malloc(sizeof(cha...

Linked Lists in C without malloc

#include <stdio.h> typedef struct node { int i; struct node *next; }node; node getnode(int a) { struct node n; n.i=a; n.next=NULL; return n; } main() { int i; node newtemp,root,temp; scanf("%d",&i); root=getnode(i); temp=root; while(i--) { newtemp=getnod...

malloc error in c++

Hi when I was trying to execute my program(c++) i was getting the following error: a.out: malloc.c:3096: 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 (struc...

How can I limit memory acquired with `malloc()` without also limiting stack?

I'm trying to keep student code from running wild with allocations and dragging my test machine to a halt. I've tried setrlimit(RLIMIT_DATA, r); where r is a struct holding the limits. But unfortunately although this limit stops brk and sbrk from allocating, the C library just fails over to mmap and keeps right on allocating. I've ...

After malloc, how to obtain more memory that is still continuous?

This is an interview question. If you use malloc to get a piece of memory, such as: char *p = (char *) malloc (100); Now you find you need more memory, say 130. How to obtain the memory such that the new piece of memory is still continuous ...

How to find how much space is allocated by a call to malloc()?

I'm trying to write a size function like this: size(void *p,int size); Which would return the size of an array which is pointed to by p. For example: Int *a = malloc((sizeof(int)*100)); size(a,sizeof(int)); // this should return 100 I think this is possible because if I recall, malloc keeps track of the space allocated in some head...

When is malloc necessary in C?

I think all malloc(sizeof(structure)) can be replaced this way: char[sizeof(structure)] Then when is malloc necessary? ...

Malloc, free and segmentation fault

Hi, I don't understand why, in this code, the call to "free" cause a segmentation fault: #include <stdio.h> #include <string.h> #include <stdlib.h> char *char_arr_allocator(int length); int main(int argc, char* argv[0]){ char* stringa = NULL; stringa = char_arr_allocator(100); printf("stringa address: %p\n", stringa);...

Dynamically Growing an Array in C++

I have an array of pointers of CName objects. I have the following constructor which initializes my array to size one. Then when I add an object I grow the array by 1 and add the new object. It compiles fine, however when I try to print them I just get segmentation fault error. Can you look and see if I'm doing anything wrong? //cons...

malloc error C++

Possible Duplicate: malloc.c:3074 error? I am getting this strange error on execution of my C++ code: malloc.c:3096: 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)((((__...