• Advertisement

Archived

This topic is now archived and is closed to further replies.

C++ Returning char* not working

This topic is 5025 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm making a funtion to get the full pathname of a file. I can't get it to return the full thing:
char* GetFullPathname(char* pFilename)
{
	char* pDst;
	char Filename[MAX_PATH];
	char Drive[64], Directory[64];
	pDst = new char[MAX_PATH];

	GetModuleFileName(NULL, Filename, MAX_PATH);
	_splitpath(Filename, Drive, Directory, NULL, NULL);

	DfgStrApp(Filename, "%s%s%s", Drive, Directory, pFilename);
	DfgStrCpy(pDst, Filename);
	return pDst;
}
// Then call it

MessageBox(NULL, GetFullPathname("Enemies.cgf"), NULL, MB_OK);

// The second way

char* GetFullPathname(char* pFilename)
{
	char Filename[MAX_PATH];
	char Drive[64], Directory[64];

	GetModuleFileName(NULL, Filename, MAX_PATH);
	_splitpath(Filename, Drive, Directory, NULL, NULL);

	DfgStrApp(Filename, "%s%s%s", Drive, Directory, pFilename);
        return Filename;
}
// Then call it

MessageBox(NULL, GetFullPathname("Enemies.cgm"), NULL, MB_OK);
Doing it the first way works, but then I have pDst that doesn't get deleted. If I do it the second way, it just gives me "c:\Dragonfire G", which is only partially correct. Thanks for any help. /* I use DirectX 9 and C++ (Microsoft Visual C++ 6.0 Professional edition) */ [edited by - Programmer16 on April 16, 2004 2:36:00 PM]

Share this post


Link to post
Share on other sites
Advertisement
Three ways are typically used.

A) The function allocates space for the string, and returns the string. It is up to the caller of the function to deallocate that string.

B) The caller of the function first allocates a buffer, and passes the buffer into the function. The function then fills in the buffer. The caller then uses the buffer, and deallocates it when done.

C) Use std::string (or some other string class). Pass in either a reference to a string (as a buffer), or just return a string object (a string copy occurs). Either way, you don''t have to worry about any memory issues at all.

I highly recommend option C. Option B can have problems with buffer overflows, and with option A, it isn''t always intuitive that the caller should deallocate the memory.

Share this post


Link to post
Share on other sites
Alright, thanks. I'll go with option C (std::string saves me once again).

Edit:
It doesn't work exactly how I wanted, but it works great. I wanted to just be able to call it and return the value, but I just have to add ".c_str()" after the call:
MessageBox(NULL, GetFullPathname("Enemies.cgf").c_str(), NULL, MB_OK);
Thanks again for the help!

Edit2:
No, not a stupid question. I had to edit the code before I posted it and forgot to copy the return statement. It's supposed to return Filename.

/*
I use DirectX 9 and C++ (Microsoft Visual C++ 6.0 Professional edition)
*/

[edited by - Programmer16 on April 16, 2004 2:33:37 PM]

[edited by - Programmer16 on April 16, 2004 2:35:22 PM]

Share this post


Link to post
Share on other sites
dumb question on my part (I think), but what variable is the second way supposed to return?

There seems to be a return statement missing...

Share this post


Link to post
Share on other sites
What are you even doing in the second function? You never return a value...so the return is undefined anyway.

There's a fourth way.

static char output[MAX_PATH];

Then return that. You will probably want to make a copy of the output first, as it will change the next time you call the function. However, I'd probably go with Agony's option C instead.

-Madgap

[edited by - Madgap on April 16, 2004 2:38:20 PM]

Share this post


Link to post
Share on other sites
Just thought of another option . You could declare a global char * variable and allocate space at the start of your program. Just fill that variable from within your function and read from it after the function returned. Nonetheless, DON''T USE THIS METHOD! Why am I posting this, you might ask? Well, I just wanted to make sure you don''t do this, because this will lead to many problems...


Indeterminatus

--si tacuisses, philosophus mansisses--

Share this post


Link to post
Share on other sites
Yeah, doing it with the static is basically a better way of taking the global approach - same kind of semantics for the storage space, except that you make sure it can''t be touched inappropriately. (Hmm that sounded wrong :/ )

Share this post


Link to post
Share on other sites

  • Advertisement