473,387 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1629
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.