Click here to Skip to main content
15,906,106 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Malloc fails...why? Pin
damir_tk12-Nov-06 9:55
damir_tk12-Nov-06 9:55 
GeneralRe: Malloc fails...why? Pin
Mark Salsbery12-Nov-06 10:27
Mark Salsbery12-Nov-06 10:27 
damir_tk wrote:
1. Why malloc sucks, I don't see any problems with it. New and delete is better when you are creating some objects that have a constructor and a destructor, as they get called automatically, but not if you use malloc. But here I only use strings and string pointers, so why?


I didn't say it sucks Wink | ;) You're using C++ so why not take advantage of the much stronger type
safety provided by the language? Using "new", you create a typed pointer so the compiler can
help point out mistakes during development because it knows the pointers type without having
to use the old C-style cast. Use what you want, of course, but I bet you'll find new and delete
much more pleasing to use.

damir_tk wrote:
2. I already rewrite the code, to use something like:

lpwFolder = (LPWSTR) malloc( (len + 1) * 2 );

Is it wrong? (Apart from not being elegant :=) ).

3. I use _tcslen instead of wcslen, is it better suited here?


The only thing I see that's not elegant is mixing the generic "T" types with the hard-coded "W"
types. By using the "T" types you have the advantage of your code working for both UNICODE and
non-UNICODE builds. It also fits the way the Windows APIs are declared.
If you know you are only ever going to use UNICODE then you could just use the "W" string types.
It's just easier to read the code if you stick to one set of types and functions.
This is all just my opinions of course. Smile | :)

Here's another version of your code using all "T" char types and new/delete...
LPTSTR FileHelper::GetFileFullPath( LPCTSTR lpwName )
{
	LPTSTR lptFileFullPath;
	DWORD LenInChars = ::GetCurrentDirectory( 0, NULL );
	++LenInChars; // add 1 for NULL terminator
	LPTSTR lpwFolder = new TCHAR[ LenInChars ];
	if ( lpwFolder == NULL )
	{
		return NULL;
	}
	if ( ::GetCurrentDirectory( LenInChars, lpwFolder ) != 0 )
	{
		// I added 6 like you did before...you may need more chars for backslashes and such
		LenInChars = (DWORD)( _tcslen( lpwFolder ) + _tcslen( lpwName ) + 6 );
		lptFileFullPath = new TCHAR[ LenInChars ];
		if ( lptFileFullPath == NULL )
		{
			// prevent memory leak
			delete[] lpwFolder;
			return NULL;
		}
		// ...the rest of the code stripped off...
	}
	// prevent memory leak
	delete[] lpwFolder;
	return lptFileFullPath;
}

GeneralRe: Malloc fails...why? Pin
damir_tk12-Nov-06 10:42
damir_tk12-Nov-06 10:42 
Questionproblem with colour the radio & check box in property page [modified] Pin
vikas amin12-Nov-06 0:54
vikas amin12-Nov-06 0:54 
AnswerRe: problem with colour the radio & check box in property page Pin
Rajesh R Subramanian12-Nov-06 19:39
professionalRajesh R Subramanian12-Nov-06 19:39 
GeneralRe: problem with colour the radio & check box in property page Pin
vikas amin12-Nov-06 20:21
vikas amin12-Nov-06 20:21 
GeneralRe: problem with colour the radio & check box in property page Pin
Rajesh R Subramanian12-Nov-06 20:43
professionalRajesh R Subramanian12-Nov-06 20:43 
GeneralRe: problem with colour the radio & check box in property page Pin
vikas amin12-Nov-06 21:14
vikas amin12-Nov-06 21:14 
Question"How can I pass jbyte * from C++ to VB" Pin
Orchid8512-Nov-06 0:46
Orchid8512-Nov-06 0:46 
Questioninterface in background Pin
Johpoke12-Nov-06 0:42
Johpoke12-Nov-06 0:42 
AnswerRe: interface in background Pin
Sceptic Mole12-Nov-06 6:16
Sceptic Mole12-Nov-06 6:16 
AnswerRe: interface in background Pin
Mike_V12-Nov-06 15:10
Mike_V12-Nov-06 15:10 
GeneralRe: interface in background Pin
Johpoke13-Nov-06 8:17
Johpoke13-Nov-06 8:17 
GeneralRe: interface in background Pin
Mike_V13-Nov-06 8:43
Mike_V13-Nov-06 8:43 
GeneralRe: interface in background Pin
Johpoke13-Nov-06 8:51
Johpoke13-Nov-06 8:51 
GeneralRe: interface in background Pin
Johpoke13-Nov-06 8:52
Johpoke13-Nov-06 8:52 
GeneralRe: interface in background Pin
Mike_V13-Nov-06 12:20
Mike_V13-Nov-06 12:20 
GeneralRe: interface in background Pin
Johpoke14-Nov-06 6:21
Johpoke14-Nov-06 6:21 
GeneralRe: interface in background Pin
Johpoke14-Nov-06 9:09
Johpoke14-Nov-06 9:09 
Questionhow to generate key for using in AES algo Pin
sharad puttar12-Nov-06 0:22
sharad puttar12-Nov-06 0:22 
AnswerRe: how to generate key for using in AES algo Pin
Mark Salsbery12-Nov-06 8:02
Mark Salsbery12-Nov-06 8:02 
QuestionDetecting mouse buttons Pin
Waldermort11-Nov-06 23:55
Waldermort11-Nov-06 23:55 
AnswerRe: Detecting mouse buttons Pin
Mike_V12-Nov-06 15:23
Mike_V12-Nov-06 15:23 
QuestionWant help,please. Pin
toeh11-Nov-06 23:04
toeh11-Nov-06 23:04 
AnswerRe: Want help,please. Pin
Hamid_RT11-Nov-06 23:13
Hamid_RT11-Nov-06 23:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.