Text (const char*)

This is a discussion on Text (const char*) within the C Programming forums, part of the General Programming Boards category; Usually, variables are deleted when out of scope. Code: int *pointer; { int number = 5; pointer = &number; } ...

  1. #1
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105

    Text (const char*)

    Usually, variables are deleted when out of scope.
    Code:
    int *pointer;
    {
      int number = 5;
      pointer = &number;
    }
    // now, outside the brackets, pointer points to freed memory and should not be used.
    Am I right so far?


    Text have the type (const char*), so I figured that text are arrays of chars stored in memory, and that (const char*) points to the first element.

    That's all good.
    But take a look at this code:
    Code:
    const char *pointer;
    {
      const char *text = "This is a text";
      pointer = text;
    }
    // What about pointer(outside of brackets) here?
    Isnt text freed when out of scope in C?
    If so, is this because text-constants are created using malloc, behind the scences?
    And if so, If i were to create a program with LOTS of text, wouldnt I need to manually free memory taken from text-constants?
    Last edited by Drogin; 12-23-2008 at 11:27 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Literal constants are normally not "freed" - if you had
    Code:
    char text[] = "Something";
    then the storage for the text is lost once you leave the block [or some time later on, but certainly by the end of the function - there is no guarantee that the end of a block ACTUALLY frees the memory - if it was C++ and an object was leaving it's scope, the destructor would be called at that point, however].

    Edit: A related case would be:
    Code:
    const char *somefunc()
    {
       return "Hello";
    }
    Obviously, "Hello" doesn't disappear when the function returns. Your original code is essentially just finding the pointer of a constant string, and then assigning a different pointer with the address of the constant string - it's perfectly legal and an acceptable thing to do.

    --
    Mats
    Last edited by matsp; 12-23-2008 at 11:33 AM.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,057
    Quote Originally Posted by Drogin View Post
    Usually, variables are deleted when out of scope.
    scope of a variable is related to its visibility in the various functions and/or source files that makeup the entire program.
    Quote Originally Posted by Drogin View Post
    Code:
    int *pointer;
    {
      int number = 5;
      pointer = &number;
    }
    // now, outside the brackets, pointer points to freed memory and should not be used.
    Am I right so far?
    freed is a misnomer in this case as pointer is an automatic variable (if local to a function); it exists until overwritten.
    Quote Originally Posted by Drogin View Post
    Text have the type (const char*), so I figured that text are arrays of chars stored in memory, and that (const char*) points to the first element.

    That's all good.
    But take a look at this code:
    Code:
    const char *pointer;
    {
      const char *text = "This is a text";
      pointer = text;
    }
    // What about pointer(outside of brackets) here?
    Isnt text freed when out of scope in C?
    to say freed would be incorrect, the variable still exists but gets overwritten by another function invocation.
    Quote Originally Posted by Drogin View Post
    If so, is this because text-constants are created using malloc, behind the scences?
    And if so, If i were to create a program with LOTS of text, wouldnt I need to manually free memory taken from text-constants?
    it is the compiler's job to store the string literals in memory and the user program simply gets a pointer to its first character.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    hmm... I tried searching for this in the 1999 edition of the C Standard but did not get a definitive answer that I could understand. On the other hand, Herb Sutter's GotW #9: Memory Management - Part I states that "the const data area stores string literals and other data whose values are known at compile time. No objects of class type can exist in this area. All data in this area is available during the entire lifetime of the program."

    Now, I have read criticism that Sutter's use of terms in that article may be biased/misleading/etc, but if he is correct on that point, then I suspect that the string literal is not destroyed, even though the text pointer itself goes out of scope.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    Okay, to make some people get a better veiw of what I mean...and to make them focus on the thing I'm actually asking for, not my vocabulary:

    Take a look at this code:
    Code:
    const char *pointer;
    {
      pointer = "This is a text";
    }
    // Would it still be legal and safe to assume pointer still reffer to 
    // "This is a text"?(outside of brackets) 
    // I would guess so, but that would mean text-constants are 
    // guaranteed not to get overwritten even when out of scope.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    Quote Originally Posted by Drogin
    // Would it still be legal and safe to assume pointer still reffer to "This is a text"?(outside of brackets) I would guess so, but that would mean
    // text-constants are guaranteed not to get overwritten even when out of scope.
    Based on what I understand and on Herb Sutter's article, I would say that yes, the lifetime of a string literal is the lifetime of the program, so pointer still refers to the string literal.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Preamble: I am not a language lawyer, so I do not know what the language standard actually says in this matter.

    Answer: I would have no qualms about writing something like that myself. In fact I have often used things similar to that in my code - in particular setting a pointer to a constant and then returning the resulting string to another function.

    It is possible that some compilers, under very special circumstances, may decide that a string is no longer needed, and discard it. A particular case that I can think of was in Turbo Pascal, where you had overlays, and a constant string in one module would be replaced with the code of another function when the second function got called - and any constant in the original code was now replaced along with the function itself. That's not C or C++, but I guess there may be similar features in some compilers (perhaps for embedded use) that do the same thing.

    --
    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.

  8. #8
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    Interesting.

    What happens at this line?
    Code:
    char test[] = "This is a test";
    Does this create a char-array and copies each char from the (const char*)"This is a test", to the array?

    Or does the "test" variable simply become a pointer to the (const char*)"This is a test" ?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    Quote Originally Posted by Drogin
    Does this create a char-array and copies each char from the (const char*)"This is a test", to the array?
    Yes, at least in terms of how you can think of it.

    Quote Originally Posted by Drogin
    Or does the "test" variable simply become a pointer to the (const char*)"This is a test" ?
    No, which is why it would not be wrong to write what you wrote instead of:
    Code:
    const char test[] = "This is a test";
    Note that test really is an array, not a pointer, though it is easily converted to a pointer to its first element.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,057
    Quote Originally Posted by Drogin View Post
    Interesting.

    What happens at this line?
    Code:
    char test[] = "This is a test";
    Does this create a char-array and copies each char from the (const char*)"This is a test", to the array?
    Yes this is an array
    Quote Originally Posted by Drogin View Post
    Or does the "test" variable simply become a pointer to the (const char*)"This is a test" ?
    The pointer example you gave in your previous post is an example of the const char*

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Drogin View Post
    Interesting.

    What happens at this line?
    Code:
    char test[] = "This is a test";
    Does this create a char-array and copies each char from the (const char*)"This is a test", to the array?

    Or does the "test" variable simply become a pointer to the (const char*)"This is a test" ?
    Laserlight has answered the exact questions above, but this is definitely a case where returning or otherwise referring to the address of "test" outside of the scope is not going to work out in all circumstances.
    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.

  12. #12
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    Cool. I understand it a lot better now.
    And I see why so many people preffer the string-class in C++

    the mixing of (const char *), char[] and when they arent guaranteed to not to be overwritten, seems to make it quite error-prone, unless you're very careful.

  13. #13
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, you can always try and doing something more safe. Like using strcpy to copy a string literal to an array of char, so you don't wonder what happens with the string literal.
    A C++ string would do the same. It will copy the string literal. This will work always:
    Code:
    char string[10];
    strcpy(string, "Hey dude");
    The advantage of C++ string though is that you don't have to worry about the size. If you give a string litteral greater than 10 you would get an error. But otherwise the string won't get discarded by any compiler

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Quote Originally Posted by itCbitC View Post
    scope of a variable is related to its visibility in the various functions and/or source files that makeup the entire program.
    Is it really?
    I suspect it is more than that. Variables that go out of scope are destroyed in the eyes of the language.

    freed is a misnomer in this case as pointer is an automatic variable (if local to a function); it exists until overwritten.
    Again, incorrect. The variable is indeed destroyed, in the eyes of the language.
    Whatever else happens is up to the OS. In case of Windows, it is as you say.

    to say freed would be incorrect, the variable still exists but gets overwritten by another function invocation.
    Same as above.

    Quote Originally Posted by C_ntua View Post
    The advantage of C++ string though is that you don't have to worry about the size. If you give a string litteral greater than 10 you would get an error. But otherwise the string won't get discarded by any compiler
    Correction: undefined behavior.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    Quote Originally Posted by Elysia
    Is it really?
    I suspect it is more than that. Variables that go out of scope are destroyed in the eyes of the language.
    I would differentiate between scope and lifetime. For variables with automatic storage duration scope and lifetime is the same, but say, a static variable in a function may exist at function scope, but its lifetime could exceed that of the function call.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help implementing a class
    By jk1998 in forum C++ Programming
    Replies: 8
    Last Post: 04-05-2007, 04:13 PM
  2. fatal error LNK1104
    By DMH in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2005, 03:46 AM
  3. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM

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