Return string issue

This is a discussion on Return string issue within the C Programming forums, part of the General Programming Boards category; I have an interesting problem here. The function returns the first cell of a local array, so I am getting ...

  1. #1
    Registered User philvaira's Avatar
    Join Date
    Apr 2004
    Posts
    72

    Return string issue

    I have an interesting problem here. The function returns the first cell of a local array, so I am getting this: returning address of local variable or temporary. How can I return a new string without this message?

    Code:
    const char *X3DConsole_GetTitle()
    { 
    	char strTitle[80]; 
    
    	GetConsoleTitle(strTitle, 80); 
    
    	return strTitle;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    One way would be to allocate "strTitle" on the heap instead of on the stack.

  3. #3
    Registered User philvaira's Avatar
    Join Date
    Apr 2004
    Posts
    72
    Good idea. I changed my code to the following. If I'm using malloc, I'd have to call free though. I'd like to do it within the function so I'm not calling free() outside of it.

    Another solution is to make strTitle a static local variable. I'm not sure if this is a good solution though.

    Code:
    const char *X3DConsole_GetTitle()
    { 
    	char *strTitle = NULL;
    
    	strTitle = (char *)malloc(sizeof(char) * 80);
    
    	GetConsoleTitle(strTitle, 80); 
    
    	return strTitle;
    }

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by philvaira View Post
    Good idea. I changed my code to the following. If I'm using malloc, I'd have to call free though. I'd like to do it within the function so I'm not calling free() outside of it.
    If you're not changing "strTitle" in the calling function, then you can call free() in there too.
    Quote Originally Posted by philvaira View Post
    Another solution is to make strTitle a static local variable. I'm not sure if this is a good solution though.
    That would work just fine.
    Last edited by itCbitC; 05-25-2009 at 02:06 AM.

  5. #5
    Registered User philvaira's Avatar
    Join Date
    Apr 2004
    Posts
    72
    I'll stick with the static solution since it is a little cleaner. Thanks

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Actually, my preferred solution is to pass in an already existing array to store the string in. You should then also pass in a size, so that you can deal with "it may not fit" problems.

    The benefit of passing it into the function is that the calling function can decide where to put it, and there is no reason to copy the string again to get it into it's final destionation - e.g. you want to store it into a struct, then that would work better by calling:

    Code:
    X3DConsole_GetTitle(mystruct.title, sizeof(mystruct.title));
    The static solution is not very flexible, the temptation is to store the pointer directly - in which case the content will change "behind the scenes" if you call the function again [assuming it doesn't always return the same thing!]. It also goes to pot if you start using threads.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    ITA with matsp since the purpose of static variables is to retain their values between function calls instead of communicating data between functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21