Thread: Modify string in function

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In my opinion, if you want to design ReceiveDataWithHeader to have a flexible interface to cater for buffers of different sizes, then either go for an interface in which there is begin/end pointer parameters to denote the range of the buffer, or in which there is a pointer parameter and also a size parameter. The former is akin to the approach taken by standard algorithms; the latter is a common approach for arrays in C. Doing the conventional follows the principle of least surprise.
    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

  2. #32
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But it does not protect against mistakes. That's why sanity checks are good.
    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. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    But it does not protect against mistakes. That's why sanity checks are good.
    It is more flexible, and appropriate sanity checks can still be done in the calling code. Also, there is no reason why this primary interface cannot be extended by wrappers for arrays and containers.
    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

  4. #34
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >> Explain HOW that's irrelevant.

    If we coded a function the way you recommended, it would not be compatible with vector anyway. A function with a pointer and a size argument is. Before you come back with something ridiculous, maybe it doesn't matter where the memory is allocated, so coding another function to handle it would not be ideal.

    >> Debugging isn't meant to make sure the code works. It's meant to FIND what causes the bugs that prevents the coding from working.

    On the other hand, if code doesn't go through debugging, you will likely never try to find bugs in run time.

    >> Regression testing makes sure the code works. This is part of regression testing.

    Implementation is part of regression testing? You can't test software before you write it.
    Last edited by whiteflags; 06-19-2012 at 08:58 AM.

  5. #35
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by whiteflags
    You can't test software before you write it.
    Some Extreme Programming advocates may beg to differ
    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

  6. #36
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    It is more flexible, and appropriate sanity checks can still be done in the calling code. Also, there is no reason why this primary interface cannot be extended by wrappers for arrays and containers.
    I am not going to complain about the design. It's good. It just needs sanity checks, which is what I am trying to get out there--that the code lacks sanity checks and that they are good. I'd prefer the callee do the checks, though.

    Quote Originally Posted by whiteflags View Post
    If we coded a function the way you recommended, it would not be compatible with vector anyway. A function with a pointer and a size argument is. Before you come back with something ridiculous, maybe it doesn't matter where the memory is allocated, so coding another function to handle it would not be ideal.
    A function with a pointer and size is error prone. It's the C way. Hence, if you can avoid that (and if you are writing C++, then most of the time you CAN), then you SHOULD.
    As for how to code a safe function that takes both vectors and arrays--or something else, for that matter--is something I don't know yet. I haven't experimented with it enough because I tend to use minimalist interfaces.
    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. #37
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Some Extreme Programming advocates may beg to differ :P
    This kind of extreme?

  8. #38
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    A function with a pointer and size is error prone. It's the C way. Hence, if you can avoid that (and if you are writing C++, then most of the time you CAN), then you SHOULD.
    As for how to code a safe function that takes both vectors and arrays--or something else, for that matter--is something I don't know yet. I haven't experimented with it enough because I tend to use minimalist interfaces.
    I kind of regret how this thread turned out because I could have shown you how to do that.

  9. #39
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    As for how to code a safe function that takes both vectors and arrays--or something else, for that matter--is something I don't know yet. I haven't experimented with it enough because I tend to use minimalist interfaces.
    I already mentioned it: you can extend the interface. Your notion of a "safe function" is flawed in that if you are going to accept a range, whether denoted by an iterator pair, a range construct or a pointer & size, then you have to trust that the caller provided the correct range. But if you want to extend this such that containers that know their own size can be used, or by allowing arrays of fixed size (besides std::array) to be passed such that their size are automatically determined, then go ahead. But suggesting passing an array by reference as the primary interface seems wrong to me: it is not conventional and limits the ease of use of the function.
    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

  10. #40
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I haven't experimented with it enough because I tend to use minimalist interfaces.
    Bwahahaha!

    I wonder, does anyone else remember the 200+ post thread relating to the interfaces that Elysia used to prefer?

    Soma

  11. #41
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Well, between then and now, templates are only a little less magical.

  12. #42
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by whiteflags View Post
    I kind of regret how this thread turned out because I could have shown you how to do that.
    It's not too late. If you feel up to it, why don't you?

    Quote Originally Posted by laserlight View Post
    I already mentioned it: you can extend the interface. Your notion of a "safe function" is flawed in that if you are going to accept a range, whether denoted by an iterator pair, a range construct or a pointer & size, then you have to trust that the caller provided the correct range. But if you want to extend this such that containers that know their own size can be used, or by allowing arrays of fixed size (besides std::array) to be passed such that their size are automatically determined, then go ahead. But suggesting passing an array by reference as the primary interface seems wrong to me: it is not conventional and limits the ease of use of the function.
    I only mentioned passing by reference as a solution to the current design by the OP.
    I am not commenting on whether the current design is a good one or if there is a better one.
    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.

  13. #43
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >> It's not too late. If you feel up to it, why don't you?

    You can use bidirectional_iterator<T>s which will work for STL containers and arrays as well, but raw pointers are also equally capable with legacy code.

    More to my point, to make a function interface that works equally well for arrays as it does vector, or any range based type, you have to understand that pointers are an example of a bidirectional iterator and how to generate pointers with, say, vector. I take the address of the first element. If you pass in that pointer and the size of the range, the other end iterator can be generated by the function. If the function interface is a beginning pointer and an end pointer, the argument for the first is the same, and the argument for the second is something like &v[v.size()] (or what have you). For sanity checks, you can do null pointer comparisons, as well as other types of static analysis if you use an iterator class. If you new'd something, then you already have the size: Treat it as const where possible.

    Perhaps the best thing about the fact that this works is that you are able to support legacy code rather seamlessly. In my opinion, using pointers would be cleaner since we are using UNIX networking code, but either of the above options are preferable to the reference to an array. It follows the same kind of iterator interface stuff the STL algorithms do and that's easy to understand. It doesn't matter where the memory is so that makes it flexible. It's even more flexible because you don't even have to pass the whole data structure to the function and only the section you want worked. You have the choice of template code instead of being forced to use it, since templates are not required. In a word, it's just clean. If you make a mistake with such an interface, you deserve it. It's as good as it comes.
    Last edited by whiteflags; 06-19-2012 at 11:16 AM.

  14. #44
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First off, it is nonsense to argue that because some part of our program is C, all code around it should also follow C conventions. It is dumb, because C is not C++. In C++, we have lots of tools at our disposal that C does not have. Does it make sense not to use them? Of course not. If we didn't use them, we might as well use C to begin with. We should instead abstract the C code with strongly typed C++ interfaces that perform a lot of static (and dynamic) checks to make sure we don't feed invalid input to the unsafe C code.

    Secondly, raw pointers are dumb because they are "dumb." They don't provide access to any rich information as iterators does. Some STL algorithms don't work with raw pointers (can't recall any on the top of my head, though)!

    Thirdly, there are no possible checks that can be made with raw pointers. We just can't make sure the input arguments are sane.

    Although, dynamic checking is better than nothing. Thus, it would be much better and much safer to use C++ iterators (and disallow raw pointers) in the interface. It wouldn't be ideal, though AFAIK, because we have no means to do static checks that the input is sane. I would much prefer this in the text you wrote.
    Last edited by Elysia; 06-19-2012 at 11:13 AM.
    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. #45
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Quote Originally Posted by Ducky View Post
    @Elysia
    Buf2 is already a pointer, so you do not need Buf.[...]
    Please use [ quote ] [ /quote ] tags. Your posting style makes it a bit hard to follow the discussion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. modify function pass it array
    By a.mlw.walker in forum C Programming
    Replies: 12
    Last Post: 08-01-2011, 04:03 AM
  2. Replies: 15
    Last Post: 05-11-2011, 05:06 PM
  3. how to modify strcmp function
    By asteroid1122 in forum C Programming
    Replies: 6
    Last Post: 08-23-2009, 12:24 AM
  4. modify pointer to a string/character constant.
    By xsouldeath in forum C Programming
    Replies: 12
    Last Post: 10-03-2007, 02:41 AM
  5. modify has function from string parameter to templates...
    By rusty0412 in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2005, 08:02 PM