malloc

C - calloc() v. malloc()

Possible Duplicate: c difference between malloc and calloc Please explain the significance of this statement, Another difference between the malloc() and calloc() functions is that the memory allocated by malloc( ) function contains garbage values, while memory allocated by calloc( ) function contains all zeros...

Help me understand why my trivial use of Python's ctypes module is failing

I am trying to understand the Python "ctypes" module. I have put together a trivial example that -- ideally -- wraps the statvfs() function call. The code looks like this: from ctypes import * class struct_statvfs (Structure): _fields_ = [ ('f_bsize', c_ulong), ('f_frsize', c_ulong), ('f_blocks...

How big can a malloc be in C?

I have a malloc in C that is 26901^2*sizeof(double) This got me thinking what the largest value can be here? Also, would I have any problems defining a macro to access this 2D array? #define DN(i,j) ((int)i * ny + (int)j) Because this seems to not be working for me - or I am at least unsure it is. I can't figure out how to make to...

Diffrence between stack memory and heap memory

Possible Duplicate: What and where are the stack and heap Where is heap memory and stack memory stored?I mean where on the harddisk?what are the limits of their size? ...

Is it possible to use a C++ smart pointers together with C's malloc?

Some of my code still uses malloc instead of new. The reason is because I am afraid to use new because it throws exception, rather than returning NULL, which I can easily check for. Wrapping every call to new in a try{}catch(){} also doesn't look that good. Whereas when using malloc I can just do if (!new_mem) { /* handle error */ }. Th...

What are out-of-memory handling strategies in C programming?

One strategy that I though of myself is allocating 5 megabytes of memory (or whatever number you feel necessary) at the program startup. Then when at any point program's malloc() returns NULL, you free the 5 megabytes and call malloc() again, which will succeed and let the program continue running. What do you think about this strategy...

Why does C++ require a cast for malloc() but C doesn't?

I have always been curious about this - why do in C++ I have to cast return value from malloc but not in C? Here is the example in C++ that works: int *int_ptr = (int *)malloc(sizeof(int*)); And here is the example in C++ that doesn't work (no cast): int *int_ptr = malloc(sizeof(int*)); I heard that in C, in fact, casting an outpu...

How is malloc() implemented internally?

Can anyone explain how malloc() works internally? I have sometimes done strace program and I see a lot of sbrk system calls, doing man sbrk talks about it being used in malloc() but not much more. I'd really like to learn how malloc() works. Thanks, Boda Cydo. ...

Pass pointer to guaranteed zeroed memory

I need to zero records of varying sizes in a file. To do this, I'm currently allocating dummy records, memseting them to zero, and passing these to a write function. Is there some region which is guaranteed to always be zeroed (and of a large enough size), that I can instead point to, removing the need for repeated allocation and zeroin...

Sun JVMs memory allocator compared to others

How does Sun JVMs memory allocator (here and here) compares to glibcs malloc (a modified ptmalloc2) or jemalloc? For example, a good article which discusses the benefits of jemalloc in comparison to the old phkmalloc implementation (the earlier default in FreeBSD) can be found in the initial presentation of jemalloc. Common problems of ...

I can't understand a leak -- Possibly Instrument fail ?

I'm trying to fix some leaks in an iPhone app, but I'm still fighting w/ one of them. It appears with Instruments, and I can see something like : ImageIO 128 bytes (Leaked Object -> Malloc) But I'm releasing nearly all used object (at least I think), could it be a false positive or some memory leak for the ImageIO library ? I know th...

Make malloc() return NULL instead of crashing the program?

I am allocating memory in a C program using malloc. It's possible for my program to allocate more memory than the system has room for, at which point the program crashes. For my purposes it would be better if malloc would just return NULL (like it's apparently supposed to), so I can catch the error. Instead what it does is it throws an e...

Is it possible to get the pointer the continous memory fragment in a std::vector<char> in C++?

I moved my code to use std::vector<char> instead of char *mem = malloc(...) but now I am facing a problem that I can only access the vector data through operator [] but not via a pointer. I can't write stuff like: std::vector<char> data; fill_data(data); char *ptr = data; Before I could do this: char *data = malloc(100); fill_data2(...

IPhone watchdog & malloc

Hi! My app is using quite a bit of memory and therefore it's often get killed by the watchdog. In my efforts to reduce memory consumptions (and change some other stuff too) I've rewritten some of the system functions (replaced few CoreText classes to be exact). That actually went pretty well and I've managed to reduce memory consumpt...

p->data = (int*)malloc(sizeof(int)); compare to (int*)p->data = (int*)malloc(sizeof(int));

typedef struct _DListNode { struct _DListNode* prev; struct _DListNode* next; void* data; }DListNode; in gcc : DListNode *p = NULL; p = (DListNode*)malloc(sizeof(DListNode)); p->data = (int*)malloc(sizeof(int)); scanf("%d", (int*)p->data); compile correctly. in gcc : DListNode *p = NULL; p = (DListNode*)malloc(...

Custom malloc for lots of small, fixed size blocks?

I need to allocate, and free lots of fixed size, small (16 byte) blocks of memory, in no fixed order. I could just call malloc and free for each, but that will probably be very inefficient. A better solution probably would be to call malloc and free for larger blocks, and handle the allocation within those blocks itself. The question ...

Scope of malloc used in a function

When a function returns, is the memory allocated via malloc freed? Or can it still be accessed in the main() function using pointers? eg. void function(int *a) { a=(int *)malloc(sizeof(int)); *a=10; } int main() { int *num; function(num); printf("%d",*num); return(0); } Can the integer stored in a be accessed ...

Doubt in malloc. C (Linux)

In the following code,what is the meaning of buf = malloc(n * sizeof(char)); is n*sizeof(char) necessary,if yes.. please elaborate. int n; char* buf; fstat(fd, &fs); n = fs.st_size; buf = malloc(n * sizeof(char)); EDIT1 And What if I write (n*sizeof(double)) ...

Does any operating system implement buffering for malloc() ?

A lot of c/malloc()'s in a for/while/do can consume a lot of time so I am curious if any operating system buffers memory for fast mallocs. I have been pondering if I could speed up malloc's by writing a "greedy" wrapper for malloc. E.g. when I ask for 1MB of memory the initial allocator would allocate 10MB and on the 2nd, 3rd, 4th etc.....

Warning while resizing integer array.

In my program I am trying to resize array using malloc function. #include <stdio.h> int main(void) { int list[5],i; int* ptr = &list; for(i = 0; i < 5; i++) list[i] = i; for(i = 0; i < 5; i++) printf("%d\n", list[i]); printf("----------------------------------------\n"); ptr = malloc(10); ...