470,870 Members | 1,387 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,870 developers. It's quick & easy.

to improve programming in pointers

how i can improve my programming skill in pointer using c++

Feb 25 '06 #1
10 1471
by more reading and practices.

Ben
Feb 25 '06 #2
si***********@gmail.com wrote:
how i can improve my programming skill in pointer using c++


Go find the answers to these questions.
struct A { int a; };

struct B : A { int b; };

A xa[ 10 ];
B xb[ 10 ];

A * pa = xb;
// Q1 - what is the value of pa pointing to ?

A * pBAD = pa + 1;
// Q2 - why is pBAD, bad ?

B * pb = xb;
// - same as Q1

A * paOK = pb + 1;
// Q3 - what does paOK point to ?
A (&( * pf )( int * ))[ 10 ];
// Q4 - what is pf ?

A * paX = & pb[1];

bool b( paX == paOK );
// Q5 - what is the value of b

A * paY = & pb[3];

int iNOTGOOD( paY - paX );
// Q6 - Why is iNOTGOOD not good ?

A * paM = new B;

int main()
{

delete paM; // line Z - NOT GOOD
// Q7 - why is line Z a bad thing ?

paM = 0;
// Q8 - what is paM now ?

delete paM;
// Q9 - what does delete paM do ?

bool x( & (* xb).b == & xb->b );
// Q10 - What is the value of x ?
}

B * F()
{
B x[2];

return x; // doh !
// Q11 - Why is the line marked "doh" broken ?
}

Feb 25 '06 #3
si***********@gmail.com a écrit :
how i can improve my programming skill in pointer using c++


You shouldn't even use pointers.
Avoid them as much as possible and only use them when it's needed.
Feb 25 '06 #4
loufoque wrote:
si***********@gmail.com a écrit :
how i can improve my programming skill in pointer using c++


You shouldn't even use pointers.
Avoid them as much as possible and only use them when it's needed.


At which point he won't know how to use them.

Brian

--
If televison's a babysitter, the Internet is a drunk librarian who
won't shut up.
-- Dorothy Gambrell (http://catandgirl.com)
Feb 25 '06 #5
Default User wrote:
loufoque wrote:

si***********@gmail.com a écrit :
how i can improve my programming skill in pointer using c++


You shouldn't even use pointers.
Avoid them as much as possible and only use them when it's needed.

At which point he won't know how to use them.

But he may have developed sufficient skill in in avoiding them so he
doesn't have to!

Are there any (real world) instances where pointers can't be avoided?

--
Ian Collins.
Feb 25 '06 #6
Ian Collins wrote:
....

Are there any (real world) instances where pointers can't be avoided?


Let's see:

a) the "this" keyword.

b) When you may or may not want to pass an interface ... e.g.

void DoThing( const A & a, ErrorReporter * e = 0 );

c) Intrusive reference counted objects.

d) When objects point to one another i.e. a<->b

+ many more
Feb 25 '06 #7
Gianni Mariani wrote:
Ian Collins wrote:
....

Are there any (real world) instances where pointers can't be avoided?

Let's see:

a) the "this" keyword.

b) When you may or may not want to pass an interface ... e.g.

void DoThing( const A & a, ErrorReporter * e = 0 );

c) Intrusive reference counted objects.

d) When objects point to one another i.e. a<->b

+ many more


With the exception of a, all these can be replace by some form of smart
pointer object.

--
Ian Collins.
Feb 26 '06 #8
Ian Collins wrote:
....

With the exception of a, all these can be replace by some form of smart
pointer object.


You still need to understand pointers even if you're managing them using
smart pointers.


Feb 26 '06 #9
Gianni Mariani wrote:
Ian Collins wrote:
....

With the exception of a, all these can be replace by some form of
smart pointer object.

You still need to understand pointers even if you're managing them using
smart pointers.

True enough, but I was trying to think of an example where raw pointers
can't be replaced with smart pointers.

--
Ian Collins.
Feb 26 '06 #10
> True enough, but I was trying to think of an example where raw pointers
can't be replaced with smart pointers.


When you are writing your own smart pointers? Perhaps?

Ben
Feb 26 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

80 posts views Thread by Bibby | last post: by
3 posts views Thread by Anand | last post: by
134 posts views Thread by evolnet.regular | last post: by
43 posts views Thread by Sensei | last post: by
reply views Thread by sivashankar21 | last post: by
16 posts views Thread by weidongtom | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.