Thread: Appending to the end of a char*

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    USA! USA! USA!
    Posts
    19
    because I declared alot of my char*s like this:
    char* variable = "stuff";

    this way brings up warnings.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Time to change to use std::string then.
    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
    Banned
    Join Date
    Aug 2010
    Location
    USA! USA! USA!
    Posts
    19
    but alot of functions I use only accept char*.
    How would you declare a char* normally?

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by thegr8n8
    but alot of functions I use only accept char*.
    What functions might these be? There are generally std::string equivalents for standard library functions that deal with null terminated strings.
    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

  5. #20
    Banned
    Join Date
    Aug 2010
    Location
    USA! USA! USA!
    Posts
    19
    I am using this networking engine.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by thegr8n8
    I am using this networking engine.
    Ah, now that changes things.

    It depends on the situation. One option is to use a std::vector<char> that is of the desired size (and is not empty), then pass &v[0] as an argument, where v is the vector name. This would not be appropriate for your original example in this thread (where std::string should be used), but it would be appropriate if you are trying to allow the engine to populate some dynamically sized buffer.

    You can actually do the same trick with a std::string, except that due to a defect in the current C++ standard it is not guaranteed to work in the same way as for a vector.
    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

  7. #22
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If you're going to call a function that uses char * do this:

    Code:
    size_t tempsize = size_t(str.size());
    char *temp = new char[tempsize + 1UL];
    str.copy (temp, tempsize);
    temp[tempsize] = '\0';
    foo (temp, tempsize);
    delete[] temp;
    You can use strings the rest of the time. And there are a number of fancy containers that will probably make things "easier".

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Note that if foo might throw an exception then whiteflags' example is not exception safe.
    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

  9. #24
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by laserlight View Post
    You can actually do the same trick with a std::string, except that due to a defect in the current C++ standard it is not guaranteed to work in the same way as for a vector.
    Oh wow, I didn't realize this. Are you saying that a std::string is not guaranteed to have a single contiguous block of memory?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  10. #25
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by laserlight View Post
    Note that if foo might throw an exception then whiteflags' example is not exception safe.
    Ooops, didn't write try catch blocks...

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by whiteflags View Post
    If you're going to call a function that uses char * do this:

    Code:
    size_t tempsize = size_t(str.size());
    char *temp = new char[tempsize + 1UL];
    str.copy (temp, tempsize);
    temp[tempsize] = '\0';
    foo (&temp[0], tempsize);
    delete[] temp;
    You can use strings the rest of the time. And there are a number of fancy containers that will probably make things "easier".
    Code:
    std::vector<char> buffer;
    std::copy(str.begin(), str.end(), std::back_inserter(buffer));
    foo(&buffer[0], buffer.size());
    kthx.
    Last edited by Elysia; 08-12-2010 at 02:32 PM. Reason: Fix bug in code
    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.

  12. #27
    Banned
    Join Date
    Aug 2010
    Location
    USA! USA! USA!
    Posts
    19
    I am now confused

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by pianorain
    Are you saying that a std::string is not guaranteed to have a single contiguous block of memory?
    Yes. Actually, adeyblue once pointed out to me that this should not be a problem because std::basic_string's operator[pos] is defined as doing something equivalent to returning data()[pos]. Unfortunately, data() is itself declared as returning a const char*, and it is explicitly forbidden to modify the corresponding character array. Hence, if one accepts this definition at face value, it means that one cannot actually modify a character returned by the non-const operator[], which does not make sense.

    In the next version of the C++ standard, this problem will be fixed by defining operator[] using iterators instead, and also std::string will be required to be implemented with a single contiguous block of memory.
    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

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by thegr8n8 View Post
    I am now confused
    It would help to clarify what you're confused about.
    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.

  15. #30
    Banned
    Join Date
    Aug 2010
    Location
    USA! USA! USA!
    Posts
    19
    this :
    Code:
    size_t tempsize = size_t(str.size());
    char *temp = new char[tempsize + 1UL];
    str.copy (temp, tempsize);
    temp[tempsize] = '\0';
    foo (&temp[0], tempsize);
    delete[] temp;
    what is teh size_t?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM
  4. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 02:40 PM
  5. appending some letters to the end of a CString variable.
    By Eber Kain in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2001, 03:00 PM