malloc

Checking if something was malloced

Given a pointer to some variable.. is there a way to check whether it was statically or dynamically allocated?? ...

Time complexity of memory allocation

What is the time complexity of dynamic memory allocation using new, malloc, etc.? I know very little about how memory allocators are implemented, but I assume the answer is that it depends on the implementation. Therefore, please answer for some of the more common cases/implementations. Edit: I vaguely remember hearing that heap allo...

Can I allocate a specific number of bits in C?

Hello, I am trying to store a large amount of boolean information that is determined at run-time. I was wondering what the best method might be. I have currently been trying to allocate the memory using: pStatus = malloc((<number of data points>/8) + 1); thinking that this will give me enough bits to work with. I could then refer...

Outputting to stderr whenever malloc/free is called

With Linux/GCC/C++, I'd like to record something to stderr whenever malloc/free/new/delete are called. I'm trying to understand a library's memory allocations, and so I'd like to generate this output while I'm running unit tests. I use valgrind for mem leak detection, but I can't find an option to make it just log allocations. Any i...

[64-Bit] Why does malloc overwrite RSP and RSP+8?

You can read about the 64-bit calling convention here. x64 functions are supposed to clean up after themselves however, when I call malloc from .asm, it overwrites the value at RSP and RSP+8. This seems very wrong. Any suggestions? public TestMalloc extern malloc : near .CODE align 8 TestMalloc proc mov rcx, 100h 000000018...

char x[256] vs. char* = malloc(256*sizeof(char));

Someone here recently pointed out to me in a piece of code of mine I am using char* name = malloc(256*sizeof(char)); // more code free(name); I was under the impression that this way of setting up an array was identical to using char name[256]; and that both ways would require the use of free(). Am I wrong and if so could someon...

How to free a C struct with Objective-C?

Given a struct, e.g. typedef struct { int value; } TestStruct; Why does the following code (in the context of an Objective-C class running on the IPhone) throw a "non-aligned pointer being freed" exception? TestStruct ts = {33}; free(&ts); N.B. My uber goal is to use a C library with many vector-math functions, hence the need to ...

Why does malloc allocate a different number of bytes than requested?

I have this piece of code #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> int main(){ void *a, *b; a=malloc(16); b=malloc(16); printf("\n block size (for a): %p-%p : %li",b,a,b-a); a=malloc(1024); b=malloc(1024); printf("\n block size (for a): %p-%p : %li",b,a,b-a); } This shouldn't display the t...

Variable Sized Arrays in C

On the discussion of dynamic memory here: "Intro to C Pointers and Dynamic Memory" The author states: A memory block like this can effectively be used as a more flexible array. This approach is actually much more common in real-world C programs. It's also more predictable and flexible than a "variable size array" The type of memor...

Memory Allocation/Deallocation Bottleneck?

How much of a bottleneck is memory allocation/deallocation in typical real-world programs? Answers from any type of program where performance typically matters are welcome. Are decent implementations of malloc/free/garbage collection fast enough that it's only a bottleneck in a few corner cases, or would most performance-critical softw...

Have you ever obtained a significant speedup by using boost::pool ?

I've played with boost::pool a few times in places where it seemed to me I was seriously hammering the heap with a lot of object "churn". Generally I've used boost::object_pool, or boost::pool_alloc as an STL template parameter. However the result is invariably that performance is virtually unchanged, or significantly worsened. I'm cu...

NSTreeController - malloc double free error

I'm using an NSTreecontroller in conjunction with an NSOutlineView in my program, and my program is constantly spitting out: malloc: *** error for object 0x1d70d0: double free. These messages go away when I remove the bindings in IB. Any idea why this is happening? Note: This is behavior is not crashing my program, but I suspect that it...

What is the best way to free memory after returning from an error?

Suppose I have a function that allocates memory for the caller: int func(void **mem1, void **mem2) { *mem1 = malloc(SIZE); if (!*mem1) return 1; *mem2 = malloc(SIZE); if (!*mem2) { /* ... */ return 1; } return 0; } I'd like to hear your feedback on the best way to free() the allocated memory i...

Adding an int member to a C struct causes a segfault

I'm still learning C, and have started using it to generate images. I can't figure out why one of my programs is segfaulting. Here's the source code, cut down to 40 lines: #include <stdio.h> #include <stdlib.h> struct color { unsigned char r, g, b; }; struct image { int w, h/*, o*/; struct color **data; }; int ma...

Does C allocate memory automatically for me?

I have been writing C for only a scant few weeks and have not taken the time to worry myself too much about malloc(). Recently, though, a program of mine returned a string of happy faces instead of the true/false values I had expected to it. If I create a struct like this: typedef struct Cell { struct Cell* subcells; } and then lat...

Do I cast the result of malloc?

In this question, someone suggested in a comment that I should not cast the results of malloc. I.e. int *sieve = malloc(sizeof(int)*length); rather than int *sieve = (int *)malloc(sizeof(int)*length); Why would this be the case? ...

Does malloc() allocate a contiguous block of memory?

I have a piece of code written by a very old school programmer :-) . it goes something like this typedef struct ts_request { ts_request_buffer_header_def header; char package[1]; } ts_request_def; ts_request_buffer_def* request_buffer = malloc(sizeof(ts_request_def) + (2 * 1024 * 1024)); the programm...

C memory allocation question.

So I have a couple of functions that work with a string type I have created. One of them creates a dynamically allocated sting. The other one takes said string, and extends it. And the last one frees the string. Note: The function names are changed, but all are custom-defined by me. string new = make("Hello, "); adds(new, "everyone"); f...

What REALLY happens when you don't free after malloc?

This has been something that has bothered me for ages now. We are all taught in school (at least, I was) that you MUST free every pointer that is allocated. I'm a bit curious, though, about the real cost of not freeing memory. In some obvious cases, like when malloc is called inside a loop or part of a thread execution, it's very impo...

Howto read chunk of memory as char in c++

Hello I have a chunk of memory (allocated with malloc()) that contains bits (bit literal), I'd like to read it as an array of char, or, better, I'd like to printout the ASCII value of 8 consecutively bits of the memory. I have allocated he memory as char *, but I've not been able to take characters out in a better way than evaluating ea...