error in releasing the static array in debug mode [duplicate] - c++

This question already has answers here:
Must I delete a static array in C++?
(4 answers)
A bit confused on exact meaning of dynamic memory allocation for C++
(2 answers)
Closed 7 years ago.
Why it has error in releasing the static array in debug mode?
int main()
{
int ar[] = { 1,2,3,4,5,6,7,8,9 };
//other code
delete(ar);
// or free(ar);
return 0;
}
I used free or delete to release the array and it finished with error in debug mode.
Do i use the free or delete correctly?
How can i release the array?

ar[] is not allocated on the heap, but local/on the stack, so can't be (and shouldn't be) deleted.
The memory is released on function (or block {}) exit.
You can only use delete with new or free() with malloc()

the delete operator and 'free' function are to be used only on pointers that
own memory allocated on the heap. your array is allocated on the stack, and
the internal implementation will crash when it will not find any heap structure.
moreover, delete is only to be applied on memory allocated with new
and free only on memory allocated with malloc,calloc or realloc.
last thing is that when you use delete on an array, use delete [] for
this means that the removal will take place on an earlier offset on the stack,
when the record of the array itself is allocated, doing otherwise might end with a memory leak or worse

Related

How does delete operator work with pointers in c? [duplicate]

This question already has answers here:
How does delete[] "know" the size of the operand array?
(9 answers)
Closed 3 years ago.
How does delete operator work in C++?
int *ptr = new int[2];
delete[] ptr;
How does delete operator know the amount of memory allocated, since ptr is just a int pointer and increment-ing(++) it would simply make it point to the next location in the continuously allocated structure.
Allocators typically hide allocation information just before the pointer in question. The allocation includes that space, but the pointer is moved after it so you don't access/modify it. This is part of why writing to a pointer at a negative index breaks things so badly.
As noted in the comments, your code is broken as written, since you used delete ptr;, not delete[] ptr;; only the latter knows to look for the information needed to destruct the whole array, not just a single element.

Confusion in syntax related to Deallocating Heap Arrays [duplicate]

This question already has answers here:
delete vs delete[] operators in C++
(7 answers)
Closed 5 years ago.
When we deallocate the heap memory occupied by an array, I have a little confusion regarding the syntax
int *p = new int[5];
Now for deallocating, which one is correct from the following:
delete p;
OR
delete[ ] p;
The latter seems to be more correct. But it confuses me, I don't understand that how would it know that how much memory the array exists on. I mean, we are only giving it the starting address of the array(through p). So, beginning from the starting address how will the compiler know that till where does it have to deallocate, and when to stop the deallocation.
Your second syntax is correct, and the compiler knows the size of the array since it took note of it when you allocated the array.This is usually stored in a piece of memory just before the memory that you allocated for the array. That way when it's time to free the memory, the deallocator knows exactly how much memory to free, by checking this memory.
In the below statement you are creating memory dynamically for int array of 5 integer
int *p = new int[5];
Its looks like below
p = operator new [] (20); /** p points to first 20 bytes from starting address **/
To free or de-allocating p you should use
delete [] p;
It's looks like below
operator delete [] (p); /* It free frees memory of 20 bytes from where p is pointing */
Note : if you using only delete p then it won't free whole 20 bytes. delete p internally converts as operator delete (p);

deleting the pointer object corrupts the heap memory [duplicate]

This question already has answers here:
What happens in a double delete?
(8 answers)
Closed 8 years ago.
I was studying about creating a object with pointer using dynamic allocation. And i read that when an object is created once and its deleted twice,The heap memory gets corrupted. what does corrupted means? Is it similar to memory leak or is it something else?
int main()
{
//consider my class name is sample
sample *p= new sample;
//some code
delete p;
//some code
delete p;
}
when i delete the p for the first time, the memory pointed by p gets cleared and returns safely to the heap. what happens the next time?
The free store is a carefully managed system of free and allocated blocks, and new and delete do bookkeeping to keep everything in a consistent state. If you delete again, the system is likely to do the same bookkeeping on invalid data, and suddenly the free store is in an inconsistent state. This is known as "heap corruption".
Once that happens, anything you do with new or delete may have unpredictable results, which can include attempting to write outside the application's memory area, corrupting data, erroneously thinking there's no more memory, or overlapping allocation.
Safest bet is always set pointer to null after deleting it.
int *ptr = new int;
// do something
delete ptr;
ptr = null;

What does delete[] do if given a pointer to the middle of an array? [duplicate]

This question already has answers here:
Can I delete[] a pointer that points into an allocated array, but not to the start of it?
(7 answers)
Closed 9 years ago.
What would happen in the following code?
int *p1 = new int[100];
int *p2 = &p1[50];
delete [] p2;
I've heard that some implementations of new store the size of the array in the (-1)th array index, but then wouldn't things go horribly wrong in the above?
Things would definitely go wrong!
The delete [] operator is defined to only work on proper array pointers. And by proper I mean it must receive a pointer that was previously initialized to point to a location in memory where an array was created with the new operator.
You should also never mix and match new/delete and malloc/free. As a rule always delete memory that has been allocated with new, and free memory that has been allocated with malloc (and derivatives)

Segfault when deleting pointer

I've been experiencing segfaults when running some C++ code. I've isolated the problem to a line in the program that deletes a pointer. Here's a simple example that produces the same error:
int main()
{
int* pointer=0;
int number = 3;
pointer = &number;
delete pointer;//This line causes a segmentation fault
pointer=0;
return 0;
}
A slight modification produces code that will work as expected:
int main()
{
int* pointer=new int(3);
delete pointer;//This line now works
pointer=0;
return 0;
}
Can someone explain why the first causes a segfault and the second does not? I know the pointer isn't invalid, since it's been assigned to the address of the number variable.
You should only ever delete memory that has been allocated with new. Automatic variables declared on the stack do not need to be deleted. As a rule, always match your memory allocation and deallocation types:
Memory allocated with new should be deallocated with delete.
Memory allocated with new [] should be deallocated with delete [].
Memory allocated with malloc() should be deallocated with free().
The segfault is because the delete operator will attempt to put that memory back into the heap, and that relies on certain properties of the memory that don't hold true for automatic memory on the stack that didn't originate from the heap.
You can't use delete on anything you didn't get with new. Trying to do so will cause undefined behaviour. Your program crashed, but anything could have happened.
Calling delete on a pointer, deallocates the dynamically allocated memory that the pointer points to.
In the first program, pointer points to a statically allocated memory location.The variable number is an 'automatic' variable, which means that its memory is automatically managed.
On the other hand in the second program, pointer is pointing to a memory location allocated in the heap segment, which needs to be manually deallocated by calling delete.
You might find this link useful.
When you delete a pointer that wasn't allocated with new, you're creating a conflict between the memory management system and the stack. Each will operate as if it still has sole ownership of the memory, and a crash can result when they overwrite each other's values.
When you allocate a variabile with new:
int *a=new int(4);
This variable is put on the heap, which contains all the memory dnamicly allocated.
If instead you declare a variable:
int a=4;
a is allocated in the stack, where there is static memory.
Dynamic memory can be deallocated with delete from the user, but static memory can not.
Static memory is automaticlly deallocated when yuo exit froma function:
void function()
{
int a;
}
When the function ends a is automatically deallocated (except for variables declared with the keyword "static").
Also variables in main function are automatically deallocated.
So you can not say to the program to deallocate a variable in the stack.
In your example number is on the stack, pointer points to number which is in the stack, if you delete it you are trying to delete a variable in the stack, which is not allowed,because it's not dynamic memory.