What is a dangling pointer?
A dangling pointer arises when you use the address of an object after
its lifetime is over. This may occur in situations like returning
addresses of the automatic variables from a function or using the
address of the memory block after it is freed. The following
code snippet shows this:
class Sample{
public:
int *ptr;
Sample(int i){
ptr = new int(i);}
~Sample(){
delete ptr;}
void PrintVal(){
cout << "The value is " << *ptr;}
};
void SomeFunc(Sample x){
cout << "Say i am in someFunc " << endl;}
int main(){
Sample s1 = 10;
SomeFunc(s1);
s1.PrintVal();}
In the above example when PrintVal() function is
called it is called by the pointer that has been freed by the
destructor in SomeFunc.
Showing posts with label pointers. Show all posts
Showing posts with label pointers. Show all posts
Tuesday, May 18, 2010
Constant Pointers and Pointers to Constants
Consider the following declaration:
char A_char = 'A'; char * myPtr = &A_char;
This is a simple declaration of the variable myPtr. myPtr is a pointer to a character variable and in this case points to the character 'A'.Don't be confused about the fact that a character pointer is being used to point to a single character—this is perfectly legal! Not every character pointer has to point to a string.Now consider the following three declarations assuming that char_A has been defined as a type char variable.:const char * myPtr = &char_A;
char * const myPtr = &char_A;
const char * const myPtr = &char_A;
What is the difference between each of the valid ones? Do you know?They are all three valid and correct declarations. Each assigns the addres of char_A to a character pointer. The difference is in what is constant.The first declaration:const char * myPtr
declares a pointer to a constant character. You cannot use this pointer to change the value being pointed to:char char_A = 'A';
const char * myPtr = &char_A;
*myPtr = 'J'; // error - can't change value of *myPtr
The second declaration,char * const myPtr
declares a constant pointer to a character. The location stored in the pointer cannot change. You cannot change where this pointer points:char char_A = 'A';
char char_B = 'B';
char * const myPtr = &char_A;
myPtr = &char_B; // error - can't change address of myPtr
The third declares a pointer to a character where both the pointer value and the value being pointed at will not change.Pretty simple, but as with many things related to pointers, a number of people seem to have trouble.
Thursday, May 6, 2010
Pointer-to-Member Operators: .* and ->*
expression .* expression expression –>* expression
The pointer-to-member operators, .* and –>*, return the value of a specific class member for the object specified on the left side of the expression. The right side must specify a member of the class. The following example shows how to use these operators:
// expre_Expressions_with_Pointer_Member_Operators.cpp
// compile with: /EHsc
#include
using namespace std;
class Testpm {
public:
void m_func1() { cout << "m_func1\n"; }
int m_num;
};
// Define derived types pmfn and pmd.
// These types are pointers to members m_func1() and
// m_num, respectively.
void (Testpm::*pmfn)() = &Testpm::m_func1;
int Testpm::*pmd = &Testpm::m_num;
int main() {
Testpm ATestpm;
Testpm *pTestpm = new Testpm;
// Access the member function
(ATestpm.*pmfn)();
(pTestpm->*pmfn)(); // Parentheses required since * binds
// less tightly than the function call.
// Access the member data
ATestpm.*pmd = 1;
pTestpm->*pmd = 2;
cout << ATestpm.*pmd << endl
<< pTestpm->*pmd << endl;
delete pTestpm;
}
Output
m_func1
m_func1
1
2
In the preceding example, a pointer to a member, pmfn, is used to invoke the member function m_func1. Another pointer to a member, pmd, is used to access them_num member.
The binary operator .* combines its first operand, which must be an object of class type, with its second operand, which must be a pointer-to-member type.
The binary operator –>* combines its first operand, which must be a pointer to an object of class type, with its second operand, which must be a pointer-to-member type.
In an expression containing the .* operator, the first operand must be of the class type of, and be accessible to, the pointer to member specified in the second operand or of an accessible type unambiguously derived from and accessible to that class.
In an expression containing the –>* operator, the first operand must be of the type "pointer to the class type" of the type specified in the second operand, or it must be of a type unambiguously derived from that class.
For more on Func-pointer
http://www.newty.de/fpt/fpt.html
Friday, January 29, 2010
Everything about the this pointer (Basics for rookies)
The this pointer is used as a pointer to the class object instance by the member function. The address of the class instance is passed as an implicit parameter to the member functions. The sample below, in this c++ Tutorial shows how to use it. It is a common knowledge that C++ keeps only one copy of each member function and the data members are allocated memory for all of their instances. This kind of various instances of data are maintained use this pointer. Look at the sample below, in this c++ Tutorial.
Important notes on this pointer:
1. this pointer stores the address of the class instance, to enable pointer access of the members to the member functions of the class.
2. this pointer is not counted for calculating the size of the object.
3. this pointers are not accessible for static member functions.
4. this pointers are not modifiable.
Look at the following example to understand how to use the 'this' pointer explained in this C++ Tutorial.
class this_pointer_example // class for explaining C++ tutorial
{
int data1;
public:
//Function using this pointer for C++ Tutorial
int getdata()
{
return this->data1;
}
//Function without using this pointer
void setdata(int newval)
{
data1 = newval;
}
};
Thus, a member function can gain the access of data member by either using this pointer or not.
Important notes on this pointer:
1. this pointer stores the address of the class instance, to enable pointer access of the members to the member functions of the class.
2. this pointer is not counted for calculating the size of the object.
3. this pointers are not accessible for static member functions.
4. this pointers are not modifiable.
Look at the following example to understand how to use the 'this' pointer explained in this C++ Tutorial.
class this_pointer_example // class for explaining C++ tutorial
{
int data1;
public:
//Function using this pointer for C++ Tutorial
int getdata()
{
return this->data1;
}
//Function without using this pointer
void setdata(int newval)
{
data1 = newval;
}
};
Thus, a member function can gain the access of data member by either using this pointer or not.
Thursday, December 24, 2009
Funny "C"- Weird Pointers
You have a declaration:
int* ptr_a, ptr_b;
Given this, what is the type of ptr_b? int *, right?
Wrong!
C’s declaration syntax ignores the pointer asterisks when carrying a type over to multiple declarations. If you split the declaration of ptr_a and ptr_b into multiple statements, you get this:
int *ptr_a;
int ptr_b;
It’s possible to do the single-line declaration in a clear way. This is the immediate improvement:
int *ptr_a, ptr_b;
Notice that the asterisk has moved. It is now right next to the word ptr_a. A subtle implication of association.
Finally, I should point out that you can do this just fine:
int *ptr_a, *ptr_b;
int* ptr_a, ptr_b;
Given this, what is the type of ptr_b? int *, right?
Wrong!
C’s declaration syntax ignores the pointer asterisks when carrying a type over to multiple declarations. If you split the declaration of ptr_a and ptr_b into multiple statements, you get this:
int *ptr_a;
int ptr_b;
It’s possible to do the single-line declaration in a clear way. This is the immediate improvement:
int *ptr_a, ptr_b;
Notice that the asterisk has moved. It is now right next to the word ptr_a. A subtle implication of association.
Finally, I should point out that you can do this just fine:
int *ptr_a, *ptr_b;
Subscribe to:
Posts (Atom)