Thread: [Dev-C++] What's the difference?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Robson B. <rob...@su...> - 2003-03-03 18:11:03
|
Hi coders, what's the difference between using pointers and using simple variables? How does this difference affects the performance of my code? I know that using pointers is working with memory addresses but using simple variables would not have the same effect? int *number or int number: what's the best to be used? Thanks! |
|
From: <st...@xs...> - 2003-03-03 18:26:04
|
This really depends on what you're doing. The main benefit to pointers is that you can send them to functions where the function can modify the value of that pointer variable. With normal variables they only get passed by value and since you can only have one return value per function it sometimes becomes necessary to modify the variables within your function rather than calling the funtion multiple times or writing multiple functions that return slightly different variables. Thanks, Steven Quoting Robson Bernard <rob...@su...>: > Hi coders, > > what's the difference between using pointers and using simple variables? How > does this difference affects the performance of my code? I know that using > pointers is working with memory addresses but using simple variables would > not have the same effect? > > int *number or int number: what's the best to be used? > > > Thanks! > > > > ------------------------------------------------------- > This sf.net email is sponsored by:ThinkGeek > Welcome to geek heaven. > http://thinkgeek.com/sf > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
|
From: Adam M. <up...@in...> - 2003-03-03 18:30:55
|
Hi. If you don't need pointers - don't use them. Unfortunately sometimes it is indispensable to use pointers. One little and simple example: when you want to pass argument to function and this function chages its value. To do this you must pass pointer or reference to the argument. And one more usage of pointers - dynamic memory allocation. regards Adam > Hi coders, > > what's the difference between using pointers and using simple variables? How > does this difference affects the performance of my code? I know that using > pointers is working with memory addresses but using simple variables would > not have the same effect? > > int *number or int number: what's the best to be used? > > > Thanks! ---------------------------------------------------------------------- KLIKAJ!!! Nie pytaj dlaczego... >>> http://link.interia.pl/f16e2 |
|
From: <or...@vp...> - 2003-03-03 22:16:25
|
Hello!
> what's the difference between using pointers and using simple variables? How
> does this difference affects the performance of my code? I know that using
> pointers is working with memory addresses but using simple variables would
> not have the same effect?
It is advisable to use pointers when you use structures, because it is
faster and more convenient. Suppose you have this structure:
struct foo{
char *Text1;
char *Text2;
char *Text3;
};
If you have a function that has this structure as its argument, then it
is better to pass only a pointer to it (&foo_struct), beacuse it already
has pointers in it to character arrays, so you have the same result if
you pass pointer or pass the whole struct. But when passing the whole
struct, it will be copied, but the Texts won't, just the pointers to it.
So it is better to pass only pointers to structs, always. It will make
your code cleaner, and you can always assure yourself, that you always
declared functions to use pointers.
--
Greetings,
Balázs
|
|
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 |
|
From: <or...@vp...> - 2003-03-04 15:20:10
|
Ioannis Vranos wrote:
> ==> ? In fact allocating/releasing space on the free store is more
> time-expensive than using local space in the stack.
It is, but using the stack is only advisable for temporary structures.
> How i would do it:
>
> #include <string>
>
> struct foo
> {
> std::string text1, text2, text3;
>
> foo(const std::string &t1, const std::string &t2, const std::string &t3):
> text1(t1), text2(t2), text3(t3) { }
>
> };
>
> void whatever(foo &);
> int main()
> {
> foo something("telos", "ton", "imeron");
> whatever(something);
> }
>
> void whatever(foo &pf)
> {
> // ...
> }
>
> ==> Or even better i would provide it as a class with access functions.
Well yes, I see now. I don't know C++, so I looked up in a book what you
wrote, and it seems you use references. Isn't that the same as using
pointers?
Anyway, in C, pointers are the only way.
--
Greetings,
Balázs
|
|
From: Ioannis V. <no...@ho...> - 2003-03-04 16:08:03
|
-------Original Message-------=0D =0D From: OROSZI Bal=E1zs=0D Date: =D4=F1=DF=F4=E7, 4 =CC=DC=F1=F4=E9=EF=F2 2003 5:34:24 =EC=EC=0D To: dev...@li...=0D Subject: Re: [Dev-C++] What's the difference?=0D =0D Ioannis Vranos wrote:=0D > =3D=3D> ? In fact allocating/releasing space on the free store is more = =0D > time-expensive than using local space in the stack.=0D =0D It is, but using the stack is only advisable for temporary structures.=0D =0D =0D =3D=3D> Well it depends. You can use the stack to callers and pass the st= ack entities to called functions.=0D =0D =0D =0D =0D =0D =0D >> =0D > =3D=3D> Or even better i would provide it as a class with access functi= ons.=0D =0D Well yes, I see now. I don't know C++, so I looked up in a book what you = =0D wrote, and it seems you use references. Isn't that the same as using =0D pointers?=0D Anyway, in C, pointers are the only way.=0D =0D =0D Yes. I did not know what language was all about and i assumed C++.=0D =0D =0D Ioannis Vranos=0D =0D * Programming pages: http://www.noicys.freeurl.com=0D * Alternative URL: http://run.to/noicys=20 |