Thread: Copying string vector to char vector

  1. #16
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you could use std::string for that.

    instead of a raw buffer and length, you can use std::string::data() and std::string::length().
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    std::string is pretty much contiguous in all possible implementations as far as I know due to its nature.
    In C++11, it is also mandated in the standard that it be contiguous, so it suffices very well as a buffer.
    You can use data() or c_str() to get a string to send, while using size() or length() to determine the length of the buffer.
    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.

  3. #18
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Great, I'll have to rewrite all of my programs then. Lot of work but it sounds like it will simplify the code so it worth it.
    Plus I heard something like that vector needs a contiguous memory to be allocated so maybe std::string will be more flexible and can allocate more memory.

    Thank you.
    Using Windows 10 with Code Blocks and MingW.

  4. #19
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Ducky View Post
    vector needs a contiguous memory to be allocated so maybe std::string will be more flexible and can allocate more memory.
    it's really up to the operating system to decide how to allocate memory for it. if it needs a big contiguous block of memory, and there isn't one available, the memory allocation will simply fail. std::string behaves nearly identically to std::vector with respect to memory allocation.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #20
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by Ducky View Post
    Plus I heard something like that vector needs a contiguous memory to be allocated so maybe std::string will be more flexible and can allocate more memory.
    Note that the "contiguous memory" is part of a virtual memory address space. For X86, virtual page size is 4k, and the physical 4k blocks can be scattered, but the operating system will map them to produce a contiguous virtual address space. How malloc(), new, free(), and delete work with an operating system isn't clearly defined, so I'm not sure what the limit is. For Windows XP 32 bit, I can usually allocate around 1.5GB with a single call to malloc(). For 64 bit systems, I'm not sure what the limit is.

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by rcgldr View Post
    For 64 bit systems, I'm not sure what the limit is.
    Most likely close to your actual free ram since the address space is so huge.
    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.

  7. #22
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Elysia View Post
    Most likely close to your actual free ram since the address space is so huge.
    It could be more than that. On Linux it would be bounded by your physical memory plus the swap partition. On Windows, the pagefile can automatically resize, so you might be able to allocate the entire free space of your hard drive of the windows partition.

    Of course at that point you're accessing at hard-drive speeds, so you'd be better off using file IO directly or a database.


    Long before that, for sufficiently large containers, continuous memory can be inefficient, so std::deque can be used. If you need random access with long strings, this might be viable.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting string vector to integer vector
    By CPlus in forum C++ Programming
    Replies: 4
    Last Post: 05-08-2010, 05:43 AM
  2. Replies: 9
    Last Post: 04-04-2008, 12:41 PM
  3. copying vector to list
    By l2u in forum C++ Programming
    Replies: 13
    Last Post: 04-01-2008, 02:28 PM
  4. std::vector<char> vs. std::string
    By drrngrvy in forum C++ Programming
    Replies: 11
    Last Post: 12-15-2006, 05:06 PM
  5. connect vector<int> and vector<char>
    By hdragon in forum C++ Programming
    Replies: 1
    Last Post: 04-27-2006, 01:16 PM