to improve programming in pointers

G

Gianni Mariani

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 ?
}
 
L

loufoque

(e-mail address removed) 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.
 
D

Default User

loufoque said:
(e-mail address removed) a écrit :

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
 
I

Ian Collins

Default said:
loufoque wrote:




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?
 
G

Gianni Mariani

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
 
I

Ian Collins

Gianni said:
Ian Collins wrote:
....



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.
 
G

Gianni Mariani

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.
 
I

Ian Collins

Gianni said:
Ian Collins wrote:
....



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.
 
B

benben

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top