473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Wierd Behaviour: Crash at 'delete' of memory.

PSN
Hi all ..

here is the code which crashes at the time of freeing the pointer
memory.

************************************
char *complete_command;
complete_command = new char[strlen(command) + strlen(" >> moto_msg")];

strcpy(complete_command, command);
strcat (complete_command, " >> moto_msg");

<SOME IRRELEVANT FUNCTIONS/CODE>

if (flag != 1)
ULTI_ERROR = -6;
delete [] complete_command; // CRASHES HERE
**************************************

Can someone explain me what am i doing wrong ??

Thanks and regards,
Prakash

Jul 23 '05 #1
3 1530
PSN wrote:
here is the code which crashes at the time of freeing the pointer
memory.

************************************
char *complete_command;
complete_command = new char[strlen(command) + strlen(" >> moto_msg")];
Add one to the number of allocated chars. You forget the terminator.
strcpy(complete_command, command);
strcat (complete_command, " >> moto_msg");

<SOME IRRELEVANT FUNCTIONS/CODE>

if (flag != 1)
ULTI_ERROR = -6;
delete [] complete_command; // CRASHES HERE
**************************************

Can someone explain me what am i doing wrong ??


V
Jul 23 '05 #2
"PSN" <pr********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi all ..

here is the code which crashes at the time of freeing the pointer
memory.

************************************
char *complete_command;
complete_command = new char[strlen(command) + strlen(" >> moto_msg")];

strcpy(complete_command, command);
strcat (complete_command, " >> moto_msg");

<SOME IRRELEVANT FUNCTIONS/CODE>

if (flag != 1)
ULTI_ERROR = -6;
delete [] complete_command; // CRASHES HERE
**************************************

Can someone explain me what am i doing wrong ??


You're writing past the end of your allocated memory.
After that, the behavior of 'delete[]' (and anything else
in your program) is undefined, so anything could happen.

Both 'strcpy()' and 'strcat()' append a terminator ('\0')
after they copy. You didn't provide enough storage for
both your strings and the terminator.

complete_command = new char[strlen(command) + strlen(" >> moto_msg") + 1];

You can eliminate all the headaches of managing your own string
memory by using the std::string type instead.

-Mike
Jul 23 '05 #3
PSN
Thank you both for your answers ..

ya, i just found out my mistake. it was very stupid. Anyway, thanks
again for your quick input.

Prakash

Jul 23 '05 #4

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

Similar topics

48
by: marbac | last post by:
Hi, i heard a lot about "undefined behaviour" in this and other newsgroups dealing with c/c++. Is there a list where all cases with undefined behaviour in C++ are listed? regards marbac
10
by: Alex Vinokur | last post by:
GNU g++ 3.3.3, Cygwin // Stuff static char* mbuffer = NULL; // Stuff void doit()
8
by: LAvoisieR | last post by:
Following test code behaviour suprised me and I dont know what's wrong with this. I have two overloaded constructors within base class and virtual destructor implemented. Derived class uses...
4
by: gautam | last post by:
can anyone pls tell me y is the memory allocation not alligned to 4 Bytes. Note : compiled with gcc in linux 9 void function(int a,int b,int c) { char buffer1; Bytes allocated(as shown by gdb)...
2
by: Vaddina Prakash Rao | last post by:
Hi all .. I describe here a wierd behaviour .. i dont understand why ... This could be very stupid aswell .. so please bear me .. I have been writing a program to accept multiple parameters and...
8
by: Steve | last post by:
Hi all, I had a strange crash this morning with an app I've been working on. I just clicked the main menu bar and got an OutOfMemory exception, with a message of something like "unable to get...
5
by: subaruwrx88011 | last post by:
I am very new to c++ and very new to maps. Below is the source of my program. The output is very strange. Am I inserting correctly into the map? Any help will be greatly appreciated. #include...
3
by: Lyndsey.Ferguson | last post by:
Hello Everyone, I am investigating a crash within our application that has me stumped. I am developing for Mac OS X, but I believe that this may be a regular gcc c++ type question. Here is...
23
by: gribouille | last post by:
Hi, via fgets() i create a array containing a text file. fp = fopen(argv, "r"); while ((c = fgetc(fp)) != EOF) { line = c; when i want to print it
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.