Thread: Serializing classes

  1. #46
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Whyrusleeping View Post
    also, in your rewritten search function, what happens when the word being searched for isnt in the file? it looks like an infinite loop
    So it is. A bug.
    I edited the original code to fix the bug.
    We have to manually check for eof. Using eof as loop control is a bad idea™ (the faq on the site explain why). Furthermore, what happens if we hit end of file just after reading the length? Another thing not covered by your code.

    Quote Originally Posted by King Mir View Post
    Of all the things wrong with his code, assuming that int is 4 bytes isn't one that counts. That's a pretty reasonable assumption.
    But still an assumption, and assumptions are bad.

    Quote Originally Posted by King Mir View Post
    Instead of a string or char array, Elysia chose to write the data into a char vector. I presume that this is because std::string data is not guaranteed to be continuous in memory, if that is in fact the case. A vector is like an array, but safer.
    The main reason is because I can control its size to make sure there is no buffer overrun. The string's length is not fixed. Plus the OP's original code is prove to buffer overruns because no check is made to ensure the length is < 32.
    Although, std::string is not guaranteed to be contiguous, that is of no concern since we're storing a C-style string which is guaranteed to be contiguous and nul-terminated. Similarly, when we read back, it will construct a new string from the C-style string.
    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.

  2. #47
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Elysia View Post
    So it is. A bug.
    I edited the original code to fix the bug.
    We have to manually check for eof. Using eof as loop control is a bad idea™ (the faq on the site explain why). Furthermore, what happens if we hit end of file just after reading the length? Another thing not covered by your code.
    Your fix demonstrates exactly why checking on eof is not a good idea. If the data is poorly formatted and the length is missing, your program will still loop forever.

    But still an assumption, and assumptions are bad.
    But it's not a ticking time bomb. It won't fail unless someone tries to compile it on some obscure OS.

    The main reason is because I can control its size to make sure there is no buffer overrun. The string's length is not fixed. Plus the OP's original code is prove to buffer overruns because no check is made to ensure the length is < 32.
    Although, std::string is not guaranteed to be contiguous, that is of no concern since we're storing a C-style string which is guaranteed to be contiguous and nul-terminated. Similarly, when we read back, it will construct a new string from the C-style string.
    std::string has an identical constructor to the one you use for vector. The only reason not to use it is if there is a chance that &buf[0] doesn't correctly access the entire string.

    I don't know if string is required to use continuous memory, or if it is allowed to function like deque.
    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.

  3. #48
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    It appears that the use of continuous memory in a std::string is going to be required in the future. See this link.

    Jim

  4. #49
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by King Mir View Post
    Your fix demonstrates exactly why checking on eof is not a good idea. If the data is poorly formatted and the length is missing, your program will still loop forever.
    True, the >> operator is a problematic one.
    Still, you know as well as I that using eof for loop control is bad™.

    But it's not a ticking time bomb. It won't fail unless someone tries to compile it on some obscure OS.
    A bug is a bug, and we specialize in portable code. Thus, if a simple fix can be made, then I say do it.

    std::string has an identical constructor to the one you use for vector. The only reason not to use it is if there is a chance that &buf[0] doesn't correctly access the entire string.

    I don't know if string is required to use continuous memory, or if it is allowed to function like deque.
    This might be a possible optimization, actually...
    Still, it was only an example. I would still go with boost to avoid all of this complexity and bug fest.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversions between base classes and derived classes
    By tharnier in forum C++ Programming
    Replies: 14
    Last Post: 03-18-2011, 10:50 AM
  2. Classes access other classes local variables
    By parad0x13 in forum C++ Programming
    Replies: 6
    Last Post: 01-14-2010, 04:36 AM
  3. Serializing/deserializing problem
    By vsla in forum C Programming
    Replies: 3
    Last Post: 04-21-2008, 03:55 PM
  4. Serializing problem.. (can't use >> operator)
    By RancidWannaRiot in forum Windows Programming
    Replies: 2
    Last Post: 10-29-2005, 11:10 AM
  5. Serializing a class
    By Prog.Patterson in forum C++ Programming
    Replies: 4
    Last Post: 10-27-2005, 10:21 PM