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.Originally Posted by frktons
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.Originally Posted by frktons
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
By using global variables unnecessarily, you would be evolving it to a worse state.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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?
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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?
the above are not clear, could you explain them please?Code:size_t size_of_buffer
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.Originally Posted by frktons
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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
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.