Questions tagged [malloc]

20 questions
12
votes
4 answers

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 call to malloc with an argument of zero. According to the…
klutt
  • 1,428
  • 1
  • 10
  • 25
12
votes
6 answers

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 kind of function often comes up. char *…
11
votes
6 answers

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 by choosing a more efficient algorithm? In my…
Niklas Rosencrantz
  • 8,008
  • 17
  • 56
  • 95
6
votes
3 answers

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-level objects. While checking for memory leaks,…
Aatch
  • 181
  • 1
  • 5
5
votes
2 answers

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, malloc and free But I was wondering how these…
Dark Templar
  • 6,223
  • 16
  • 46
  • 46
3
votes
2 answers

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 up to 100 million nodes. What conclusion can I…
Niklas Rosencrantz
  • 8,008
  • 17
  • 56
  • 95
3
votes
3 answers

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 of a paper about a…
Vroomfondel
  • 367
  • 2
  • 10
3
votes
2 answers

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

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 scalable/parallel[izing] allocator be entirely…
Fizz
  • 588
  • 6
  • 13
3
votes
2 answers

How are new[] and malloc implemented in Windows?

So when you call malloc or new [] from your C/C++ application, how does the CRT translate it into Windows API calls?
CS01
  • 149
  • 1
  • 4
2
votes
2 answers

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) { free(ptr); }
Kyoma
  • 139
  • 2
2
votes
1 answer

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, one implementation is Free Lists, but it says…
Lance
  • 2,537
  • 15
  • 34
2
votes
1 answer

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 time I did. For example, strdup could do it for me.…
Niklas Rosencrantz
  • 8,008
  • 17
  • 56
  • 95
2
votes
3 answers

Use of malloc in C

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
  • 23
  • 3
2
votes
1 answer

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 malloc() and free() are, rather than describing…
Dark Templar
  • 6,223
  • 16
  • 46
  • 46
1
vote
1 answer

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 more then 4069 bytes also it is calling only one…
Harish
  • 119
  • 4
1
2