What happens when malloc doesn't find the memory? [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
What happens when malloc returns the NULL value to the pointer?
When this if statement gets executed
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
And we come out of the program. Then after that what makes malloc to find memory for the same code?

What happens when malloc returns the NULL value to the pointer?
According to the manual page for malloc, it can return NULL in two cases:
The requested size was 0.
The allocator isn't able to find enough free memory to satisfy the request.
Assuming #2 happens, there can be different reasons behind it:
Your system really ran out of memory. This could either be caused by your program or by some other program(s) running on the same machine.
Your process exceeded the resource limits set for its memory (see RLIMIT_AS and RLIMIT_DATA).
Your program is being traced/debugged by something/someone that is controlling allocations and returning NULL for testing purposes.
Then after that what makes malloc to find memory for the same code?
Using the same numbering as above, malloc will find memory on the next run of your program if:
The program is restarted after the system is able to release the needed amount of memory.
The program is restarted under different rlimit memory limits.
The program is restarted outside the testing/debugging context.

Related

Importance of using function [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
It has been on my mind for a while now.
How is space managed when we use a function.
Particularly this:
main()
{
printf("Helloworld");
}
and this:
void fun()
{
printf("Helloworld");
}
main()
{
fun();
}
So in terms of memory consumption are both of these the same? Or one of them is consuming lesser memory.
I understand that in a large program functions help us not repeating the same codes again and again AND also it releases its space every time it ends, But I want to know what happens in a small program where memory consumption is insignificantly small where the memory release of function after it ends has no significant effect.
What are the pro's and con's of function in this case
The C standard doesn't tell anything about memory consumption when using a function. An implementation (i.e. a specific compiler on a specific computer system) is free to do function calls the way it wants. It's even allowed to suppress function calls and put the functions code directly where the call was (called: inline). So there is no answer that will cover all systems in all situations.
Most systems uses a stack for handling function calls. A stack is a pre-allocated memory block that is assigned to the program at start up. The running program keeps track of the memory used within that block using a stack pointer. When a function is called, the stack pointer is changed according to the memory requirement for the function. When the function returns, the stack pointer is changed back to the original value. This allows for fast allocation and deallocation of variables local to the function and also any overhead memory used for the call itself (e.g. for storing a return address, cpu registers, etc.).
Since the stack is a pre-allocated fixed memory block, there is really no extra memory consumption involved in a function call. It's only a matter of using the already allocated memory.
However, if you do many nested function calls, you may run out of stack memory but that's another issue.

Why is there not a function in C that returns size of memory allocated pointed to by a pointer? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
After searching around for a bit on how deallocation (via free) after allocation (via something like malloc) works, I'm left puzzled.
Reading around, sources say that when memory is allocated, it actually has one word more allocated which stores the size of the allocation. This is what free uses to deallocate the memory. But with this information arises another question which I can not find the answer to anywhere: If the size of memory allocated is stored somewhere, why is there not a function or method in C that can give us the size of memory allocated via something like malloc?
The C standard only specifies the functions needed to allocate and free memory. This means that how the heap is managed is a implementation detail of the standard library on your system. How Linux does it is likely very different from Microsoft's implementation.
That being said, sometimes these implementations expose additional functions that expose the internal state of the heap. For example, Linux has a function called malloc_usable_size which is passed a pointer to allocated memory and returns the number of bytes that may be written at that address.
Still, it's best not to depend on system specific functions like that. Your program should have some way of keeping track of how much space a malloc'ed block of memory points to.

Finding unused memory in process memory [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm looking for a reliable way to find unused memory in a C program's process since I need to "inject" some data into somewhere without it corrupting anything.
Whenever I find an area with only zeros in it, that's a good sign. However, no guarantees: It can still crash. All the non-zero memory is most likely being used for sure so it cannot be overwritten reliably (most memory has some kind of data in it).
I understand that you can't really know (without having the application's source code for instance) but are there any heuristics that make sense such as choosing certain segments or memory looking a certain way? Since the data can be 200KB this is rather large and finding an appropriate address range can be difficult/tedious.
Allocating memory via OS functions doesn't work in this context.
Without deep knowledge of a remote process you cannot know that any memory that is actually allocated to that process is 'unused'.
Just finding writable memory (regardless of current contents) is asking to crash the process or worse.
Asking the OS to allocate some more memory in the other process is the way to go, that way you know the memory is not used by the process and the process won't receive that address through an allocation of its own.

Allocating and freeing memory in C in a good manner [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
This is maybe a more general question. I have a pointer in my code to store some data. The size of the pointer is determined only while the program is executed and needs to be allocated dynamically.
So I'm using
calloc()
to allocate memory and set it to zero. After the run of the program I'm using
free()
to free it.
Is this usage of memory in a good manner? Or is there something more "nice" one could do?
For dynamic memory allocation, the steps you had mentioned is correct, as #Stargateur pointed use malloc(), if you don't need to initialize the memory allocated.
Also take care to free the allocated memory, on all possible exit condition of the program.

what happens to the memory allocated in functions? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I wonder what happens to the memory that is used in functions. I am writing a multi-threaded program and I wonder what happens if I just call a functions and its memory after it returns.
"Automatic" storage -- variables you declare directly rather than explicitly allocating from the heap -- is obtained from the stack, and essentially goes away when the function exits.
Anything you explicitly malloc() MUST eventually be explicitly free()d, once and only once. It's your responsibility to structure your code so that happens correctly. If you don't intend to use it after the function exits, you should free it before the function exits. If it's part of a larger data structure, or being returned to the caller, you need to design your program to be aware of this and clean up after itself when that block of memory is no longer needed.
If you allocate, you must free -- or must document clearly whose responsibility it is to free the memory when they're done with it.
(Note that this is very different from Java and other "garbage-collected" languages, where memory is automatically recovered when nobody is actively using it.)

Resources