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

delete [] problem 2

Status
Not open for further replies.

bubak

Programmer
Jun 25, 2001
208
SK
When I do this :

LPTSTR s=new char[5];
sprintf(s,"Starting on %d",uID-3000);
MessageBox(s,NULL,MB_OK);
delete [] s;

it throw an error on the delete [] line, it functions firts after I do ...new char[15] or greater. Why ?

bubak
 
Looks to me that the string length of s may be the problem. Try a larger number then 15, maybe 30. Or get really large and try 128 or something.

Matt
 
yep, when the string is longer than 2^4 it works fine, but why ?
 
becuse in sprintf you try to put many characters than are allocated for your pointer. John Fill
1c.bmp


ivfmd@mail.md
 
It seems that the problem comes from the line:

sprintf(s,"Starting on %d",uID-3000);

where you are writting a string "Starting on %d" which is 14 characters length into a LPTSTR(which is an unsigned short*) alocated at a length of five.

Use LPSTR instead, or even better char*
Anyway I don't understand why are you working with the new/delete(on the heap).
If you were working on the stack with declarations like
char s[100]; you wouldn't have any problems

Hope this helps,
s-)

Blessed is he who in the name of justice and good will, shepards the week through the valley of darknees...
 
thanx U all, that was a stupid problem ;-| ;-(
first it was just sprintf(s,"%d",i) and than I forgot to make the string longer ;-(
thx
 
the reason why is uID - 3000. if this value is greater then 9999 then it is 5 characters long giving you a string of 16 characters into an array of 15 characters.

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top