using _ultoa_s is creating a problem
-
Hi all, I am using _ultoa_s for conversion of long to string using this way
_ultoa_s ( pDir32->Dir [ i ].fileSize,(char *)Name,sizeof(&Name), 10 );
pListCtrl->SetItemText ( index, 2, (char *)Name);But the problem is sometimes its executing fine but sometimes it just crashes my application.... i am working in vc 2008.. Thanks in advance
-
Hi all, I am using _ultoa_s for conversion of long to string using this way
_ultoa_s ( pDir32->Dir [ i ].fileSize,(char *)Name,sizeof(&Name), 10 );
pListCtrl->SetItemText ( index, 2, (char *)Name);But the problem is sometimes its executing fine but sometimes it just crashes my application.... i am working in vc 2008.. Thanks in advance
sizeof(&Name) is the culprit - the size of an address (pointer) is always 4 bytes. (for a 32-bit compiler) What type is the variable Name? If its a character array, you should use sizeof(Name). if it's a pointer, you need to know the size with which it was allocated.
-
Hi all, I am using _ultoa_s for conversion of long to string using this way
_ultoa_s ( pDir32->Dir [ i ].fileSize,(char *)Name,sizeof(&Name), 10 );
pListCtrl->SetItemText ( index, 2, (char *)Name);But the problem is sometimes its executing fine but sometimes it just crashes my application.... i am working in vc 2008.. Thanks in advance
VCProgrammer wrote:
But the problem is sometimes its executing fine but sometimes it just crashes my application....
I dont think this is an issue of
_ultoa_s
. It can also be an issue in accessingpDir32->Dir [ i ].fileSize
variable. So first try to isolate it. Please change the code as follows and find out at which line the crash occurs.unsigned long lFileSize = pDir32->Dir [ i ].fileSize;
_ultoa_s ( lFileSize ,(char *)Name,sizeof(&Name), 10 ); -
Hi all, I am using _ultoa_s for conversion of long to string using this way
_ultoa_s ( pDir32->Dir [ i ].fileSize,(char *)Name,sizeof(&Name), 10 );
pListCtrl->SetItemText ( index, 2, (char *)Name);But the problem is sometimes its executing fine but sometimes it just crashes my application.... i am working in vc 2008.. Thanks in advance
-
sizeof(&Name) is the culprit - the size of an address (pointer) is always 4 bytes. (for a 32-bit compiler) What type is the variable Name? If its a character array, you should use sizeof(Name). if it's a pointer, you need to know the size with which it was allocated.
L. Madhavan wrote:
sizeof(&Name) is the culprit
:doh: I missed that point. How ever if the Name is allocated in the stack, there is no problem in using
sizeof(Name)
.nave [OpenedFileFinder] [My Blog]
modified on Wednesday, December 17, 2008 2:34 AM
-
L. Madhavan wrote:
sizeof(&Name) is the culprit
:doh: I missed that point. How ever if the Name is allocated in the stack, there is no problem in using
sizeof(Name)
.nave [OpenedFileFinder] [My Blog]
modified on Wednesday, December 17, 2008 2:34 AM
-
But if Name is dynamically allocated on heap, sizeof(Name) and sizeof(&Name) both returns the size of pointer. Doesn't it ?
-Malli...! :rose:****
If Name is a plain string, use strlen instead. It is much safer. Note to self: take a morning coffee before answering questions... :sigh:
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++modified on Wednesday, December 17, 2008 3:17 AM
-
But if Name is dynamically allocated on heap, sizeof(Name) and sizeof(&Name) both returns the size of pointer. Doesn't it ?
-Malli...! :rose:****
-
If Name is a plain string, use strlen instead. It is much safer. Note to self: take a morning coffee before answering questions... :sigh:
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++modified on Wednesday, December 17, 2008 3:17 AM
strlen returns the current length of the string, not the actual size of the buffer. Moreover, if the buffer is uninitialized, (i.e. no null terminator) the behaviour of strlen is undefined.
-
strlen returns the current length of the string, not the actual size of the buffer. Moreover, if the buffer is uninitialized, (i.e. no null terminator) the behaviour of strlen is undefined.
Oh crap, I forgot to take my coffee this morning X| Yes, of course, we need to pass the size of the available buffer, not the size of the string...
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++