Click here to Skip to main content
15,889,861 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
char *day = new char[2];
char *tempMonth = NULL;
char *tempDay = NULL;
char temp[12];

strcpy_s(temp,sizeof(temp),"12-12");
tempDay = strtok_s(temp,"-",&tempMonth);

strcpy_s(day,sizeof(day),tempDay);

delete[] day;
Posted
Comments
NipunG 16-Jun-11 8:59am    
still this code crashes...
Albert Holguin 16-Jun-11 10:08am    
yes there is...
Albert Holguin 16-Jun-11 20:08pm    
lol, no prob...
Sergey Alexandrovich Kryukov 16-Jun-11 14:41pm    
How come? If you do "new" who will reclaim the memory?
--SA

At first glance I woud say misunderstanding sizeof is your problem. sizeof a char* will give you the size of a pointer, i.e. 4 bytes on a 32bit system. Copy 4 bytes into a 2 byte buffer and you're lucky to get as far as the delete[] before it crashes :-)
Check the docs for sizeof.
 
Share this answer
 
Comments
NipunG 16-Jun-11 9:23am    
Thanks Mathew...problem solved:)
Albert Holguin 16-Jun-11 10:10am    
good catch... my 5
Sergey Alexandrovich Kryukov 16-Jun-11 14:39pm    
Agree, a 5.
--SA
Remember that strings are zero terminated! You should always reserve one more character for the \0 at the end of the string. In other words - change:
char *day = new char[2];

to:
char *day = new char[3];

That should do it.

BTW I disagree with strogg - IMHO it's always a good idea to pair new[] with delete[] - even if there are no destructors to call.
 
Share this answer
 
Comments
Albert Holguin 16-Jun-11 10:10am    
I disagree with strogg as well... but think matthew had correct solution
TRK3 16-Jun-11 18:40pm    
Actually, Matthew solved the crash, but he's going to want to add a 3rd char to null terminate *day if he's going to use it anywhere. Without that as soon as he tries to use *day he's going to have a problem.
Albert Holguin 16-Jun-11 20:08pm    
true

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900