[LC++]Size of structure pointer/array

Paul M Foster paulf at quillandmouse.com
Thu Aug 30 14:47:04 UTC 2001


On Thu, Aug 30, 2001 at 03:51:53AM +0200, Carlo Wood wrote:

> On Wed, Aug 29, 2001 at 09:29:11PM -0400, Paul M Foster wrote:
> > Assuming:
> >
> > //////////////////////
> >
> > struct int_struct {
> >	int a;
> >	int b;
> >	int c;
> > };
> >
> > void is_func(int_struct *i); // defined elsewhere
> >
> > int_struct is[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
> 

<snip>

> > is_func(is);
> >
> > ////////////////////
> >

<snip>

> This is typical C coding however and should not be looked at by those on
> this list that want to learn C++  ;) (then you'd use references and
> vectors).

C++, by design, includes the subset of C code. And not every facility of
C++ is superior to every facility of C in every situation. For example,
I consider iostreams and iomanip brain damaged when compared to the
simplicity of printf. In the above case, the array is fixed length,
initialized on declaration, and will not be changed for the whole
program. 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



More information about the tuxCPProgramming mailing list