malloc

GUI for a GNU Debugger

Hi, am pretty excited with the GNU Debugger and a GUI called Insight as it has saved me A LOT OF time. Thus I am posting this question/answer for other newbies out there like me having problems with their C code looking for a visual way to see what's going on. I am working on Linux Mint (Ubuntu) btw. ...

is there a flag "M_FAST" in FreeBSD kernel for Malloc Call ?

if you know there is one, can you let me know what its for ? if not please say so : ) thanks. Signature : void * malloc(unsigned long size, struct malloc_type type, int flags); for example. other flags are... M_ZERO Causes the allocated memory to be set to all zeros. M_WAITOK Indicates that it is OK to wait for...

malloc results in segmentation fault after mprotect

I'm getting a segmentation fault the first time I call malloc() after I protect a memory region with mprotect(). This is a code sniplet that does the memory allocation the the protection: #define PAGESIZE 4096 void* paalloc(int size){ // Allocates and aligns memory int type_size = sizeof(double); void* p; p = ...

Does exiting from a pthread release malloced memory ?

Let's say I pthread_create and then pthread_detach it. Now, from within the thread function, I malloc some block. When the thread exits, will the malloc'ed memory be freed automatically? (been googling for an answer to no avail...) ...

Is there a way to determine if free() would fail?

Is there a way to determine if free() would fail if ever called on a certain memory block pointer? I have the following situation: a thread having access to a shared resource fails whilst it may have been in the state of freeing the said resource. Now I need to devise a safe way to clean-up this shared resource. Of course I have assig...

Custom malloc() implementation header design

I am trying to write a custom allocator for debugging purposes (as an exercise) in C, where I will be using a single linked list to hold together the free list of memory using the First Fit Algorithm. I've shown below the structure I would like to create in an "Empty Memory Node". How do I write the header block (a union to be specific)...

using malloc for char inputs in C

For an assignment, I have to declare a struct as follows: struct Food { char *name; int weight, calories; } lunch[5] = { { "apple", 4, 100 }, { "salad", 2, 80 } }; In my main, I am trying to ask the user for the rest of the inputs to fill the struct to print them out. I figure I would try to use ma...

Why does my program stop crashing if I call malloc instead of GetMem?

I am calling a C DLL from a Delphi 2009 application and I keep getting errors when memory allocated by GetMem or AllocMem is passed to the DLL. The only way around I could avoid these errors was by using malloc from msvcrt.dll. What is malloc doing that the built-in memory routines aren't, and how can I get the built-in ones to work? I r...

C: Correctly freeing memory of a multi-dimensional array

Say you have the following ANSI C code that initializes a multi-dimensional array : int main() { int i, m = 5, n = 20; int **a = malloc(m * sizeof(int *)); //Initialize the arrays for (i = 0; i < m; i++) { a[i]=malloc(n * sizeof(int)); } //...do something with arrays //How do I fre...

Read a file into dynamic memory array using malloc and POSIX file operations

Possible Duplicate: reading a text file into an array in c Hi, I'm trying to read a file into a dynamic array. Firstly I open the file using open() so I get the file descriptor But then I don't know how can I allocate the memory using malloc to a dynamic array in order to do some data modification in the file from memory. Than...

malloc and free

I am new to C I am trying to get comfortable with malloc + free. I have coded following test but for some reason the memory isn't freed completely (top still indicates about 150MB of memory allocated to process). Why is that? #include <stdio.h> #include <malloc.h> typedef struct { char *inner; } structure; int main() { int i; ...

malloc vs mmap in C

Hi, I built two programs, one using malloc and other one using mmap. The execution time using mmap is much less than using malloc. I know for example that when you're using mmap you avoid read/writes calls to the system. And the memory access are less. But are there any other reasons for the advantages when using mmap over malloc? T...

Opening a file with path in malloc

I'm trying to open a file with fopen, but I don't want a static location so I am getting the string in from the user when he/she runs the program. However if a user does not enter one a default file is specified. Can I just put the malloc var in to the fopen path parameter? char *file_path_mem = malloc(sizeof(char)); if (file_path_mem ...

Are there compiler flags to get malloc to return pointers above the 4G limit for 64bit testing (various platforms)?

I need to test code ported from 32bit to 64bit where pointers are cast around as integer handles, and I have to make sure that the correct sized types are used on 64 bit platforms. Are there any flags for various compilers, or even flags at runtime which will ensure that malloc returns pointer values greater than the 32bit limit? Platf...

Core Data Malloc Errors

Hi there, I've noticed I'm getting a few errors at random points in my app. I've had 2 errors, "double free" and "incorrect checksum for freed object". Heres the stack trace of a "double free" error. Can anyone provide any insight? It's happening on a call in my code that just sets an attribute to an NSNumber so I can't understand why i...

When to use malloc for char pointers

I'm specifically focused on when to use malloc on char pointers char *ptr; ptr = "something"; ...code... ...code... ptr = "something else"; Would a malloc be in order for something as trivial as this? If yes, why? If not, then when is it necessary for char pointers? ...

Malloc a pointer to a pointer to a structure array by reference

The code below compiles, but immediately crashes for reasons obvious to others, but not to me. I can't seem to get it right, can anyone tell me how to fix this. *array_ref[2] = array[0]; *array_ref[3] = array[1]; It crashes on that part everytime. typedef struct test { char *name; char *last_name; } person; int setName(per...

Is it a better practice to typecast the pointer returned by malloc?

For the C code below, compare the defintions of the int pointers a and b; #include <stdio.h> #include <stdlib.h> int main() { int *a=malloc(sizeof(int)); int *b=(int *)malloc(sizeof(int)); return(0); } Is it better in any way to typecast the pointer of type void, returned by the malloc function? Or is it auto-typecasted while...

SIGSEGV, (seemingly) caused by printf

First and foremost, apologies for any cross-posting. Hope I'm not repeating an issue here, but I was unable to find this elsewhere (via Google and Stack Overflow). Here's the gist of the error. If I call printf, sprintf or fprintf anywhere within my code, to display a float, I get a SIGSEGV (EXC_BAD_ACCESS) error. Let me give an example...

Effect of filling memory allocated by malloc()

Although I was able to correct my program and make it run properly, the reason why it wasn't working left me really curious. I made a string with malloc and initialized it...then I did several strcat with it...and then I declares a file pointer...after that and if my string had more than approx. 26 chars the rest would be garbage...but ...