malloc

How does Intel TBB's scalable_allocator work ?

What does the tbb::scalable_allocator in Intel Threading Building Blocks actually do under the hood ? It can certainly be effective. I've just used it to take 25% off an apps' execution time (and see an increase in CPU utilization from ~200% to 350% on a 4-core system) by changing a single std::vector<T> to std::vector<T,tbb::scalable_...

git add error : "fatal : malloc, out of memory"

When i attempt to do a git add i get the error "fatal : malloc, out of memory". i imagine the system has ran out of memory obviously but is there a way to get around this. Also i am running windows server 2003 and using msysGit. EDIT: After more searching around i think its a problem with the packing of git, apparently their compressio...

How do I create an array in C++ which is on the heap instead of the stack?

I have a very large array which must be 262144 elements in length (and potentially much larger in future). I have tried allocating the array on the stack like so: #define SIZE 262144 int myArray[SIZE]; However, it appears that when I try and add elements past a certain point, the values are different when I try to access them. I under...

Is memory allocated in nsapi initalization function permanent?

nsapi supplies a MALLOC macro that gets memory from a pool that will be freed for you when the request ends, but it doesn't say what happens if you call this malloc function in an initialization function, when there is no request. Can this malloc (or pblock_created) memory be presumed to be persistent? It doesn't say anything about it in...

Differentiate Between Pointer and Reference at Run Time ANSI C++

How does one differentiate between pointers and references at runtime? For example, if I wanted to free a pointer of a data type without knowing whether it were a pointer or not how would I do so? Is there any method to tell if a variable has been allocated on the stack or through malloc()? void destInt(int* var) { free(var); } int...

Is there a way to find out what the maximum number of bytes available is when using malloc in c?

Or do you just have to do it and check errno and/or the pointer to see if you succeeded? ...

I want to make my own Malloc

I want to make my own malloc/free so I can make a precise copying allocator. Any gurus have any tips and suggestions? I have a few questions for now: Should I just malloc large chunks of memory, and then distribute from that so I don't have to call the system calls? How are copying collectors usually done? I would imagine that this p...

Access violation with malloc() and glDrawPixels()?

Can anyone see what's wrong with this code? SIZE_BG is 6MB as I am trying to draw a large bitmap image (3366x600). I use malloc to prevent my image from overflowing the stack. I get an access violation error on the call to glDrawPixels(). bgPtr seems to point to the correct data as I checked the first few bytes before calling glDrawPix...

Force garbage collection/compaction with malloc().

I have a C++ program that benchmarks various algorithms on input arrays of different length. It looks more or less like this: # (1) for k in range(4..20): # (2) input = generate 2**k random points for variant in variants: benchmark the following call run variant on input array # (3) Is it possible to reset the whole he...

What happens to a malloc'ed block if you don't use it?

Consider the following C code: int main(){ int* c; c = (int*)malloc(sizeof(int)); c = 0xdeadbeef; free(c); return 0; } This will segfault because you are trying to free c, which is not something that has been malloc'ed before. My question is what happens to the block i just malloc'ed? Obviously c is not...

C Malloc to a Pointer Through Function Call Causes Bus Error

Due to my feeble understanding of allocating type memory to pointers, the following causes a bus error on the call to barrier_create ("hi" is never printed). typedef struct barrier barrier_t; typedef struct barrier *barrier_p; barrier_p test_barrier_p; int main(int argc, char *argv[]) { barrier_create(*test_barrier_p); } int barr...

Should I bother detecting OOM (out of memory) errors in my C code?

I've devoted a large number of lines of C code to cleanup-labels/conditionals for failed memory allocation (indicated by the alloc family returning NULL). I was taught that this was a good practice so that, on memory failure, an appropriate error status could be flagged and the caller could potentially perform "graceful memory cleanup" a...

realloc from within a function

I have a struct called ball, a number of balls, an array of balls and a function where I want to add new balls to the array: The struct: typedef struct ball{ BITMAP *image; int x; int y; int vector_x; int vector_y; } ball; The (non working function): void add_balls(int *num_balls, ball **myballs){ num_balls++...

When to use Malloc instead of New

Duplicate of: In what cases do I use malloc vs new? Just re-reading this question: http://stackoverflow.com/questions/807939/what-is-the-difference-between-new-and-malloc-and-calloc-in-c-closed I checked the answers but nobody answered the question: When would I use malloc instead of new? There are a couple of reasons (I can think ...

64 bit large mallocs

What are the reasons a malloc() would fail, especially in 64 bit? My specific problem is trying to malloc a huge 10GB chunk of RAM on a 64 bit system. The machine has 12GB of RAM, and 32 GB of swap. Yes, the malloc is extreme, but why would it be a problem? This is in Windows XP64 with both Intel and MSFT compilers. The malloc sometim...

Why should I use malloc() when "char bigchar[ 1 << 31 - 1 ];" works just fine?

What's the advantage of using malloc (besides the NULL return on failure) over static arrays? The following program will eat up all my ram and start filling swap only if the loops are uncommented. It does not crash. ... #include unsigned int bigint[ 1 << 29 - 1 ]; unsigned char bigchar[ 1 << 31 - 1 ]; int main (int argc, char **argv)...

Int in a simulated memory array of uchar

In C, in an Unix environment (Plan9), I have got an array as memory. uchar mem[32*1024]; I need that array to contain different fields, such as an int (integer) to indicate the size of memory free and avaliable. So, I've tried this: uchar* memp=mem; *memp=(int)250; //An example of size I want to assign. I know the size of an int i...

Malloc of 2 bytes gives issues

I am trying to use a malloc of short, something like typedef union _SOME_STRUCT_ { struct { USHORT u:4; USHORT v:4; USHORT w:4; } x; USHORT word; } SOME_STRUCT, *PSOME_STRUCT; PSOME_STRUCT p = malloc (sizeof (SOME_STRUCT)); if (p) { p->x.u = 0; } free (p); // **** RANDOMLY CRASHING HERE **** I am d...

Malloc thread-safe?

Is malloc re-entrant? ...

Windows malloc replacement (e.g., tcmalloc) and dynamic crt linking

A C++ program that uses several DLLs and QT should be equipped with a malloc replacement (like tcmalloc) for performance problems that can be verified to be caused by Windows malloc. With linux, there is no problem, but with windows, there are several approaches, and I find none of them appealing: 1. Put new malloc in lib and make sure ...