Thread: Segmentation fault when comparing dynamic string to literal

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include <string>
    
    int main()
    {
        std::string foo("Hello World");
        ...
        return 0;
    }
    The memory for string object foo exists in two places in this example. foo itself is on the stack and only occupies some small fixed sized bit of memory (regardless of how many characters are being managed). The characters themselves are on the heap... it is this part of the string object that grows and shrinks as needed and which the string object manages.

    It works kinda similar to this:
    Code:
    struct my_string
    {
        char * ptr;
    };
    
    int main()
    {
        my_string str;
    
        str.ptr = new char[12];
        strcpy(str.ptr,"Hello World");
        ...
        return 0;
    }
    The my_string variable called str exists on the stack in a manner similar to how the string object foo does. Also, the str variable has memory allocated to it's ptr member from the heap and data copied into this allocated memory similar to how the string object allocates memory and copies the data it manages. In the case of the string object, this allocated data can grow or shrink as needed as operations (concatenation of several strings for example) are made upon the string object.

    So... the string objects themselves are on the stack (unless you create the object itself dynamically) but are fixed and very tiny in the memory they occupy. The data they manage however exists on the heap and can be quite large (millions of characters or larger).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Your argument basically goes like this: "I'm declaring the string on the stack, therefore the memory occupied by the string data is on the stack."

    You seem to think this is a general argument, so let's apply it to another situation.

    Code:
    char *buffer = new char[128];
    The pointer buffer is a variable on the stack. Therefore the memory it references must be on the stack. Hopefully you agree that that's incorrect.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String assignment segmentation fault (core dump)
    By kapil1089thekin in forum C++ Programming
    Replies: 19
    Last Post: 08-07-2010, 12:51 AM
  2. Segmentation fault when changing a string
    By lilydjwg in forum C Programming
    Replies: 6
    Last Post: 12-02-2009, 07:43 AM
  3. Replies: 7
    Last Post: 07-18-2005, 08:43 AM
  4. segmentation fault when processing a string
    By Nakel in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 04:02 PM
  5. segmentation fault when processing a string
    By EMC2 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2003, 02:56 PM