Jump to content
  • Advertisement
Sign in to follow this  
Brocketino

Local address

This topic is 4970 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

This is a silly question i guess:) If i have a method like this one -> char * GetTitle(HWND hwnd) { char szTitle[255]; memset(szTitle,0,255); GetWindowText(hwnd,szTitle,255); return szTitle; } int WINAPI WinMain(...) { ... char * szWindowTitle = GetTitle(g_hwnd); ... } I receive the following when compiling -> warning C4172: returning address of local variable or temporary' Is there a more appropriate way of doing this?

Share this post


Link to post
Share on other sites
Advertisement
use std::string instead to make your life easier.

#include <string>

std::string GetTitle(HWND hwnd)
{
char szTitle[255];
memset(szTitle,0,255);
GetWindowText(hwnd,szTitle,255);
std::string Title = szTitle;
return Title;
}


There may be an even better way to do that though, but that should be a start. I can't think of how you would use all std::strings with that function. You could try:

std::string GetTitle(HWND hwnd)
{
std::string Title
Title.resize(255);
GetWindowText(hwnd,(char*)&Title,255);
return Title;
}


But I do not know (nor think) that it will work correctly.

Share this post


Link to post
Share on other sites
I'd just like to point out that this code:

(char*)&Title

is one of those lovely 'undefined behaviour' pieces of code.

There is no guarantee that std::string stores it's data as a contiguous array of char, and there is no guarantee that you'll find a C-style delimiter at the end of it.

To pass std::strings to code needing C-style char* arrays, use:

Title.c_str()

To answer the OP:

In your GetTitle function, you are defining szTitle locally; this means it only exists within the scope of that function; once the function exits, it's goodbye szTitle, as it's popped off the stack. Therefore, you are returning a pointer to a piece of data that no longer exists - the char* pointer you are returning from GetTitle doesn't point at something (or, if you're unlucky, points at something you really don't want it to....).

So, what are the options. Well, the simplest is to note that the only thing your function adds over just changing your code to:

char szTitle[255];
szTitle = GetWindowText(g_hwnd, szTitle, 255);

is a call to memset. So is the function even needed? Alternatively, you could simply store the windowname at global scope, or even better, within a class or struct. If you want something to persist within that function, you could try:


char* GetTitle(HWND hwnd)
{
static char szTitle[255];
GetWindowText(hwnd, szTitle, 255);
return &szTitle;
}



It's still a char-array, which is a bit of a pain in the arse, so Drew_Bentons solution is nicer (if you're using C++ and not C) - so long as you remember the caveat about .c_str() above.

And finally - as Drew_Benton mentions - there is no nice and easy way to read straight into a string from a function that wants to return a char* array (or if there is, feel free to let me know!).

So - in summary - the best solution is probably to use Drews original code, or just drop the function completely.

HTH,
Jim.

Share this post


Link to post
Share on other sites
Quote:
char* GetTitle(HWND hwnd)
{
static char szTitle[255];
GetWindowText(hwnd, szTitle, 255);
return &szTitle;
}


Danger!

char* title1 = GetTitle(foo);
char* title2 = GetTitle(bar);

assert(title1 == title2);


Bit me in the ass in my MUD programming days.

I'm all for dropping the function.

Share this post


Link to post
Share on other sites
Quote:

Danger!


Good point!

When I wrote that I wasn't even sure if you could take the address of a function-static object, so I was just playing around with it in VS - hadn't thought of that implication.

Nice to know though, that if I'm going to make a mistake, it's not so obvious that others haven't made it before.

Ta,
Jim.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!