Concatenating a stack string with a heap string gives odd results?

There are lots of problems with your code. Let's take it apart, shall we.

There are lots of problems with your code. Let's take it apart, shall we: _TCHAR* whatTheHeck(_TCHAR* name) // We're inside a local scope { _TCHAR BufferBUFSIZE; // "Buffer" has automatic storage _TCHAR* what = new _TCHARBUFSIZE; // "what" points to newly allocated dyn. Memory what = _tcscat(Buffer, TEXT("\\")); // Oh no, we overwrite "what" - leak!

// Now what == Buffer. What = _tcscat(what, name); // Equivalent to "_tcscat(Buffer, name)" return Buffer; // WTPF? We're returning a local automatic!

} As you can see, you are both causing a memory leak with a gratuitious and reckless new, and you are also returning the address of a local object past its lifetime! I would strongly recommmend reading the documentation for strcat and understanding "source" and "destination", not using strcat, but a safer version like strncat, not using strncat, but instead std::string.

That happens because _tcscat concatenates into the destination parameter, which is the first one, and then returns it. So, it returns a pointer to the array Buffer and it gets stored in what in this line: what = _tcscat(Buffer, TEXT("\\")); Then this pointer is returned, and you have undefined behavior once you try to use it, because the local Buffer no longer exists. Furthermore, the line above also causes the memory allocated for what to be leaked: _TCHAR* what = new _TCHARBUFSIZE; what = _tcscat(Buffer, TEXT("\\")); // this loses the memory allocated // in the previous line.

TCHAR* what = new _TCHARBUFSIZE; what = _tcscat(Buffer, TEXT("\\")); Aren't you overwriting what with the Buffer which is a local variable to the function. Once the stack is unwound, Buffer goes out of scope and thus you get unexpected values. Also it's a memory leak.In heap allocation scenarios, you may prefer to declare the pointer a const to avoid such hazards: _TCHAR* const what = new _TCHARBUFSIZE; ^^^^^ (avoids overwriting) The better approach is to use std::string and be out of such small issues.

The dynamically allocated memory what points to is not initialized. It contains gibberish. _tcscat expects the string to be properly null-terminated.

_TCHAR* what = new _TCHARBUFSIZE(); This fills what with '\0' characters. _TCHAR* what = new _TCHARBUFSIZE; what0 = '\0'; This properly terminates the empty string. GetCurrentDirectory doesn't expect the buffer to be null-terminated.It writes something to that and properly null-terminates it.

You can then pass that to concatenation functions. As a side note, your function seems to be vulnerable to buffer overflows. Apparently you allow GetCurrentDirectory to fill up everything you allocated, and then you want to append to it without caring if there is any more space left.

The allocated memory what points to is leaked and not the cause of the gibberish. – R. Martinho Fernandes Jul 29 at 10:46 I assumed _tcscat works like strcat in which case assignments of the result should be ok, though redundant (the destination pointer is not modified anyway).

– visitor Jul 29 at 10:52 It works like strcat. – R. Martinho Fernandes Jul 29 at 10:53 @Martinho: I missed that OP's passing Buffer the first time.

Can't imagine someone managing C strings without any idea how the C string functions work... – visitor Jul 29 at 10:59 7 @visitor: It's called learning. – Saul Jul 29 at 11:09.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions