Re: [Dev-C++] What's the difference?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <no...@ho...> - 2003-03-04 01:19:41
|
-------Original Message-------=0D
=0D
From: OROSZI Bal=E1zs=0D
Date: =D4=F1=DF=F4=E7, 4 =CC=DC=F1=F4=E9=EF=F2 2003 12:29:50 =F0=EC=0D
To: dev...@li...=0D
Subject: Re: [Dev-C++] What's the difference?=0D
=0D
Hello!=0D
=0D
It is advisable to use pointers when you use structures, because it is =0D
faster and more convenient. Suppose you have this structure:=0D
struct foo{=0D
char *Text1;=0D
char *Text2;=0D
char *Text3;=0D
};=0D
=0D
If you have a function that has this structure as its argument, then it =0D
is better to pass only a pointer to it (&foo_struct), beacuse it already =
=0D
has pointers in it to character arrays, so you have the same result if =0D
you pass pointer or pass the whole struct. But when passing the whole =0D
struct, it will be copied, but the Texts won't, just the pointers to it. =
=0D
So it is better to pass only pointers to structs, always. It will make =0D
your code cleaner, and you can always assure yourself, that you always =0D
declared functions to use pointers.=0D
=0D
=0D
=3D=3D> ? In fact allocating/releasing space on the free store is more
time-expensive than using local space in the stack. So for example:=0D
=0D
// C++=0D
=0D
struct foo=0D
{=0D
char *Text1, *Text2, *Text3;=0D
};=0D
=0D
=0D
foo something;=0D
=0D
something.Text1=3Dnew char[20];=0D
something.Text2=3Dnew char[30];=0D
something.Text3=3Dnew char[40];=0D
=0D
// ...=0D
=0D
delete [] something.Text1;=0D
delete [] something.Text2;=0D
delete [] something.Text3;=0D
=0D
=0D
is more than expensive than=0D
=0D
=0D
struct foo=0D
{=0D
char Text1[20], Text2[30], Text3[40];=0D
};=0D
=0D
foo something;=0D
=0D
=0D
// "something" dies at the end of its scope=0D
=0D
=0D
=3D=3D> Not to mention allocating something itself on the free store.=0D
=0D
=3D=3D> Passing a pointer to struct with local arrays doesn't copy anythi=
ng
except of the pointer address (value). For example:=0D
=0D
=0D
struct foo=0D
{=0D
char text1[10], text2[20], text3[30];=0D
};=0D
=0D
=0D
=0D
void whatever(foo *);=0D
=0D
=0D
int main()=0D
{=0D
foo something=3D{"telos", "ton", "imeron"};=0D
=0D
whatever(&something);=0D
=0D
}=0D
=0D
=0D
// Passed by "reference".=0D
// Only the address of something is copied, not its elements.=0D
void whatever(foo *pf)=0D
{=0D
// ...=0D
}=0D
=0D
=0D
=0D
=3D=3D> However i do not like this style. How i would do it:=0D
=0D
#include <string>=0D
=0D
struct foo=0D
{=0D
std::string text1, text2, text3;=0D
=0D
foo(const std::string &t1, const std::string &t2, const std::string &t3)=
:=0D
text1(t1), text2(t2), text3(t3) { }=0D
=0D
};=0D
=0D
=0D
=0D
void whatever(foo &);=0D
=0D
=0D
int main()=0D
{=0D
foo something("telos", "ton", "imeron");=0D
=0D
whatever(something);=0D
=0D
}=0D
=0D
void whatever(foo &pf)=0D
{=0D
// ...=0D
}=0D
=0D
=0D
=0D
=3D=3D> Or even better i would provide it as a class with access function=
s.=0D
=0D
=0D
=0D
Ioannis Vranos=0D
=0D
* Programming pages: http://www.noicys.freeurl.com=0D
* Alternative URL: http://run.to/noicys=20 |