[LC++]Size of structure pointer/array
Carlo Wood
carlo at alinoe.com
Fri Aug 31 08:26:17 UTC 2001
On Thu, Aug 30, 2001 at 12:46:23AM -0400, Paul M Foster wrote:
> C++, by design, includes the subset of C code.
True, the syntax is therefore C++.
> And not every facility of
> C++ is superior to every facility of C in every situation.
Agreed.
> For example,
> I consider iostreams and iomanip brain damaged when compared to the
> simplicity of printf.
That is a matter of "being used to". I prefer ostreams because then
I can print abstract objects, and not only builtin types.
One of the main problems with ostreams I encountered was that their
benefit is lost unless you use 100% ostreams (and "never" printf).
This is why I wrote libcwd :) (http://libcw.sourceforge.net/debugging/)
so that there would exist a good interface for writing debug output
using ostreams too.
> In the above case, the array is fixed length,
> initialized on declaration, and will not be changed for the whole
> program.
Then you should make it const:
struct int_struct {
int a;
int b;
int c;
};
int_struct const is[] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
^^^^^
This will then force you to make the argument of is_func point
to a constant array too:
void is_func(int_struct const* i);
^^^^^
which will protect you from errors of accidently changing it's
content.
(For the reason why I put the 'const' AFTER the type instead of before it,
read: http://www.xs4all.nl/~carlo17/c++/const.qualifier.html)
> The code generated by using a vector would be far more complex
> than simply using an array. Harder to debug if incorrect, as well.
>
> That said, though, I do use vectors when I don't know how big an "array"
> will get, and it's more natural to initialize it with a push_back().
>
> The main reasons I use C++ are to make things easier on myself as the
> programmer, and to protect my code from me. ;-} That is, 1) type
> checking is more rigid in C++; 2) classes allow for a simpler API in the
> main program; 3) classes allow for more compartmented testing of
> software components; and 4) classes (if properly designed), prevent me
> and others from screwing around with the inside of a class object (that
> is, the object is opaque, except for its API or methods).
>
> Paul
Of course I agree with you, and of course I'd use an array too in
this case. It's my opinion however that I think that C++ newbies should
FIRST learn to use the STL correctly and THEN learn about arrays.
I am sorry if my remark came accross as an attack towards your coding
style - that was not my intention; my intention was to warn the learning
newbie that this knowledge is dangerous to get used to too early ;).
When I write to this list I address everyone, especially the unexperienced
users.
--
Carlo Wood <carlo at alinoe.com>
More information about the tuxCPProgramming
mailing list