Thread: Pointer to String and memory status

  1. #1
    Registered User
    Join Date
    Jul 2008

    Question Pointer to String and memory status


    I'm studying C and I have a question concerning the use of strings in C.

    If I have the following code:

    char *string;

    string = "Hello";
    string = "I have a question";

    What's happens with the reserved memory for the string?
    - In the first atribution (string = "Hello") we will reserve memory for this string
    - In the second atribution what's happens with the memory? Is it the previous memory space for the previous string deleted and a new one is created?

    Thanks for any help

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    The only memory "reserved" for string is the size of a pointer. When you do string = "Hello", then you set that pointer to point to a piece of memory that holds 'H', 'e', 'l', 'l', 'o', '\0'. Next when you set it to a different string, the pointer is set to a different memory location, where the new string is. The "Hello" string will still remain where it was in memory - you just have no way to find out were that is.

    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
    Jul 2008
    ahh ok I understood.
    Thanks for your quick and nice answer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM