Solved

C++ Double Pointer

Posted on 2001-09-04
5
8,369 Views
Last Modified: 2009-05-16
I would like to know how delete operator will work on with double pointer...
0
Comment
Question by:logeswari_b
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 2

Expert Comment

by:abesoft
ID: 6455340
If you mean:
    X **p;
then delete will work fine for one of the items pointed at.  So, p points at a pointer to an X.  delete will delete the X, or the pointer to the X, but not both.  So typically you would do something like:
    p = new X*;
    *p = new X;
    // ... use p ...
    delete *p;
    delete p;
You need to think hard about what you are allocating and deleting.
0
 

Expert Comment

by:JBoschen
ID: 6455345
If you're referring to a pointer to a pointer, p**, delete will only operate on the first pointer. To delete both, you need to delete the second first, delete *p; then delete p;
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6456391
>> I would like to know how delete operator will work on with double pointer...

Just to cover the bases:
Deleting a pointer to a double works just like deleting a pointer to anything else -- it frees up the allocation.

   double* pFloatingPtNum= new double;
   *pFloatingPtNum= 0.123456789;

   delete pFloatingPtNum; // no problemo!

=-=-=-=-=-=-=-=-=-
I have a nearly irresitible urge to lock this question.

-- Dan
0
 
LVL 2

Expert Comment

by:abesoft
ID: 6457297
Aaaagh
0
 
LVL 30

Accepted Solution

by:
Axter earned 100 total points
ID: 6457932
I'm assuming that you are reffering to (**) type of double pointers, which is a pointer array.
Pointer array variables are used for an array of pointers.

The array of pointers are create with the new[] operator.
Since the new[] operator is used to create the array, the delete[] operator should be used to delete the double pointer array.
If you use the plain delete operator, this can cause memory leaks in your program.

The following is an example on how to use a pointer array variable.

void CreateAndDestroyA_Pointer_Array(int QtyPointers)
{
     int i;
     //Create an array of pointers
     int **List = new int*[QtyPointers];
     
     //Create an object (int) for each pointer
     for (i = 0;i < QtyPointers;i++)
     {
          List[i] = new int;
     }

     //Assign a value to each item in the list
     for (i = 0;i < QtyPointers;i++)
     {
          *List[i] = i*11;
     }

     //Delete each object
     for (i = 0;i < QtyPointers;i++)
     {
          delete List[i];
     }

     //Delete the array of pointers
     delete [] List;
}


int main(int argc, char* argv[])
{
     CreateAndDestroyA_Pointer_Array(15);
     return 0;
}

You'll notice in the above example, that before using the delete[] operator, first each individual pointer was deleted.  Then the pointer array was deleted.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sorting efficency of sorting algorithm 30 149
Least Squares Curve Fitting 4 133
Create a path if not exists 7 125
Winapi.Windows.hpp problem 7 44
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question