Questions tagged [malloc]
The malloc tag has no usage guidance.
20
questions
11
votes
4answers
812 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 ...
2
votes
2answers
275 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) {
...
0
votes
1answer
350 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 ...
3
votes
2answers
183 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 ...
2
votes
1answer
833 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,...
3
votes
3answers
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 ...
0
votes
2answers
136 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 ...
1
vote
1answer
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 ...
8
votes
6answers
19k 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 ...
2
votes
1answer
1k 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 ...
-1
votes
1answer
196 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! ...
12
votes
6answers
10k 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 ...
2
votes
2answers
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 ...
-3
votes
1answer
76 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.
...
2
votes
3answers
311 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
1
vote
2answers
5k 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 ...
5
votes
3answers
4k 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-...
2
votes
2answers
3k 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?
2
votes
1answer
4k 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 ...
5
votes
2answers
445 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,...