char* functions

This is a discussion on char* functions within the C++ Programming forums, part of the General Programming Boards category; First it seemed like a good idea, but I’m not so sure anymore. What will happen to the content/data this ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    10

    Question char* functions

    First it seemed like a good idea, but I’m not so sure anymore.

    What will happen to the content/data this function return?
    Will the content/data properly be deleted when the program terminates, or will it cause a memory leakage?

    Code:
    char* display(myenum type)
    {
    	switch (type) {
    		case type1:		return "Type 1"; break;
    		case type2:		return "Type 2"; break;
    	}
    }
    
    cout << "Type: " << display(enumvar);
    I guess in general it's the same thing as

    Code:
    char* foo = “This is a text”;
    Is this method by the book?

    What I’m trying to avoid is use the new/delete operation, because there is absolutely no reason why I need this value in a variable.
    My plan is to use this function in both writing to file and screen.

    Thanks!
    Last edited by muusle; 04-11-2007 at 08:56 AM.

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    The compiler takes care of memory for string constants, your function won't cause a memory leak.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    10
    Good, that’s what my intuition was telling me.

    So in other words, the string constant is a “special case” in the world of pointers because the program automatically allocates memory for you, in a “place” it’s taken care of when the program terminates.

    As far as I understand there is a difference between how C++ handles memory in global scope and local scope, such as inside a function.
    So if I call the display function inside another function, the data/content will still be handled properly on exit?


    Code:
    void anotherfunc()
    {
    	cout << "Type: " << display(enumvar);
    }
    Last edited by muusle; 04-11-2007 at 08:57 AM.

  4. #4
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    So in other words, the string constant is a “special case” in the world of pointers
    No, not at all. The rule is that if you allocate it, you free it. The compiler allocates it, so the compiler frees it. There's no special case involved.
    So if I call the display function inside another function, the data/content will still be handled properly on exit?
    You have to call the display function inside another function because it's not a part of any static object initialization. Regular functions can't be called at the global scope. But yeah, it'll still work the way you want.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    10
    Tanks!

    Hehe, right you are! When I said “special case” I was referring to case you weren’t allocating the memory, I guess it’s not that “special” indeed

    But of course, my bad, the transition from PHP can elude you from thinking strait!

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    String literals are a different special case, though: they are the only const arrays that can be converted to a non-const pointer.

    In other words, always assign string literals to a const char*, never a char*. Although the latter is supported for backwards compatibility, actually modifying the string is undefined behaviour and often leads to crashes, because the string literal might be in read-only memory. Still, that's a runtime error, and it's better to make the pointer const and catch the error at compile time.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    10
    Ah, I see.
    But how does this apply to the function returning a char pointer?

    If I were to assign the returning value to a variable (which I don’t want to do), I could do:

    Code:
    const char* foo = display(enumvar);

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    It applies in that the return type of display should be const char*.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    10
    Thanks for clarifying, Bee, much appreciated!

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    10
    I have one last question about this matter.
    In a larger program, which is potentially running over many days without being restarted: Will this cause a “temporary” memory leakage?
    And with temporary leakage I mean the allocated memory set aside for this const char*, which potentially doesn’t get freed until the program terminates.

    If this is the case, I can only imagine a graveyard of dead memory… until the program restarts and resurrect the fallen ones…

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    String literals occupy space just like the program code itself does: it's allocated when the program starts, and might be paged out by the OS if unused. The space definitely doesn't grow during the runtime of the program.
    I hope that answers your question, because I don't understand it exactly.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    And with temporary leakage I mean the allocated memory set aside for this const char*, which potentially doesn’t get freed until the program terminates.
    Yeah, if you want to call that a memory leak. It's not like the compiler allocates new memory for the string constant. If it's the same string, it'll probably have the same address and be reused everywhere you use the constant or a pointer to the constant. So even if you use the string constant in a thousand places, it all uses the same memory.

  13. #13
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Out of curiousity, is it guaranteed that in that context (a string constant within a function), the pointer will still be valid when referenced from outside of the function's scope? In situations like this, I've always assigned the strings to a static variable internally, and used that for returning.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  14. #14
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Yes, a string constant has static lifetime.

  15. #15
    Registered User
    Join Date
    Apr 2007
    Posts
    10
    Just for the fun of it:

    Code:
    const char* myStringconst()
    {
    	return "Foob";
    }
    
    int main()
    {
    	const char *sconst1 = myStringconst(); 
    	const char *sconst2 = myStringconst();; 
    
    	cout 	<< '\n' << &sconst1
    		<< '\n' << &sconst2;
    
    	const char *sconst3 = myStringconst();; 
    	const char *sconst4 = myStringconst();;
    	
    	cout 	<< '\n' << &sconst3
    		<< '\n' << &sconst4
    		<< '\n';
    
    	return 0;
    }
    
    /* OUT: 
    0012FF60	
    0012FF54	
    
    0012FF48	
    0012FF3C	
    */

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM

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