Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access Violation using new char*

Status
Not open for further replies.

biot023

Programmer
Nov 8, 2001
403
GB
Hi.
This is probably really basic stuff, but I'm coming very unstuck with using arrays of chars on the heap.
My class (Tdb_field) only creates them in two places (both class constructors) with the following code - btw, str is a string passed to the constructor, and char* Fdata is the character array I use to store the data.

if(str.length()>0){
Fdata=new char[str.length()+1];
strcpy(Fdata,str.c_str());
}
else
Fdata=0;

In the destructor, I have:

if(Fdata){
if(strlen(Fdata>0)){
delete[]Fdata;
Fdata=0;
}
}

On top of this, I set Fdata to NULL before any of this is reached, and nowhere else in my code do I alter Fdata.
I've tried changing the line in the destructor to "delete Fdata;", but I get the same error.
The only thing that works currently is commenting out the delete, but this is not acceptable for obvious reasons.
I guess I know what the problem is, I just don't know what's causing it - so has anyone any handy hints as to what I might look for?
All help appreciated,
Douglas J Livesey

Common sense is what tells you the world is flat.
 
Oh, how embarrassing...
Hm.
You remember that first C++ book you read, way back in the day?
You remember when it covered copy constructors, and the need to create your own in certain situations?
Like when inserting loads of stuff into a vector?
Good grief!
I hope nobody wasted their time by actually reading this!
Man!
Ah, well, back to the pig farm... ;)

DJL

Common sense is what tells you the world is flat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top