Messages in this thread | | | Date | Thu, 10 Nov 2005 09:15:17 +0100 | From | "J.A. Magallon" <> | Subject | Re: typedefs and structs |
| |
On Wed, 9 Nov 2005 14:12:38 -0800 (PST), Vadim Lobanov <vlobanov@speakeasy.net> wrote:
> On Wed, 9 Nov 2005, linux-os \(Dick Johnson\) wrote: > > > > > On Wed, 9 Nov 2005, linas wrote: > > > > > On Wed, Nov 09, 2005 at 08:22:15AM -0800, Vadim Lobanov was heard to remark: > > >> On Wed, 9 Nov 2005, J.A. Magallon wrote: > > >> > > >>> void do_some_stuff(T& arg1,T& arg2) > > >> > > >> A diligent C programmer would write this as follows: > > >> void do_some_stuff (struct T * a, struct T * b); > > >> So I don't see C++ winning at all here. > > > > > > I guess the real point that I'd wanted to make, and seems > > > to have gotten lost, was that by avoiding using pointers, > > > you end up designing code in a very different way, and you > > > can find out that often/usually, you don't need structs > > > filled with a zoo of pointers. > > > > > > > But you can't avoid pointers unless you make your entire > > program have global scope. That may be great for performance, > > but a killer if for have any bugs. > > Just to extract some useful technical knowledge from the current ongoing > "flamewar"... > I'm not entirely sure if the above statement regarding performance is > correct. Some enlightenment would be appreciated. > > Suppose you have the following code: > int myvar; > void foo (void) { > printf("%d\n", myvar); > bar(); > printf("%d\n", myvar); > } > If bar is declared in _another_ file as > void bar (void); > then I believe the compiler has to reread the global 'myvar' from memory > for the second printf(). > > However, if the code is as follows: > void foo (void) { > int myvar = 0; > printf("%d\n", myvar); > bar(&myvar); > printf("%d\n", myvar); > } > If bar is declared in _another_ file as > void bar (const int * var); > then I think the compiler can validly cache the value of 'myvar' for the > second printf without re-reading it. Correct/incorrect? >
Nope. You can't trust bar() not doing something like
bar(const int* local_var) { ... use local_var as ro... extern int myvar; myvar = 7; }
For the compiler to do that, you must tag bar() with attribute(pure).
-- J.A. Magallon <jamagallon()able!es> \ Software is like sex: werewolf!able!es \ It's better when it's free Mandriva Linux release 2006.1 (Cooker) for i586 Linux 2.6.14-jam1 (gcc 4.0.2 (4.0.2-1mdk for Mandriva Linux release 2006.1)) [unhandled content-type:application/pgp-signature] | |