Thread: Is there a standard function?

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by frktons
    I've taken into account the fact that this is an evolving function that
    will be ready some weeks forward in the future. The final idea is that I'll use a structure
    with these variables, and the prototype of the function, and all of them
    will be in a header.

    The function will be called with a pointer to the structure.
    Good to hear, but as you evolve the design, avoid evolving it to a worse state. A such, if you need to give the function many parameters for now, go ahead.
    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
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    Quote Originally Posted by laserlight View Post
    Good to hear, but as you evolve the design, avoid evolving it to a worse state. A such, if you need to give the function many parameters for now, go ahead.
    Am I going to evolve it to a worse state? Sorry I did not understand your suggestion/warning.

  3. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By using global variables unnecessarily, you would be evolving it to a worse state.
    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
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    Quote Originally Posted by laserlight View Post
    By using global variables unnecessarily, you would be evolving it to a worse state.
    I have to pass the function these parameters:

    1) bool sign - to tell the function if I want the sign or not in the formatted string
    2) char buffer[15] - the workspace to prepare my integer formatted (10 digits, 3 thousand separators,
    and 1 optional sign).
    3) char sep - the separator to use (comma for british, point for latin, and so on)
    4) the integer to format
    5) maybe something else that I'm still thinking about

    How would you do that?

  5. #35
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    void   int_format(int num, bool sign, char sep)
    {
        char buffer[15];
        // ...
    }
    What is so difficult?
    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.

  6. #36
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ignore part 5 for the time being. You then only need 4 parameters.

    EDIT:
    Elysia's example probably will not work: you should pass a pointer to the first character of the buffer to the function so that you can write to it, with the result reflected in the caller. You can assume that the caller provides a string with sufficient space.
    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. #37
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    Quote Originally Posted by Elysia View Post
    Code:
    void   int_format(int num, bool sign, char sep)
    {
        char buffer[15];
        // ...
    }
    What is so difficult?
    If the function is declared void of a return value, how am I going to have
    the char buffer[] back?

  8. #38
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ah, then I misinterpreted what you needed it for.
    Then more correct would be:
    Code:
    void   int_format(int num, bool sign, char sep, char buffer[], size_t size_of_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.

  9. #39
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    Quote Originally Posted by Elysia View Post
    Ah, then I misinterpreted what you needed it for.
    Then more correct would be:
    Code:
    void   int_format(int num, bool sign, char sep, char buffer[], size_t size_of_buffer)
    {
        // ...
    }
    I'm thinking about pros and cons of passing arguments by value and by reference.
    If I am concerned with performance, I'd avoid passing arguments by value.
    This is the reason I was thinking about passing only a pointer to a structure, that
    is passing arguments by reference as you know.
    The global variables are faster, but there are some drawbacks:
    you have to pay extra attention to what you modify, and data hiding is over
    as well.

    Deciding what's best depends on what you're going to decide is better:
    performance, sticking to standard coding, style, whatever.

    In your opinion what comes first?

    Code:
    size_t size_of_buffer
    the above are not clear, could you explain them please?

  10. #40
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by frktons
    I'm thinking about pros and cons of passing arguments by value and by reference.
    If I am concerned with performance, I'd avoid passing arguments by value.
    You only need to be concerned about this when passing arguments that are struct objects. Since you have not even defined your struct yet, do not be concerned about this as it would be pre-mature optimisation.
    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

  11. #41
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    Quote Originally Posted by laserlight View Post
    You only need to be concerned about this when passing arguments that are struct objects. Since you have not even defined your struct yet, do not be concerned about this as it would be pre-mature optimisation.
    Some programmers put comments in parts of their programs to remember that
    in those points of the code they need to make some change/improvement.

    I'm just doing the same, using both comments and declarations of variables
    that I'm going to move elsewhere. Thinking about the present, I chose global
    variables because they are faster, and I'd like the function would be 2 - 3 times
    faster when I'm finished.

    You can call it pre-mature optimization, or you can call it the direction you intend
    to follow? Maybe both.

    By the way, any other suggestion about performance or anything else?
    How would you move if you wamt to have a better performance?

    Well I actually need also point 5: I need to tell the function if I'd like the
    formatted number being right or left aligned as well.
    Last edited by frktons; 07-03-2010 at 10:13 AM.

  12. #42
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by frktons View Post
    Thinking about the present, I choosed global
    variables because they are faster
    That is maybe okay if:
    1) the scale of the whole program is not too big
    2) this is the central operation of the whole program
    3) the function in question is very short and called many times repeatedly

    Otherwise, the use of globals is no optimization at all, it's just messy and pointless. It seems to me you are developing a general purpose function which you might use in some other, larger project, so in fact the use of globals here is very inappropriate.
    Last edited by MK27; 07-03-2010 at 10:15 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #43
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Listen, you don't know if it's a bottleneck or not. Premature optimizations leads to bad code. Just don't do it.
    It is better to create readable and easy-to-understand code and stick to standards when coding. You can then measure with a profiler to find real bottlenecks later if performance is an issue.
    Just for the sake of it, I can give you some more info to make you doubt:
    - Global variables aren't necessarily better. Why? Because they can be modified everywhere, the compiler cannot assume certain things that it can on local variables. Hence it cannot perform some optimizations.
    - Passing things by address also causes overhead because you must dereference the pointer everytime you use it. Variables passed by value does not have this overhead.
    - Of course, passing by value means that a copy of the variables needs to be made, which costs time. But pointers also has to be copied.
    So you see, it isn't as black and white as you would like it.

    As for the size_t size_of_buffer:
    It's the size of your buffer that you pass in. Use it to make sure you don't cause a buffer overrun.
    I highly recommend that you write routines for all kinds of copying that makes an assertion if you are trying to copy more data than there is room for. It will save you time hunting down bugs and will increase the security of your application.
    Last edited by Elysia; 07-03-2010 at 10:20 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.

  14. #44
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    Quote Originally Posted by MK27 View Post
    That is maybe okay if:
    1) the scale of the whole program is not too big
    2) this is the central operation of the whole program
    3) the function in question is very short and called many times repeatedly

    Otherwise, the use of globals is no optimization at all, it's just messy and pointless. It seems to me you are developing a general purpose function which you might use in some other, larger project, so in fact the use of globals here is very inappropriate.
    Yes MK27, you are absolutely right.

    1) a program that print numbers, a lot of, I mean on real paper.
    2) in practice yes, other than read a file and print it.
    3) maybe 5 times for each data record, thousands of them
    Last edited by frktons; 07-03-2010 at 10:24 AM.

  15. #45
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    Quote Originally Posted by Elysia View Post
    Listen, you don't know if it's a bottleneck or not. Premature optimizations leads to bad code. Just don't do it.
    It is better to create readable and easy-to-understand code and stick to standards when coding. You can then measure with a profiler to find real bottlenecks later if performance is an issue.
    Just for the sake of it, I can give you some more info to make you doubt:
    - Global variables aren't necessarily better. Why? Because they can be modified everywhere, the compiler cannot assume certain things that it can on local variables. Hence it cannot perform some optimizations.
    - Passing things by address also causes overhead because you must dereference the pointer everytime you use it. Variables passed by value does not have this overhead.
    - Of course, passing by value means that a copy of the variables needs to be made, which costs time. But pointers also has to be copied.
    So you see, it isn't as black and white as you would like it.

    As for the size_t size_of_buffer:
    It's the size of your buffer that you pass in. Use it to make sure you don't cause a buffer overrun.
    I highly recommend that you write routines for all kinds of copying that makes an assertion if you are trying to copy more data than there is room for. It will save you time hunting down bugs and will increase the security of your application.
    Thanks Elysia, I'll think about these things.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  3. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM