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.
Related
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.
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 3 years ago.
Improve this question
I was asked a question during an C Language interview. the question is:
Can I change the address?
struct node * root;
root=(int*)malloc(sizeof(int));
printf("%d",root) =10128000 // new address: root=101590000
None of the C standard library's allocation functions allow you to specify the address where you want the allocated space to be. It would not make sense to do so, because it's very unlikely for the programmer to know or care what specific address they get, unless something known to them is already there, in which case it's not an available address.
You can, however, allocate a large block (e.g. via malloc), and then manually assign chunks of that block however you like. That allows you to choose your own addresses relative to the base of the allocated block. For example:
my_node *node_base = malloc(AS_MUCH_MEMORY_AS_I_NEED);
// ...
// malloc analog:
size_t an_index = choose_a_node_index_by_some_criteria();
my_node *node = node_base + an_index;
// free analog:
mark_index_available_again(an_index);
Of course, the devil is in the details, and those are both specific to your needs and more complex than I'm prepared to go into here. Overall, this is not something that a self-declared beginner really ought to be trying to do.
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 4 years ago.
Improve this question
I was in an interview recently where I was given a piece of paper with a few function signatures and asked to fill in the code, I was also instructed not to "allocate memory".
The question was relatively simple (Smallest value in a list) so I solved it recursively which the interviewers seemed unimpressed by, they seemed to suggest that I could have declared variables on the stack but i was nervous and regrettably didn't press the interviewer on it.
What does it mean to "allocate" memory in C?
Allocate memory means you keep for your program an amount of memory situated in the HEAP section. On the contrary, when you don't allocate memory, new variables are stored in the STACK section.
See What and where are the stack and heap?
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How is memory allocated using malloc() ? Who allocates the memory OS or the compiler? Once the memory is freed using free() can it be used by other processes ?
In an OS there are 4 memory regions Heap,Stack,Text and Data. When you use malloc the OS provides the memory from the heap region. Compiler isn t responsible for allocating this memory. When you use free the memory block is returned back to the heap.
Usually, the heap memory is directly supplied by a runtime sub-allocator underneath whatever the OS provides. The sub-allocator is process-specific and does not require a kernel call. If the heap needs more, it has to resort to a syscall to get another chunk from the OS.
It's implementation-specific whether the sub-allocator ever releases chunks back to the OS.
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
I am trying to deallocate a *pointer if it already had something allocated at the end of a program. Is this the proper way to do it?
The pointer is in another function with argument (char **mena).
It seems to work, because when I try to print the values after, the program crashes. I just want to make sure.
int n(FILE *file, char ***mena,int already){
//var declaration
if (already) {
for (i=0;i<already;i++)
free((*mena)[i]);
free(*mena);
*mena=NULL;
}
// function continues.....
That is hard to tell, because it depends on the way these pointers were allocated.
But, as you have pointers 3 levels deep, I suppose it is not enough to free them.
You should have a look at tools like valgrind which helps you to identify memory leaks.
To explain a bit, char *** means that you have a pointer to a pointer to a pointer to a char. That means that the pointer points to a region of memory which can be allocated statically or dynamically. The same holds for all other levels.
You have to free() exactly those levels which were created dynamically, i. e. via malloc(), calloc(), realloc() or strdup().