Questions tagged [malloc]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
12 votes
4 answers
3k views

Why would you ever use `malloc(0)`?

While reading an answer here, I saw this code: char ** v = malloc(0); while ((r = strtok(s, " ")) != NULL) { char ** vv = realloc(v, (n+1)*sizeof(*vv)); The thing that bugged me was the ...
klutt's user avatar
  • 1,428
12 votes
6 answers
12k views

Functions returning strings, good style?

In my C programs I often need a way to make a string representation of my ADTs. Even if I don't need to print the string to screen in any way, it is neat to have such method for debugging. So this ...
Øystein Schønning-Johansen's user avatar
11 votes
6 answers
30k views

How efficient is malloc and how do implementations differ?

If I use malloc, does malloc always use the same algorithm regardless of what it is allocating or does it look at the data and select an appriopriate algorithm? Can we make malloc faster or smarter ...
Niklas Rosencrantz's user avatar
6 votes
3 answers
6k views

How many `malloc` calls is too many? If any?

I'm implementing a system in C, implemented partially as a library. The library does most of the memory management itself, with the application layer just having to call *_destroy functions on the top-...
Aatch's user avatar
  • 181
5 votes
2 answers
598 views

Heaps: Why is there a tradeoff between amount of space occupied (fragmentation), and speed at which operations are carried out?

Apparently, the two major judging criteria of the effectiveness of heaps are (1) how much we can minimize the amount of space it takes up and (2) how fast operations on the heap can be carried out, eg,...
Dark Templar's user avatar
  • 6,303
3 votes
3 answers
1k views

Is O(log n) for memory management considered slow?

I am talking about single thread general purpose memory allocation/deallocation from a global 'heap' as e.g. every C programmer knows in the form of malloc()/free(). I can't recite the actual title ...
Vroomfondel's user avatar
3 votes
2 answers
5k views

How are new[] and malloc implemented in Windows? [closed]

So when you call malloc or new [] from your C/C++ application, how does the CRT translate it into Windows API calls?
CS01's user avatar
  • 149
3 votes
2 answers
2k views

Why don't we see (more) widespread adoption of lock-free dynamic memory allocators? [closed]

In a multithreaded programming environment, lock contention on the heap is often enough a performance bottleneck. Theoretically at least, the cream-of-the-crop solution for this problem is to have ...
the gods from engineering's user avatar
3 votes
2 answers
377 views

malloc on different platforms

I am testing a red-black tree implementation (repository) and I find that with Windows 10 and gcc, malloc starts returning NULL after inserting approx. 50 million nodes but on Linux it works at least ...
Niklas Rosencrantz's user avatar
2 votes
3 answers
434 views

Use of malloc in C [closed]

Is it necessary to call free function every time we use malloc in C. I am asking this because I have seen many times that it is not called . Thank you
Supreet's user avatar
  • 23
2 votes
2 answers
650 views

malloc in caller, free in callee

Is it good practice to malloc in the caller function, and then free in the callee function? Example: int main() { int *ptr = malloc(100); foo(ptr); return 0; } void foo(int *ptr) { ...
Kyoma's user avatar
  • 139
2 votes
1 answer
2k views

When to malloc and free?

Valgrind does not report a memory leak during my actual usage, only during my scripted test that I scripted with a shell script to test my own shell. I found that I didn't have to use malloc every ...
Niklas Rosencrantz's user avatar
2 votes
1 answer
3k views

Algorithm for efficiently allocating memory

Given the following constraints: No multithreading. No care about hardware cache utilization. Wondering what a reasonably optimal memory allocation scheme would look like. From my limited knowledge,...
Lance's user avatar
  • 2,605
2 votes
1 answer
5k views

At a higher level description, how is DLMALLOC supposed work?

There doesn't seem to be that many good descriptions that go into the specifics about how dlmalloc works. The sources I have come across so far mention dlmalloc, but then only goes on to explain what ...
Dark Templar's user avatar
  • 6,303
1 vote
2 answers
7k views

Which is better: for valid buffer length?

This is a basic question. But, I think understanding this would be helpful to play with buffers in 'C'. Thanks. Scenario Just a sample snippet char *test_buff = (char *) malloc(512); //allocate 512 ...
smaven's user avatar
  • 11
1 vote
1 answer
731 views

Is memory allocation hardware handled or OS?

I am currently working on a bare metal project with an arm processor using GCC arm compiler. If I call malloc, is the allocated memory tracked in the MMU? For some reason I had assumed an OS typically ...
Matthew Huber's user avatar
1 vote
1 answer
2k views

Need info on malloc trace

When I try the below code I am not clearly able to analyze malloc api internal calls.What I am not clear is about the system call mmap is called only once for 2 or more malloc calls.If I am assigning ...
Harish's user avatar
  • 119
0 votes
2 answers
299 views

how efficient is sorting and searching in malloc/free?

I'm studying malloc and free and writing my own malloc and free. But when I read about the different algorithms (best fit, first fit, worst fit) then it doesn't say which algorithm is used for ...
Niklas Rosencrantz's user avatar
-1 votes
1 answer
282 views

Should I add 1 to my unknown string size when I create a char array to hold it via malloc? [closed]

When I have a string of unknown size and I create an char array to hold this string. I do something like this: #define LINE_END "\r\n" int line_end_size = strlen(LINE_END); char line[]="Hello! ...
jc__'s user avatar
  • 103
-3 votes
1 answer
84 views

How can this allocation of bi-dimensional arrays work? [closed]

I was seeing this post on StackOverflow and saw a new way (at least for me) to define a two dimensional array of 5x5, it works well, but I feel I don't understand what is going on in the background. ...
OiciTrap's user avatar
  • 729