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.
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 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 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
So the question is, how does the original developer prevent a situation where user/new developer is trying to access a free'd memory element.
int *num=(int *)malloc(n*sizeof(int));
int i;
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
}
for(i=0;i<n-1;i++){
temp = some_function(x);
}
free(num);
for(i=0;i<n;i++)
{
printf("\nnum[%d]= %d\n",i,num[i]);
}
P.S.: The above code works and actually prints out data in array. Which is not our intention.
[EDIT] Sorry if I was not clear enough. Someone suggested to ask this as a separate question, I thought why not. Here's the original post
The fact that your code "works" is a manifestation of the undefined behaviour that you're experiencing when reading memory that you no longer own.
You could consider setting num to NULL after the first free call. Then writing num[i] will almost certainly crash the program. It can also occasionally be useful: a free called with NULL passed as the pointer is a no-op.
There's little else you can do unfortunately.
But, in general, setting freed pointers to NULL leads to sloppy programming so I tend to avoid it.
The first thing you can do is set num=NULL.
More generally you can add a malloc hook to write garbage to the memory before freeing the memory (probably debug only). This will force a crash if the memory contains pointers or print garbage in this case when accessing freed memory.
Also since you are talking C++, make use of smart pointers.
Edit: fixed up the crash issue.
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().
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.)