473,326 Members | 2,114 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,326 software developers and data experts.

delete[] causing DAMAGE

Whats wrong with the code ? delete[] tp; is throwing DAMAGE: After
normal block(#56) at 0x00321480
Environment, VS2003, XP

#include <iostream>
#include <fstream>
#include <string>
#include <set>
#include <stack>
#include <sstream>

using namespace std;
string PutS(std::string& type)
{
char *tp;
tp = new char[type.length()+1];
type.resize(type.length() + 1);
memcpy(tp, type.c_str(), strlen(type.c_str()));
*(tp + type.length()-1) = 's';
*(tp + type.length()) = '\0';
string ntype(tp);
cout << ntype.c_str();
delete[] tp;
return ntype;
}

int main()
{
std::string type = "CAMERA";
PutS(type);

getchar();
}

Sep 26 '07 #1
3 1435
raan wrote:
Whats wrong with the code ? delete[] tp; is throwing DAMAGE: After
normal block(#56) at 0x00321480
Environment, VS2003, XP

#include <iostream>
#include <fstream>
#include <string>
#include <set>
#include <stack>
#include <sstream>

using namespace std;
string PutS(std::string& type)
{
char *tp;
tp = new char[type.length()+1];
What's the intent? If you need to append 's' to your string,
shouldn't you then allocate +2 and not +1?
type.resize(type.length() + 1);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Why do you do that? Do you realise that 'length()' will
now return a different value?
memcpy(tp, type.c_str(), strlen(type.c_str()));
*(tp + type.length()-1) = 's';
*(tp + type.length()) = '\0';
You allocated fewer characters than you're trying to access.
string ntype(tp);
cout << ntype.c_str();
delete[] tp;
return ntype;
}

int main()
{
std::string type = "CAMERA";
PutS(type);

getchar();
}
I am not sure why you're doing all this dancing around with
naked pointers. Why don't you simply define a local string,
initialise it from the string passed in, and then append
something to it using += ?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 26 '07 #2
"raan" <pa*******@gmail.comwrote in message
news:11**********************@r29g2000hsg.googlegr oups.com...
Whats wrong with the code ? delete[] tp; is throwing DAMAGE: After
normal block(#56) at 0x00321480
Environment, VS2003, XP

#include <iostream>
#include <fstream>
#include <string>
#include <set>
#include <stack>
#include <sstream>

using namespace std;
string PutS(std::string& type)
{
char *tp;
tp = new char[type.length()+1];
type.resize(type.length() + 1);
memcpy(tp, type.c_str(), strlen(type.c_str()));
*(tp + type.length()-1) = 's';
*(tp + type.length()) = '\0';
string ntype(tp);
cout << ntype.c_str();
delete[] tp;
return ntype;
}

int main()
{
std::string type = "CAMERA";
PutS(type);

getchar();
}
Why don't you just do:

std::string PutS( std::string& type )
{
return type + "s";
}
Sep 26 '07 #3
On Sep 26, 2:57 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
I am not sure why you're doing all this dancing around with
naked pointers. Why don't you simply define a local string,
initialise it from the string passed in, and then append
something to it using += ?
duh

Sep 26 '07 #4

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

Similar topics

11
by: Spikinsson | last post by:
I create a new char and when I attempt to delete it, the debug gives this error (during runtime): Debug Error! Program: xxx DAMAGE: after Normal block (#58) at 0x00C315A0.
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
4
by: pike | last post by:
8.1 FP11 on AIX 5.3. The following DELETE is poorly performing and causing lock escalation (and subsequent deadlock time-outs): DELETE FROM submission_log WHERE subm_id = ? OR subm_id =...
6
by: mdinino | last post by:
Hi, I am new to C++, and I have a simple question but I can't seem to find a direct answer. If I use new to create something in the scope of a function and then I return the pointer that I...
5
by: Angus | last post by:
Hello I am doing this in a worker thread: char* szPartial = new char; if (szPartial) { lstrcpy(szPartial, szBuffer); PostMessage(m_thishWnd, WM_USER+1, 0, (LPARAM)szPartial); }
3
by: alanbe | last post by:
Someone filled out a comment form to me with the following string within the message: #file=E:\\util\\xr32\\Projects\\www42t35Href.txt The comments are stored in a mysql database When...
5
by: Renato | last post by:
I have an array of pointers to class Shape. I create 4 items and display their values. shapes Shape *shapes; shapes = new Shape (p1); shapes = new Triangle (p1); shapes = new Polygon (p2);
23
by: Martin T. | last post by:
Hi all! char* p = new char; // POD! .... delete p; // std compliant delete p; // will work on VC8 free(p); // will also work on VC8 I am interested on which platforms/compilers the...
0
by: Now You Know | last post by:
Water Damage Restoration 24 hour open 7 Days Phone 1 877 364 5264 When disaster strikes such as a washing machine overflow, bath overflow, burst pipe, rainwater from balcony etc, water damage...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.