473,473 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

memory overflow

See the program below. I am trying to copy 20 bytes of data in a 10
bytes of buffer. The copy succeeds and also it displays the result
correctly. But the delete [] a1 fails and gives an error. Can anyone
point out the exact reason why delete fails.

int main()
{
char*a1 = new char[10];

char* str20 = "12345678901234567890";

strcpy(a1,str20);
cout<<"a1: "<<a1;

delete [] a1;
}
Nov 19 '07 #1
4 2559
On Sun, 18 Nov 2007 22:29:54 -0800 (PST) in comp.lang.c++,
singhraghvendra <ra********@gmail.comwrote,
>See the program below. I am trying to copy 20 bytes of data in a 10
bytes of buffer. The copy succeeds and also it displays the result
correctly. But the delete [] a1 fails and gives an error. Can anyone
point out the exact reason why delete fails.
Copying 20 bytes to a 10 byte buffer is invalid. Subsequent behavior of
your program is undefined, anything may happen with no guarantees.
In short, Don't Do That!

Nov 19 '07 #2
On Sun, 18 Nov 2007 22:29:54 -0800 (PST), singhraghvendra
<ra********@gmail.comwrote in comp.lang.c++:
See the program below. I am trying to copy 20 bytes of data in a 10
bytes of buffer. The copy succeeds and also it displays the result
What do you mean by "succeeds"?
correctly. But the delete [] a1 fails and gives an error. Can anyone
point out the exact reason why delete fails.
What do you mean, "delete fails". What makes you think if fails? How
do you know that it fails?

Writing past the end of an array generates undefined behavior. There
is no "reason" why delete fails. Once you produce undefined behavior,
you are beyond the realm of the C++ language. The language does not
define, nor place any requirements on, the behavior of the program.
int main()
{
char*a1 = new char[10];

char* str20 = "12345678901234567890";

strcpy(a1,str20);
cout<<"a1: "<<a1;

delete [] a1;
}
There is no such thing as an "exact reason" when undefined behavior is
involved. Anything that happens is just as right or wrong as anything
else. You broke the rules, anything goes.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Nov 19 '07 #3
On Nov 19, 7:24 am, Jack Klein <jackkl...@spamcop.netwrote:
On Sun, 18 Nov 2007 22:29:54 -0800 (PST), singhraghvendra
<raghu.i...@gmail.comwrote in comp.lang.c++:
See the program below. I am trying to copy 20 bytes of data in a 10
bytes of buffer. The copy succeeds and also it displays the result

What do you mean by "succeeds"?
correctly. But the delete [] a1 fails and gives an error. Can anyone
point out the exact reason why delete fails.

What do you mean, "delete fails". What makes you think if fails? How
do you know that it fails?

Writing past the end of an array generates undefined behavior. There
is no "reason" why delete fails. Once you produce undefined behavior,
you are beyond the realm of the C++ language. The language does not
define, nor place any requirements on, the behavior of the program.
int main()
{
char*a1 = new char[10];
char* str20 = "12345678901234567890";
strcpy(a1,str20);
cout<<"a1: "<<a1;
delete [] a1;
}

There is no such thing as an "exact reason" when undefined behavior is
involved. Anything that happens is just as right or wrong as anything
else. You broke the rules, anything goes.
That said, of course, there is a reason why it "fails" in this case.
It's just implementation specific. The C++ spec allows the compiler/
runtime implementors to make certain assumptions about how correct the
code is. They've implemented the delete[] assuming that you have
respected the requirements of creating a dynamic array. It might be,
for instance, that the runtime is storing the size of the array that
you allocated at the memory at the end of the array. When you overflow
the array you overwrite the array size and when you delete the wrong
size is read back, the runtime tries to delete memory it doesn't own
and crashes. If you really want to know what's going on check the
compiled code.

Nov 19 '07 #4
singhraghvendra wrote:
See the program below. I am trying to copy 20 bytes of data in a 10
bytes of buffer. The copy succeeds and also it displays the result
correctly. But the delete [] a1 fails and gives an error. Can anyone
point out the exact reason why delete fails.

int main()
{
char*a1 = new char[10];

char* str20 = "12345678901234567890";

strcpy(a1,str20);
cout<<"a1: "<<a1;

delete [] a1;
}
Be very careful when "learning by doing" with C++. Many things which
appear to work fine actually invoke undefined behaviour - meaning that
mysterious failures like this are possible. In other words, just because
you've tried something out and it worked for you, doesn't mean it's
valid C++.

--
Philip Potter pgp <atdoc.ic.ac.uk
Nov 19 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: trouble | last post by:
Hi, I have a program that needs to reserve about 1 to 10 Mb of memory every time it's run. Only one instance of the program is run at any given time but it's called about 100 times in...
3
by: Rajwarya | last post by:
Hi ppl, I am trying to run a code on SUN OS 5.8 and using Sun WorkShop 6 update 2 C++ 5.3. The problem is that the code is leaking memory in strstreambuf. We are using ostrstream with...
11
by: Yvad | last post by:
When I encounter software crash, the software always pop-up something like " The instruction at "0x1000a1eb" referenced memory at "0x000000c0". The memory could not be "read"". Then Visual C++...
11
by: Sushil | last post by:
Hi Gurus I've tried to come up with a small logical example of my problem. The problem is platform specific (MIPS) which I understand should not be discussed here. So here goes my example: ...
1
by: BillyO | last post by:
In the attached code fragment I have a buffer overflow and a memory access violation. When I run the code .Net fails to verify the IL because of the buffer overflow and I get an exception as...
5
by: Senapathy | last post by:
Environment: WinXP, VC++ 7.1 Standard Edition ~~~~~~~~~~~ I have a set of functions that I invoke from main() function of a sample console app. These are the functions:...
9
by: Randy Yates | last post by:
Hi Folks, This may be somewhat off-topic, but it sorta fits. This is a C++ application that will be built using gcc and targeted (via the wonderful wxWidgets library) for both Windoze and...
49
by: vfunc | last post by:
If I have a large array 10,000+ elements then how do I reserve memory for this ? Currently I get a segmentation fault. Dynamic reservation is good, but allowing a chunk for the program is an...
5
by: nithya4u | last post by:
I am working on a c++ module, where large amount of data needs to be written to a stream and str() method is been used to assign the value of this to the char*. This produces the intended result....
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.