Thread: Char / string Question

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    Char / string Question

    Now I like the way C++ handles strings; I find parsing so much easier in every way.

    But...

    Here's the way I handle strings and such, a few code examples. I was just wondering if there's a more efficient way, or if I'm doing some n00b error type things.

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
            char* buffer = new char[120];
            
            cout << "Enter a sentance: ";
            cin >> buffer;
    
            delete [] buffer;
            return 0;
    }
    A pretty useless example i know, but anyway...

    Code:
    #include <iostream>
    using namespace std;
    
    char* ret_buffer ()
    {
            char* buf = new char[50];
            return buf;
    }
    
    int main ()
    {
            char* buffer = new char[50];
    
            strcpy (buffer, ret_buffer(), 50);
            delete [] buffer;
            return 0;
    }
    And another.

    I was just wondering if there are any glaring mistakes? I just feel like I'm doing something wrong but can't put my finger on it. I have quarms about my second example where the function 'ret_buffer()' returns the pointer to a buffer (buf) which is never delete []'d.

    Any help appreciated .

    PS Please don't point out syntax errors I'll notice them after i've posted this and won't really care - we all know what it's supposed to do.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The second one is returning a local variable . . . not good.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
         string S1 = "my name is";
         cout << S1;
         S1 += " Petey Pablo!";
         cout << S1 << endl;
         cin.get();
         return 0;
    }
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
         string userinput;
         cout << "Enter a string!  : ";
         getline(cin, userinput, '\n');
         cout << userinput;
         cin.get();
         return 0;
    }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your qualms about the second example are correct. The memory you allocate in ret_buffer is never cleaned up, and you have a memory leak.

    BTW, that is how C handles strings. C++ handles strings with a string class that handles all the memory management for you so you don't have to worry about these things. Look up the C++ string class in the <string> standard header as ILoveVector's examples show.

    Also, the second one does not return a local variable, that statement is incorrect. It returns a pointer to dynamic memory, but that pointer is not saved and the memory is leaked.

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I knew I was doing something wrong. I spelt qualms 'quarms'. Tutt. Summer heat.

    Thanks to all for your help!
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  6. #6
    FOX
    Join Date
    May 2005
    Posts
    188
    Quote Originally Posted by dwks
    The second one is returning a local variable . . . not good.
    I don't see any problem with that as long as you remember to save the return value to a new pointer. The allocated memory from new is not local.

  7. #7
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Duly noted. Cheers.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM