Difference between pointer to a new element and new array? [duplicate]How could pairing new[] with delete possibly lead to memory leak only?What is the difference between #include <filename> and #include “filename”?What are the differences between a pointer variable and a reference variable in C++?What is a smart pointer and when should I use one?Is the practice of returning a C++ reference variable evil?What is the difference between const int*, const int * const, and int const *?What is difference between instantiating an object using new vs. withoutWhat are the basic rules and idioms for operator overloading?When to use references vs. pointersWhy are elementwise additions much faster in separate loops than in a combined loop?Why is it faster to process a sorted array than an unsorted array?
What is the largest (size) solid object ever dropped from an airplane to impact the ground in freefall?
Who will lead the country until there is a new Tory leader?
What does the view outside my ship traveling at light speed look like?
Can I install both XCode & Android Studio on MacBook Air with only 8 GB of Ram
Does Nitrogen inside commercial airliner wheels prevent blowouts on touchdown?
Website returning plaintext password
Compactness of finite sets
Are these reasonable traits for someone with autism?
Is real public IP Address hidden when using a system wide proxy in Windows 10?
What is the object moving across the ceiling in this stock footage?
How strong are Wi-Fi signals?
How to illustrate the Mean Value theorem?
If a person had control of every single cell of their body, would they be able to transform into another creature?
Image processing: Removal of two spots in fundus images
Should one buy new hardware after a system compromise?
How can I tell if I'm being too picky as a referee?
What are these arcade games in Ghostbusters 1984?
How to use Palladio font in text body but Computer Modern for Equations?
Count rotary dial pulses in a phone number (including letters)
Use backslash or single-quotes for field separation
Construct a word ladder
When and what was the first 3D acceleration device ever released?
Plot twist where the antagonist wins
Where have Brexit voters gone?
Difference between pointer to a new element and new array? [duplicate]
How could pairing new[] with delete possibly lead to memory leak only?What is the difference between #include <filename> and #include “filename”?What are the differences between a pointer variable and a reference variable in C++?What is a smart pointer and when should I use one?Is the practice of returning a C++ reference variable evil?What is the difference between const int*, const int * const, and int const *?What is difference between instantiating an object using new vs. withoutWhat are the basic rules and idioms for operator overloading?When to use references vs. pointersWhy are elementwise additions much faster in separate loops than in a combined loop?Why is it faster to process a sorted array than an unsorted array?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This question already has an answer here:
How could pairing new[] with delete possibly lead to memory leak only?
10 answers
In C++ is there any difference between the pointers p and q in the below code?
int* p = new int;
int* q = new int[5];
I understand that one allots new memory for a single int and the second allots memory for an array of 5 ints, but fundamentally is there any difference between a pointer pointing to a single int and one pointing to an array of ints?
I got this doubt because I read that one must use delete[] q to free up memory pointed to by q but just delete p for the single int pointed to by p.
What would happen if I used delete q?
c++
New contributor
strider0160 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
marked as duplicate by genpfault
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
May 20 at 16:55
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 2 more comments
This question already has an answer here:
How could pairing new[] with delete possibly lead to memory leak only?
10 answers
In C++ is there any difference between the pointers p and q in the below code?
int* p = new int;
int* q = new int[5];
I understand that one allots new memory for a single int and the second allots memory for an array of 5 ints, but fundamentally is there any difference between a pointer pointing to a single int and one pointing to an array of ints?
I got this doubt because I read that one must use delete[] q to free up memory pointed to by q but just delete p for the single int pointed to by p.
What would happen if I used delete q?
c++
New contributor
strider0160 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
marked as duplicate by genpfault
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
May 20 at 16:55
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
sloppy speaking,deletedoesnt do much to the pointer itself but to the stuff it points to and deleting anintis not the same as deleting an array of ints
– formerlyknownas_463035818
May 20 at 12:08
2
Some implementations will track the size of the allocation of the array in the address before the pointer returned. This is an implementation detail not defined by the standard. You must remember to use the correct form of delete however.
– drescherjm
May 20 at 12:09
@drescherjm I thought of something like this but i was unsure of how the compiler would find the size of the allocation. Could you point me to more info on this?
– strider0160
May 20 at 12:35
I don't have a good reference. This answer talks a little more about how this could work: stackoverflow.com/a/1913393/487892
– drescherjm
May 20 at 12:37
@drescherjm Your answer is more informative than the current answer, so if you can, please write it as an answer, so that I can mark it as accepted and close the question.
– strider0160
May 20 at 12:47
|
show 2 more comments
This question already has an answer here:
How could pairing new[] with delete possibly lead to memory leak only?
10 answers
In C++ is there any difference between the pointers p and q in the below code?
int* p = new int;
int* q = new int[5];
I understand that one allots new memory for a single int and the second allots memory for an array of 5 ints, but fundamentally is there any difference between a pointer pointing to a single int and one pointing to an array of ints?
I got this doubt because I read that one must use delete[] q to free up memory pointed to by q but just delete p for the single int pointed to by p.
What would happen if I used delete q?
c++
New contributor
strider0160 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
This question already has an answer here:
How could pairing new[] with delete possibly lead to memory leak only?
10 answers
In C++ is there any difference between the pointers p and q in the below code?
int* p = new int;
int* q = new int[5];
I understand that one allots new memory for a single int and the second allots memory for an array of 5 ints, but fundamentally is there any difference between a pointer pointing to a single int and one pointing to an array of ints?
I got this doubt because I read that one must use delete[] q to free up memory pointed to by q but just delete p for the single int pointed to by p.
What would happen if I used delete q?
This question already has an answer here:
How could pairing new[] with delete possibly lead to memory leak only?
10 answers
c++
c++
New contributor
strider0160 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
strider0160 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
strider0160 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked May 20 at 12:05
strider0160strider0160
635
635
New contributor
strider0160 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
strider0160 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
marked as duplicate by genpfault
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
May 20 at 16:55
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by genpfault
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
May 20 at 16:55
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
sloppy speaking,deletedoesnt do much to the pointer itself but to the stuff it points to and deleting anintis not the same as deleting an array of ints
– formerlyknownas_463035818
May 20 at 12:08
2
Some implementations will track the size of the allocation of the array in the address before the pointer returned. This is an implementation detail not defined by the standard. You must remember to use the correct form of delete however.
– drescherjm
May 20 at 12:09
@drescherjm I thought of something like this but i was unsure of how the compiler would find the size of the allocation. Could you point me to more info on this?
– strider0160
May 20 at 12:35
I don't have a good reference. This answer talks a little more about how this could work: stackoverflow.com/a/1913393/487892
– drescherjm
May 20 at 12:37
@drescherjm Your answer is more informative than the current answer, so if you can, please write it as an answer, so that I can mark it as accepted and close the question.
– strider0160
May 20 at 12:47
|
show 2 more comments
1
sloppy speaking,deletedoesnt do much to the pointer itself but to the stuff it points to and deleting anintis not the same as deleting an array of ints
– formerlyknownas_463035818
May 20 at 12:08
2
Some implementations will track the size of the allocation of the array in the address before the pointer returned. This is an implementation detail not defined by the standard. You must remember to use the correct form of delete however.
– drescherjm
May 20 at 12:09
@drescherjm I thought of something like this but i was unsure of how the compiler would find the size of the allocation. Could you point me to more info on this?
– strider0160
May 20 at 12:35
I don't have a good reference. This answer talks a little more about how this could work: stackoverflow.com/a/1913393/487892
– drescherjm
May 20 at 12:37
@drescherjm Your answer is more informative than the current answer, so if you can, please write it as an answer, so that I can mark it as accepted and close the question.
– strider0160
May 20 at 12:47
1
1
sloppy speaking,
delete doesnt do much to the pointer itself but to the stuff it points to and deleting an int is not the same as deleting an array of ints– formerlyknownas_463035818
May 20 at 12:08
sloppy speaking,
delete doesnt do much to the pointer itself but to the stuff it points to and deleting an int is not the same as deleting an array of ints– formerlyknownas_463035818
May 20 at 12:08
2
2
Some implementations will track the size of the allocation of the array in the address before the pointer returned. This is an implementation detail not defined by the standard. You must remember to use the correct form of delete however.
– drescherjm
May 20 at 12:09
Some implementations will track the size of the allocation of the array in the address before the pointer returned. This is an implementation detail not defined by the standard. You must remember to use the correct form of delete however.
– drescherjm
May 20 at 12:09
@drescherjm I thought of something like this but i was unsure of how the compiler would find the size of the allocation. Could you point me to more info on this?
– strider0160
May 20 at 12:35
@drescherjm I thought of something like this but i was unsure of how the compiler would find the size of the allocation. Could you point me to more info on this?
– strider0160
May 20 at 12:35
I don't have a good reference. This answer talks a little more about how this could work: stackoverflow.com/a/1913393/487892
– drescherjm
May 20 at 12:37
I don't have a good reference. This answer talks a little more about how this could work: stackoverflow.com/a/1913393/487892
– drescherjm
May 20 at 12:37
@drescherjm Your answer is more informative than the current answer, so if you can, please write it as an answer, so that I can mark it as accepted and close the question.
– strider0160
May 20 at 12:47
@drescherjm Your answer is more informative than the current answer, so if you can, please write it as an answer, so that I can mark it as accepted and close the question.
– strider0160
May 20 at 12:47
|
show 2 more comments
3 Answers
3
active
oldest
votes
When using new [] some c++ implementations will track the size of the allocation of the array in the address before the pointer returned. This is an implementation detail not defined by the standard.
The following answer describes this possible implementation in a little more detail: How could pairing new[] with delete possibly lead to memory leak only?
You must always match new with delete and new [] with delete []. It is Undefined Behavior to mix these.
This is patently wrong, the standard specifies that there are multiple differences between the pointers, and conflating them consequently leads to undefined behaviour. What's true is that the implementation isn't obliged to notify the programmer when they forget the differences.
– Konrad Rudolph
May 20 at 16:55
add a comment |
The pointers themselves are completely indistinguishable. That's why you must remember to match new/delete and new[]/delete[].
Mismatching them triggers undefined behaviour.
add a comment |
In C++ is there any difference between the pointers p and q in the below code?
There is no visible difference between the pointers, but there certainly is one, and it is important. One is a pointer to an integer, the other is a pointer to an integer, which is also the first element in an array of a given size.
Unluckily, given only the pointer, you have no way of telling.
What would happen if I used delete q?
Probably nothing, but possibly a lot.
First of all, calling delete instead of delete[] will call the destructor exactly once, on the first element of the array, rather than on every element as it should. Now, the destructor for a trivial type like int doesn't do anything, so... as far as that goes, there is no real difference. There is, however, a huge difference for not-so-trivial types where the destructor (or chain of destructors) actually does something.
Second, you are interfering with proper deallocation of the underlying raw memory block. This can (and sometimes does) cause a hard crash. It might even cause a crash which occurs at a later time, in an unrelated, innocent piece of code, due to corruption earlier. Try and debug that.
Or, you might get a silent memory leak, it depends on the implementation and sometimes even on your "luck" in the particular case (e.g. hit a page boundary or not).
Because, well, allocating and freeing an array and allocating and freeing a single element just isn't the same thing. They are (usually) implemented slightly differently, and while the implementation may be able to cope with mismatched new/delete, that isn't guaranteed. You might get different behavior in the debugger compared to normal exection, too, whatever, anything.
Calling the wrong form of delete means invoking undefined behavior. Which basically means anything can happen. That includes "nothing" as well as "problem which is impossible to debug". It also includes the possibility of the compiler optimizing maliciously or just stripping out the entire surrounding function, or assuming a certain condition to be always-true. Which can lead to very nasty surprises on which you spend days and days trying to figure out what's going on.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
When using new [] some c++ implementations will track the size of the allocation of the array in the address before the pointer returned. This is an implementation detail not defined by the standard.
The following answer describes this possible implementation in a little more detail: How could pairing new[] with delete possibly lead to memory leak only?
You must always match new with delete and new [] with delete []. It is Undefined Behavior to mix these.
This is patently wrong, the standard specifies that there are multiple differences between the pointers, and conflating them consequently leads to undefined behaviour. What's true is that the implementation isn't obliged to notify the programmer when they forget the differences.
– Konrad Rudolph
May 20 at 16:55
add a comment |
When using new [] some c++ implementations will track the size of the allocation of the array in the address before the pointer returned. This is an implementation detail not defined by the standard.
The following answer describes this possible implementation in a little more detail: How could pairing new[] with delete possibly lead to memory leak only?
You must always match new with delete and new [] with delete []. It is Undefined Behavior to mix these.
This is patently wrong, the standard specifies that there are multiple differences between the pointers, and conflating them consequently leads to undefined behaviour. What's true is that the implementation isn't obliged to notify the programmer when they forget the differences.
– Konrad Rudolph
May 20 at 16:55
add a comment |
When using new [] some c++ implementations will track the size of the allocation of the array in the address before the pointer returned. This is an implementation detail not defined by the standard.
The following answer describes this possible implementation in a little more detail: How could pairing new[] with delete possibly lead to memory leak only?
You must always match new with delete and new [] with delete []. It is Undefined Behavior to mix these.
When using new [] some c++ implementations will track the size of the allocation of the array in the address before the pointer returned. This is an implementation detail not defined by the standard.
The following answer describes this possible implementation in a little more detail: How could pairing new[] with delete possibly lead to memory leak only?
You must always match new with delete and new [] with delete []. It is Undefined Behavior to mix these.
edited May 20 at 17:02
answered May 20 at 12:53
drescherjmdrescherjm
6,75523553
6,75523553
This is patently wrong, the standard specifies that there are multiple differences between the pointers, and conflating them consequently leads to undefined behaviour. What's true is that the implementation isn't obliged to notify the programmer when they forget the differences.
– Konrad Rudolph
May 20 at 16:55
add a comment |
This is patently wrong, the standard specifies that there are multiple differences between the pointers, and conflating them consequently leads to undefined behaviour. What's true is that the implementation isn't obliged to notify the programmer when they forget the differences.
– Konrad Rudolph
May 20 at 16:55
This is patently wrong, the standard specifies that there are multiple differences between the pointers, and conflating them consequently leads to undefined behaviour. What's true is that the implementation isn't obliged to notify the programmer when they forget the differences.
– Konrad Rudolph
May 20 at 16:55
This is patently wrong, the standard specifies that there are multiple differences between the pointers, and conflating them consequently leads to undefined behaviour. What's true is that the implementation isn't obliged to notify the programmer when they forget the differences.
– Konrad Rudolph
May 20 at 16:55
add a comment |
The pointers themselves are completely indistinguishable. That's why you must remember to match new/delete and new[]/delete[].
Mismatching them triggers undefined behaviour.
add a comment |
The pointers themselves are completely indistinguishable. That's why you must remember to match new/delete and new[]/delete[].
Mismatching them triggers undefined behaviour.
add a comment |
The pointers themselves are completely indistinguishable. That's why you must remember to match new/delete and new[]/delete[].
Mismatching them triggers undefined behaviour.
The pointers themselves are completely indistinguishable. That's why you must remember to match new/delete and new[]/delete[].
Mismatching them triggers undefined behaviour.
edited May 20 at 12:18
P.W
21.6k41961
21.6k41961
answered May 20 at 12:09
QuentinQuentin
47.5k692150
47.5k692150
add a comment |
add a comment |
In C++ is there any difference between the pointers p and q in the below code?
There is no visible difference between the pointers, but there certainly is one, and it is important. One is a pointer to an integer, the other is a pointer to an integer, which is also the first element in an array of a given size.
Unluckily, given only the pointer, you have no way of telling.
What would happen if I used delete q?
Probably nothing, but possibly a lot.
First of all, calling delete instead of delete[] will call the destructor exactly once, on the first element of the array, rather than on every element as it should. Now, the destructor for a trivial type like int doesn't do anything, so... as far as that goes, there is no real difference. There is, however, a huge difference for not-so-trivial types where the destructor (or chain of destructors) actually does something.
Second, you are interfering with proper deallocation of the underlying raw memory block. This can (and sometimes does) cause a hard crash. It might even cause a crash which occurs at a later time, in an unrelated, innocent piece of code, due to corruption earlier. Try and debug that.
Or, you might get a silent memory leak, it depends on the implementation and sometimes even on your "luck" in the particular case (e.g. hit a page boundary or not).
Because, well, allocating and freeing an array and allocating and freeing a single element just isn't the same thing. They are (usually) implemented slightly differently, and while the implementation may be able to cope with mismatched new/delete, that isn't guaranteed. You might get different behavior in the debugger compared to normal exection, too, whatever, anything.
Calling the wrong form of delete means invoking undefined behavior. Which basically means anything can happen. That includes "nothing" as well as "problem which is impossible to debug". It also includes the possibility of the compiler optimizing maliciously or just stripping out the entire surrounding function, or assuming a certain condition to be always-true. Which can lead to very nasty surprises on which you spend days and days trying to figure out what's going on.
add a comment |
In C++ is there any difference between the pointers p and q in the below code?
There is no visible difference between the pointers, but there certainly is one, and it is important. One is a pointer to an integer, the other is a pointer to an integer, which is also the first element in an array of a given size.
Unluckily, given only the pointer, you have no way of telling.
What would happen if I used delete q?
Probably nothing, but possibly a lot.
First of all, calling delete instead of delete[] will call the destructor exactly once, on the first element of the array, rather than on every element as it should. Now, the destructor for a trivial type like int doesn't do anything, so... as far as that goes, there is no real difference. There is, however, a huge difference for not-so-trivial types where the destructor (or chain of destructors) actually does something.
Second, you are interfering with proper deallocation of the underlying raw memory block. This can (and sometimes does) cause a hard crash. It might even cause a crash which occurs at a later time, in an unrelated, innocent piece of code, due to corruption earlier. Try and debug that.
Or, you might get a silent memory leak, it depends on the implementation and sometimes even on your "luck" in the particular case (e.g. hit a page boundary or not).
Because, well, allocating and freeing an array and allocating and freeing a single element just isn't the same thing. They are (usually) implemented slightly differently, and while the implementation may be able to cope with mismatched new/delete, that isn't guaranteed. You might get different behavior in the debugger compared to normal exection, too, whatever, anything.
Calling the wrong form of delete means invoking undefined behavior. Which basically means anything can happen. That includes "nothing" as well as "problem which is impossible to debug". It also includes the possibility of the compiler optimizing maliciously or just stripping out the entire surrounding function, or assuming a certain condition to be always-true. Which can lead to very nasty surprises on which you spend days and days trying to figure out what's going on.
add a comment |
In C++ is there any difference between the pointers p and q in the below code?
There is no visible difference between the pointers, but there certainly is one, and it is important. One is a pointer to an integer, the other is a pointer to an integer, which is also the first element in an array of a given size.
Unluckily, given only the pointer, you have no way of telling.
What would happen if I used delete q?
Probably nothing, but possibly a lot.
First of all, calling delete instead of delete[] will call the destructor exactly once, on the first element of the array, rather than on every element as it should. Now, the destructor for a trivial type like int doesn't do anything, so... as far as that goes, there is no real difference. There is, however, a huge difference for not-so-trivial types where the destructor (or chain of destructors) actually does something.
Second, you are interfering with proper deallocation of the underlying raw memory block. This can (and sometimes does) cause a hard crash. It might even cause a crash which occurs at a later time, in an unrelated, innocent piece of code, due to corruption earlier. Try and debug that.
Or, you might get a silent memory leak, it depends on the implementation and sometimes even on your "luck" in the particular case (e.g. hit a page boundary or not).
Because, well, allocating and freeing an array and allocating and freeing a single element just isn't the same thing. They are (usually) implemented slightly differently, and while the implementation may be able to cope with mismatched new/delete, that isn't guaranteed. You might get different behavior in the debugger compared to normal exection, too, whatever, anything.
Calling the wrong form of delete means invoking undefined behavior. Which basically means anything can happen. That includes "nothing" as well as "problem which is impossible to debug". It also includes the possibility of the compiler optimizing maliciously or just stripping out the entire surrounding function, or assuming a certain condition to be always-true. Which can lead to very nasty surprises on which you spend days and days trying to figure out what's going on.
In C++ is there any difference between the pointers p and q in the below code?
There is no visible difference between the pointers, but there certainly is one, and it is important. One is a pointer to an integer, the other is a pointer to an integer, which is also the first element in an array of a given size.
Unluckily, given only the pointer, you have no way of telling.
What would happen if I used delete q?
Probably nothing, but possibly a lot.
First of all, calling delete instead of delete[] will call the destructor exactly once, on the first element of the array, rather than on every element as it should. Now, the destructor for a trivial type like int doesn't do anything, so... as far as that goes, there is no real difference. There is, however, a huge difference for not-so-trivial types where the destructor (or chain of destructors) actually does something.
Second, you are interfering with proper deallocation of the underlying raw memory block. This can (and sometimes does) cause a hard crash. It might even cause a crash which occurs at a later time, in an unrelated, innocent piece of code, due to corruption earlier. Try and debug that.
Or, you might get a silent memory leak, it depends on the implementation and sometimes even on your "luck" in the particular case (e.g. hit a page boundary or not).
Because, well, allocating and freeing an array and allocating and freeing a single element just isn't the same thing. They are (usually) implemented slightly differently, and while the implementation may be able to cope with mismatched new/delete, that isn't guaranteed. You might get different behavior in the debugger compared to normal exection, too, whatever, anything.
Calling the wrong form of delete means invoking undefined behavior. Which basically means anything can happen. That includes "nothing" as well as "problem which is impossible to debug". It also includes the possibility of the compiler optimizing maliciously or just stripping out the entire surrounding function, or assuming a certain condition to be always-true. Which can lead to very nasty surprises on which you spend days and days trying to figure out what's going on.
answered May 20 at 14:43
DamonDamon
53k15105161
53k15105161
add a comment |
add a comment |
1
sloppy speaking,
deletedoesnt do much to the pointer itself but to the stuff it points to and deleting anintis not the same as deleting an array of ints– formerlyknownas_463035818
May 20 at 12:08
2
Some implementations will track the size of the allocation of the array in the address before the pointer returned. This is an implementation detail not defined by the standard. You must remember to use the correct form of delete however.
– drescherjm
May 20 at 12:09
@drescherjm I thought of something like this but i was unsure of how the compiler would find the size of the allocation. Could you point me to more info on this?
– strider0160
May 20 at 12:35
I don't have a good reference. This answer talks a little more about how this could work: stackoverflow.com/a/1913393/487892
– drescherjm
May 20 at 12:37
@drescherjm Your answer is more informative than the current answer, so if you can, please write it as an answer, so that I can mark it as accepted and close the question.
– strider0160
May 20 at 12:47