Skip to main content

Questions tagged [malloc]

12 votes
4 answers
5k views

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,448
2 votes
2 answers
821 views

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
1 vote
1 answer
834 views

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
3 votes
2 answers
441 views

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
1 answer
3k views

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 Pollard's user avatar
3 votes
3 answers
2k views

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
0 votes
2 answers
369 views

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 vote
1 answer
2k views

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
10 votes
6 answers
32k views

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
2 votes
1 answer
2k views

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
-1 votes
1 answer
306 views

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
12 votes
6 answers
13k views

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
3 votes
2 answers
2k views

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 ...
got trolled too much this week's user avatar
-3 votes
1 answer
87 views

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
2 votes
3 answers
468 views

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

15 30 50 per page