Click here to Skip to main content
15,881,757 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Memory deallocation Pin
Andrew Brock2-Feb-11 4:32
Andrew Brock2-Feb-11 4:32 
QuestionRe: Memory deallocation Pin
csrss2-Feb-11 4:53
csrss2-Feb-11 4:53 
AnswerRe: Memory deallocation Pin
Aescleal2-Feb-11 4:49
Aescleal2-Feb-11 4:49 
GeneralRe: Memory deallocation Pin
csrss2-Feb-11 5:21
csrss2-Feb-11 5:21 
GeneralRe: Memory deallocation Pin
Aescleal2-Feb-11 5:54
Aescleal2-Feb-11 5:54 
GeneralRe: Memory deallocation Pin
csrss2-Feb-11 6:16
csrss2-Feb-11 6:16 
Answer... [modified] Pin
csrss2-Feb-11 6:46
csrss2-Feb-11 6:46 
GeneralRe: ... Pin
Andrew Brock2-Feb-11 16:54
Andrew Brock2-Feb-11 16:54 
That is looking good, just a few things I would like to mention:

1. When you modify the value of a parameter, you are only modifying its value within the scope of the function, unless the parameter is passed by reference or you change the value of the pointed to value with a dereference like *p = NULL;
__declspec(dllexport)void FreeMem(void *Ptr) {
	if(Ptr != NULL) {
		free(Ptr);
		//Ptr = NULL; //This line does nothing, and with optimisations enabled won't even get compiled into the dll.
	}
}


2. sizeof(wchar_t *) * wcslen(String) + 1 is wrong for a few reasons when allocating space for the string.
The +1 needs to be multiplied by the size of each character as well sizeof(wchar_t *) * (wcslen(String) + 1)
Secondly, you need to multiply the number of characters by the size of each character, not the size of the pointer to each character sizeof(wchar_t) * (wcslen(String) + 1)

3. If you allocate a string as an array of wchar_t's rather than a pointer you can get the compiler to hard-code the size in, rather than having to calculate it at runtime.
This only works when the string is known at compile time, as it is in your case.
strlen is a somewhat slow function, it must iterate through every character in the string, check the value of the character and increment a counter. While this is ok if it only happens occasionally, you should prefer to use sizeof() when possible as it gets the compiler to do all this for you.
A similar principal applies for strcpy but to a lesser extent. memcpy can copy the data in chunks rather than 1 character at a time
__declspec(dllexport)void Stuff(wchar_t *&out) {
	wchar_t String[] = L"test string whatever"; //we can now use sizeof() to get the string length at compile time
	//sizeof(String) == sizeof(wchar_t) * (wcslen(String) + 1)
	out = (wchar_t *)malloc(sizeof(String));
	memcpy(out, String, sizeof(String)); //memcpy is faster than strcpy
}


Since you are iterating over this 10,000,000 times (at least in your example) you will notice an increase in performance

Finally, it would be better to return the pointer from Stuff()
__declspec(dllexport)wchar_t *Stuff() {
	wchar_t String[] = L"test string whatever";
	wchar_t *out = (wchar_t *)malloc(sizeof(String));
	memcpy(out, String, sizeof(String));
	return out;
}

[DllImport("testdll.dll")]
public static extern IntPtr Stuff();

GeneralRe: ... Pin
csrss2-Feb-11 22:41
csrss2-Feb-11 22:41 
AnswerRe: Memory deallocation Pin
Stefan_Lang3-Feb-11 6:38
Stefan_Lang3-Feb-11 6:38 
Question[REQUEST] TabControl Sample Code (Visual C++, Win32, Win32 Project) with Tutorial on what to edit to customize it Pin
Curtis Tong1-Feb-11 23:38
Curtis Tong1-Feb-11 23:38 
GeneralRe: [REQUEST] TabControl Sample Code (Visual C++, Win32, Win32 Project) with Tutorial on what to edit to customize it Pin
Malli_S1-Feb-11 23:57
Malli_S1-Feb-11 23:57 
AnswerRe: [REQUEST] TabControl Sample Code (Visual C++, Win32, Win32 Project) with Tutorial on what to edit to customize it Pin
Malli_S2-Feb-11 0:04
Malli_S2-Feb-11 0:04 
GeneralRe: [REQUEST] TabControl Sample Code (Visual C++, Win32, Win32 Project) with Tutorial on what to edit to customize it Pin
Andrew Brock2-Feb-11 0:51
Andrew Brock2-Feb-11 0:51 
GeneralRe: [REQUEST] TabControl Sample Code (Visual C++, Win32, Win32 Project) with Tutorial on what to edit to customize it Pin
Malli_S2-Feb-11 0:57
Malli_S2-Feb-11 0:57 
AnswerRe: [REQUEST] TabControl Sample Code (Visual C++, Win32, Win32 Project) with Tutorial on what to edit to customize it Pin
Andrew Brock2-Feb-11 0:51
Andrew Brock2-Feb-11 0:51 
QuestionSong and video Parsing Pin
VikramRathod1-Feb-11 22:30
VikramRathod1-Feb-11 22:30 
AnswerRe: Song and video Parsing Pin
Cedric Moonen1-Feb-11 23:12
Cedric Moonen1-Feb-11 23:12 
AnswerRe: Song and video Parsing Pin
Emilio Garavaglia2-Feb-11 1:59
Emilio Garavaglia2-Feb-11 1:59 
JokeRe: Song and video Parsing Pin
Cool_Dev2-Feb-11 2:16
Cool_Dev2-Feb-11 2:16 
GeneralRe: Song and video Parsing Pin
VikramRathod10-Feb-11 20:15
VikramRathod10-Feb-11 20:15 
Questionhow do i sync two places.sqlite..? Pin
yogish2931-Feb-11 19:58
yogish2931-Feb-11 19:58 
AnswerRe: how do i sync two places.sqlite..? Pin
User 74293381-Feb-11 20:13
professionalUser 74293381-Feb-11 20:13 
GeneralRe: how do i sync two places.sqlite..? Pin
yogish2931-Feb-11 20:17
yogish2931-Feb-11 20:17 
AnswerRe: how do i sync two places.sqlite..? Pin
Niklas L1-Feb-11 21:20
Niklas L1-Feb-11 21:20 

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.