Re: Returning a CHAR[] from a function




"Tim" <tnorton@xxxxxxxxxxx> wrote in message
news:1156773731.740360.65920@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
LPCTSTR CAutoRunDlg::GetProgramPath(void)
{
char buffer[_MAX_PATH]; //declares maimum path

return buffer;
}

Your function is incorrect. You are returning a pointer to a local variable
which goes out of scope as soon as the function has finished. As Vipin told
you earlier, you could make this variable static in order to allow you to
reference it beyond the scope of the function. Otherwise, what you are
doing is incorrect and will cause your code to fail sooner or later in
mysterious ways -usually in a release build!

As you were told earlier, the better solution is to return an object such as
std::string or CString rather than returning a pointer to a buffer.

-Pete


.



Relevant Pages

  • [net-next PATCH 4/5] igb: Add support for enabling VFs to PF driver.
    ... pointer to the hardware struct ... under the terms and conditions of the GNU General Public License, ... * @msg: The message buffer ... * returns SUCCESS if it successfully copied message into the buffer ...
    (Linux-Kernel)
  • [patch] x86, ptrace: PEBS support
    ... BTS and PEBS recording. ... * - buffer overflow handling ... (interrupt occurs when write pointer passes interrupt pointer) ... * guarding context and buffer memory allocation. ...
    (Linux-Kernel)
  • Re: The coming death of all RISC chips.
    ... different area of memory than the "stack", ... Any arbitrarily indirect pointer to code that can be ... overwritten by a buffer overflow is a potential hole (and an actual ... ways to exploit buffer overflows. ...
    (comp.arch)
  • Re: Simple C containers, std::vector analog
    ... the computing power of thiry ... Thirty years ago it was 1975 and computers ran in Kilohertz ... and some implementations do choke on realloc'ing a null pointer. ... template f void f(voidpT buffer){ ...
    (comp.lang.c)
  • Re: some unanswered questions on C
    ... A pointer variable that's never been given a value. ... you don't know what memory you're modifying. ... >what i want to ask is that when i declare my buffer for fgets as ... "char *buffer" creates a pointer, ...
    (comp.unix.programmer)