Thread: Memory reallocation

  1. #16
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    No, it's recv() Win32 API wont compile with std::string. Thanks though.
    I hope it's good that way, I use memcpy() cause it's a binary string.

    Code:
    int main()
    {
        char * Buf = new (nothrow)char[1024];
        if(Buf == 0) return 1;
        char Temp[1024];
        strcpy(Buf, "hello"); // just for the test
        memcpy(Temp, Buf, 6);
    
        delete[] Buf;
        Buf = new (nothrow)char[2048];
        if(Buf == 0) return 1;
        memcpy(Buf, Temp, 6);
    
        delete[] Buf;
        return 0;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ducky
    No, it's recv() Win32 API wont compile with std::string.
    That's rubbish. It will compile, otherwise your standard library or Windows API implementation is broken, or you are simply writing incorrect code.

    That said, in the case of recv, it might be semantically better to use a std::vector<char> instead of a std::string since you're dealing with a buffer rather than a string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by laserlight View Post
    That said, in the case of recv, it might be semantically better to use a std::vector<char> instead of a std::string since you're dealing with a buffer rather than a string.
    And do not forget to resize (or create) your vector to be big enough to store the data you want to receive, and you actually put pointer to the first element and not the vector variable itself to your recv call as in laserlight's post #10
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #19
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    But looks like it is supported only starting with MSVC2012... I'm still on 2008/2010 mostly.
    O_o

    In practice, few library implementations (I don't know of any; just hedging my bets.) make use of the availability of "Rope" or "Chain" data structures to implement `std::basic_string<???>' thanks to the complexity of implementing `c_str', `data', and `operator[]' methods over common improvements and "improvements" like "short buffers", "lazy initialization", and "copy-on-write".

    Add to the complexity that `c_str' and `data' are both constant methods which would need `mutable' or a registered proxy to implement without just using a contiguous buffer and always terminating the string on mutation.

    Add to the complexity and mutability issue the way in which different methods are allowed to invalidate or required to not invalidate the memory returned by `c_str' and `data' combined with a non-contiguous primary buffer which you'd need a global garbage collector to fix in the face of some of the common implementation strategies.

    *shrug*

    The contiguous aspect isn't mandated by the standard, but much like how the validity of the "struct hack" isn't mandated by the standard, you will be on pretty safe territory making the assumption thanks to the meeting of what bits the standard does guarantee with reality.

    Soma

  5. #20
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by phantomotap View Post
    O_o

    In practice, few library implementations (I don't know of any; just hedging my bets.)
    I prefer not to utilize the implementation defined behavior in my code as much as I can.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #21
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I prefer not to utilize the implementation defined behavior in my code as much as I can.
    O_o

    I wasn't trying to tell you how to code.

    I was just providing information.

    If you'd rather not rely on a quirk of reality, I applaud the decision.

    Soma

  7. #22
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    You were right Laserlight, recv() compiles with char vector.
    Yahoo, no more messing around with pointers. ;-)
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reallocation of heap memory
    By sukhdeep in forum C++ Programming
    Replies: 17
    Last Post: 12-20-2011, 02:16 AM
  2. Reallocation of memory (3d dynamic matrix)
    By beta3designs in forum C Programming
    Replies: 13
    Last Post: 08-01-2011, 12:43 PM
  3. Memory allocation/reallocation
    By magda3227 in forum C Programming
    Replies: 10
    Last Post: 07-04-2008, 03:27 PM
  4. Memory reallocation in C++
    By spank in forum C++ Programming
    Replies: 2
    Last Post: 08-08-2007, 09:56 AM
  5. Dynamic memory reallocation
    By Gravedigga in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2005, 06:39 PM