0

I have inherited the following line of code:

TCHAR temp[300];
GetModuleFileName(NULL, temp, 300);

However, this fails as the first 3 bytes are filled with garbage values (always the same ones though, -128, -13, 23, in that order). I said, well fine and changed it to:

TCHAR temp[300];
ZeroMemory(temp, 300);
GetModuleFileName(NULL, temp, 300);

but the garbage values persisted! Note that after the ZeroMemory() call, all other bytes were zeroed out properly and after GetModuleFileName(), the directory was stored in the buffer properly. It's as if temp was being replaced by temp+3. Could this have something to do with word boundaries?

Can someone explain what is going on and how to fix it?

samoz
  • 56,849
  • 55
  • 141
  • 195
  • And what do you see after these three garbage values? Anything meaningful? – AnT stands with Russia May 20 '10 at 18:12
  • After ZeroMemory() it is full of zero's and after GetModuleFileName() it has the directory, as it should. I'll edit post to reflect that. – samoz May 20 '10 at 18:13
  • 1
    Maybe those are meaningful characters? – GManNickG May 20 '10 at 18:15
  • Regardless, they should have been zeroed out though overwritten shouldn't they? – samoz May 20 '10 at 18:16
  • 3
    This is probably a problem with the debugger not displaying the correct memory contents for some reason. Are you running an optimized build? Did you try outputting the contents of `temp` in your program? – interjay May 20 '10 at 18:28
  • 1
    unrelated but ZeroMemory should be passed 300 * sizeof(TCHAR) whose result depends on whether you compile as unicode or not – sean e May 20 '10 at 18:28
  • Er... Are you saying that immediately after `ZeroMemory` (and before `GetModuleFileName` call) the garbage values were already there? – AnT stands with Russia May 20 '10 at 18:35
  • I'm saying there were there both before and after the ZeroMemeory() call. – samoz May 20 '10 at 18:41
  • Why not just do `TCHAR temp[300] = {};`? – GManNickG May 20 '10 at 19:39
  • Are you sure that there is really a problem? I suspect you are debugging a release build which has various optimizations turned on that screw up the debugging symbols. I would suggest checking the return value of GetModuleFileName and printing the value of temp to the screen or a file. – Luke May 20 '10 at 19:49
  • Again unrelated, but ZeroMemory(temp, sizeof(temp)) is better, that is if temp is defined as an array (e.g. temp[100])... that way if type or size of temp changes, the call to ZeroMemory can stay as it is. – santahopar Dec 05 '14 at 00:33

1 Answers1

1

ZeroMemory works in terms of bytes, whereas you have an array of 300 TCHARs. This makes me assume you're working with widechar (not multi-byte) compilation option.

You should use:

ZeroMemory(temp, 300 * sizeof(TCHAR));

Or in your specific case:

ZeroMemory(temp, sizeof(temp));

However be careful with the latter. It's applicable if temp is an automatic array whose declaration is visible within the function. If it's a pointer whose allocation size is "invisible" for the compiler - sizeof will give just the size of the pointer.

valdo
  • 12,632
  • 2
  • 37
  • 67