What does "memory allocation" actually mean in C [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 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?

Related

How to allocate a desired address in Ram with malloc or other function? [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 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.

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.

Are there differences between pointers to object stored on stack and heap? [closed]

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 6 years ago.
Improve this question
Are there differences between pointers to object stored on stack and heap ? Are there internal representation in common C/C++ compilers (or JVM/LLVM) differs ?
this is very interesting question somehow related to main : memory location patterns on stack and heap
A pointer is a pointer. No matter where it points to.
I mean: you can assign to the same pointer both the address of a region on the stack and on the heap, don't you? So there cannot be any intrinsic difference between a pointer pointing here or there.
The difference being ones on the stack you do not need to free

Next Fit Memory Allocation Issue [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
I am trying to write an algorithm to simulate next-fit memory allocation and I have a specific doubt that I have not been able to find the answer too.
My situation:
A process has been added to the memory at the memories 1/2 way point. Now another process of size 50 wants to be added to the memory. All the holes after the spot we are currently at are all less than size 50. I know that the algorithm will check every hole after the 1/2 way point to see if there is enough space for this new process of size 50. Now my question is, after it has reached the end of memory, will it go back to the start of memory to see there is a big enough hole BEFORE the 1/2 way point where we initially started off.
Yes, that's why it's sometimes also called "rotating-first-fit".
Otherwise you'd be pretty soon running "out of memory" anyway ;-)

Free a Unicode struct [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
I have a pointer to an wide char array, which I want to free.
What is the easiest way to do this?
That depends on how the memory was allocated.
If malloc() was used, pass the address of the allocated memory to free(). If it came from a third-party library, see that library's documentation. If the struct is actually a local or global variable, don't worry about cleaning it up at all.

Resources