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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Unhandled exception" after strcpy

Status
Not open for further replies.

cadbilbao

Programmer
Apr 9, 2001
233
ES
Hi!

Using VC++ 6.0, if I insert this piece of code into my program, I get the
"Unhandled exception. Acces violation" error:


--------------//------------------
char *file,*name;
name = attr.getNodeValue().transcode(); //It's a value from parsing an XML doc
strcpy(file,name);
-------------//-------------------



I know that I am problably accessing memory I don't own, or I did
not allocate enough memory. But my question is:

+ How can I solve this?
+ How can access the memory I own?
+ How can allocate memory? Wich are the commands?

TIA.
 
first you must alloc memory for string to copy.
file=new char[strlen(name)+1];
strcpy(file,name); John Fill
1c.bmp


ivfmd@mail.md
 
first you must alloc memory for string to copy.

And free it when you're done.

[tt]free(file);[/tt]

Chip H.
 
I think C++ operator delete is better:
delete[] file; John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top