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().
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 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.
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
Is this allocation done on the stack and do i need to make delete on this cLastDateToRun code? Its inside a function.
wchar_t cLastDateToRun[9] = { 0 };
wcsncpy_s(cLastDateToRun, SerialNumber, 8);
cLastDateToRun[8] = L'\0';
int LastDateToRun = _wtoi(cLastDateToRun);
delete[] cLastDateToRun;
Since you have your array on stack no need to free() it. Only memory which is allocated by you using malloc() calloc() or realloc() should be freed.
Freeing memory explicitly on stack will lead to undefined behavior.
For C(can also be used with C++):
You can only free() something you malloc,calloc or realloc
For C++ :
You can use delete or delete[] something you new or new[]
Freeing or deleting something not dynamically allocated will cause issues like undefined behavior.
For more knowledge you better refer more about dynamic memory
IMHO the other comments are too weak. It's not you need not it's you must not. Freeing is for dynamically allocated memory. The thing you have is reserved by the compiler and vanishes after leaving the function (but in the case of static)
I've not standard at hand, but I'd assume hat freeing a local variable not dynamically allocated invokes undefined behaviour. So that would mean it's free to to any harm to whatever it steers.
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 9 years ago.
Improve this question
What is the main reason to use function returning pointer? Basically its return the pointer value but there are we can also return the value through pointer.
So why we need function returning pointer?? Is it reduce the size of code or any else?
Is there any application where we use function returning pointer?
Not sure what you are asking but yes? you could copy a 2Gb variable or an address to that variable that is orders of magnitude smaller. You tell me which is quicker/more efficient ;)
Here is a tutorial that explains their usage:
C pointers
Right off the bat it explains how it is easy to use numbers to lookup a safety deposit box so you can go access it.
without the ability to return pointers there would be no point to even having pointers at all. i.e. when you allocated memory for data, a function returns the pointer to its location. So without that you would never know were the memory you allocated is.
So I guess the functionality of returning pointers is arguably to allow the presence of pointers themselves?
One example, you can return a pointer to a value. Anyone who has that pointer can then change the value.