472,955 Members | 2,662 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,955 software developers and data experts.

to improve programming in pointers

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

Feb 25 '06 #1
10 1609
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Casper | last post by:
Is there features/containers of standard C++ or the STL which will assist in minimizing memmory fragmentation upon creation of a huge ammount of objects on the heap? Currently my application...
80
by: Bibby | last post by:
Hi, I'm interested in getting started in the programming world. I've dabbled in C, C++ and VB6. Which would be the best language to focus my attention to regarding the following considerations: ...
3
by: Anand | last post by:
Hi Iam keen interested to improve my skills in c programming. Now Iam familiar with all the syntax and concepts. My main idea is to know how entire c compiler behaves in all circumstances(i.e...
3
by: user | last post by:
Hi all, At the outset, I regret having to post this slightly OT post here. However, I strongly feel that people in this group would be the best to advise me on my predicament. I am working as...
134
by: evolnet.regular | last post by:
I've been utilising C for lots of small and a few medium-sized personal projects over the course of the past decade, and I've realised lately just how little progress it's made since then. I've...
43
by: Sensei | last post by:
Hi! I'm thinking about a good programming style, pros and cons of some topics. Of course, this has nothing to do with indentation... Students are now java-dependent (too bad) and I need some...
0
by: Laurence Parry | last post by:
Some people over on Wikipedia seem to have the idea that VB.NET is not a major programming language: http://en.wikipedia.org/wiki/Template_talk:Major_programming_languages_small#Visual_Basic_.NET ...
0
by: sivashankar21 | last post by:
how i can improve my programming skill in pointer using c++
16
by: weidongtom | last post by:
Hi, I have just finished reading some tutorials on C, I am wondering how I could improve my skill. Is there any advice? Is reading others' codes the best way? If so, what type of codes are...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
1
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.