943,732 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 588
  • C++ RSS
Oct 19th, 2008
0

any articles or books about pointer?

Expand Post »
i am confused by pointers. How do we know the type the pointer points? Is it compiler specific? how do we implement delete[]? Are there any books or articles about this topic? Wait for your replies.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
panpanf is offline Offline
18 posts
since Oct 2008
Oct 19th, 2008
1

Re: any articles or books about pointer?

Seek and thou shalt find.

>>How do we know the type the pointer points?
You have to declare the data type when the pointer is created. int* iPointer;
>>Is it compiler specific?
No. Pointers are defined by C and C++ standards, so all compilers comply.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Oct 19th, 2008
1

Re: any articles or books about pointer?

Here's a silly little illustration I drew just now:
Attached Thumbnails
Click image for larger version

Name:	pointer_illustration.jpg
Views:	25
Size:	72.7 KB
ID:	7834  
Reputation Points: 92
Solved Threads: 16
Junior Poster
chococrack is offline Offline
149 posts
since Oct 2008
Oct 19th, 2008
0

Re: any articles or books about pointer?

http://www.eternallyconfuzzled.com/t..._pointers.aspx

Here is another llink that would help.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Oct 20th, 2008
0

Re: any articles or books about pointer?

when we use int *p=new int; we know p point to int. But i just wanna know how the compiler know p's type? When we delete a pointer to a array, how the compiler know how many bytes to release?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
panpanf is offline Offline
18 posts
since Oct 2008
Oct 20th, 2008
0

Re: any articles or books about pointer?

Click to Expand / Collapse  Quote originally posted by panpanf ...
when we use int *p=new int; we know p point to int. But i just wanna know how the compiler know p's type?
Because you've told it so. int *p tells the compiler that p is a pointer to int. The compiler also knows that the expression "new int" yields a value of type pointer to int, that can stored in the pointer p.

If you want a self-respecting compiler to complain bitterly, tell it "int *p = new double;" : it recognises that "new double" does not yield a pointer to int, and complains about a mismatch of types.
Click to Expand / Collapse  Quote originally posted by panpanf ...
When we delete a pointer to a array, how the compiler know how many bytes to release?
The compiler doesn't. All the compiler knows is that it's asked to delete an array (as in "delete [] whatever;") because the [] tells it that.

When dynamically allocating an array, you specifically tell it how many elements to allocate. There is nothing stopping code that manages memory allocation from keeping track of things, and retrieving the value when working with the delete operator. The methods of doing that, however, vary between compilers. And there is no portable way for your code to retrieve that value (unless you also keep track of such things yourself).
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Oct 20th, 2008
0

Re: any articles or books about pointer?

Oh; another tutorial on pointers is here.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Syntax Error
Next Thread in C++ Forum Timeline: visual c++ 2005 how to resolve?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC