473,699 Members | 2,927 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointer to Pointer or not?

Hi

I am creating my own Queue class to learn about Queues and pointers.

I have come across a question of two styles and I don't know if there
are any dangers associated with them.

I coded my remove function as follows

template <class T>
bool adsQ<T>::Remove (T* theData)
{
Qnode<T>* removeNode = 0;

if (true == isEmpty())
{
//cout << "False" << endl;
return false;
}
else
{
/* What I am trying to achive here is to change the data pointed to
by theData to the data pointed to by pData.*/

*theData =*(pHead->pData); //Data exchanged

removeNode = pHead;

if (count == 1)
{
pHead = 0; //Pointers to Null
pTail = 0;
}
else
{
pHead = pHead->nextQnode;
}

count--;

/*I then free up the memory which contained the pHead, pData.
Knowing that *theData now contains the same data as pData did*/

delete removeNode;

removeNode = 0; //Make pointer safe

//cout << "True" << endl;
return true;
}
}

I then read about pointers to pointers and amended my function signature

template <class T>
bool adsQ<T>::Remove (T** theData) ...

and the line where the data is exchanged.

*theData = pHead->pData;

And called the function thus

pQ->Remove(&pD);

I now appear to get the same results from both, the returning of the
data contained in the pointer pData outwith the function.

However in changing the function to take a pointer to pointer am I
storing up trouble for myself in that as the free space pointed to by
pData is now pointed at by pD outwith the function (is this right?) and
also pData within the function, can I now delete it within the function
(through removeNode) safely or is this trouble as I appear to be
deleting memory pointed at by two pointers.

TYIA

nifsmith
Jul 22 '05 #1
2 1362
On Sun, 3 Oct 2004, nifsmith wrote:
Hi

I am creating my own Queue class to learn about Queues and pointers.

I have come across a question of two styles and I don't know if there
are any dangers associated with them.

I coded my remove function as follows

template <class T>
bool adsQ<T>::Remove (T* theData)
{
Qnode<T>* removeNode = 0;

if (true == isEmpty())
{
//cout << "False" << endl;
return false;
}
else
{
/* What I am trying to achive here is to change the data pointed to
by theData to the data pointed to by pData.*/

*theData =*(pHead->pData); //Data exchanged

removeNode = pHead;

if (count == 1)
{
pHead = 0; //Pointers to Null
pTail = 0;
}
else
{
pHead = pHead->nextQnode;
}

count--;

/*I then free up the memory which contained the pHead, pData.
Knowing that *theData now contains the same data as pData did*/

delete removeNode;

removeNode = 0; //Make pointer safe

//cout << "True" << endl;
return true;
}
}

I then read about pointers to pointers and amended my function signature

template <class T>
bool adsQ<T>::Remove (T** theData) ...

and the line where the data is exchanged.

*theData = pHead->pData;

And called the function thus

pQ->Remove(&pD);

I now appear to get the same results from both, the returning of the
data contained in the pointer pData outwith the function.

However in changing the function to take a pointer to pointer am I
storing up trouble for myself in that as the free space pointed to by
pData is now pointed at by pD outwith the function (is this right?) and
also pData within the function, can I now delete it within the function
(through removeNode) safely or is this trouble as I appear to be
deleting memory pointed at by two pointers.

TYIA

nifsmith


In the first aproach you are dereferreing the pointer and assignin to
instance it points. It'll call assign operator of type it points to. So
mem will be copied and you can delete original 'safely'.

In second version done by pointers to pointers only the address of
instance is assigned. Deleting original now would result in a pointer to
free mem.

I hope I'm making sense to you.
Jul 22 '05 #2

"nifsmith" <ha*********@ly cos.co.uk> wrote in message
news:cj******** **@titan.btinte rnet.com...
Hi

I am creating my own Queue class to learn about Queues and pointers.

I have come across a question of two styles and I don't know if there are
any dangers associated with them.

I coded my remove function as follows

template <class T>
bool adsQ<T>::Remove (T* theData)
{
Qnode<T>* removeNode = 0;

if (true == isEmpty())
{
//cout << "False" << endl;
return false;
}
else
{
/* What I am trying to achive here is to change the data pointed to by
theData to the data pointed to by pData.*/

*theData =*(pHead->pData); //Data exchanged

removeNode = pHead;

if (count == 1)
{
pHead = 0; //Pointers to Null
pTail = 0;
}
else
{
pHead = pHead->nextQnode;
}

count--;

/*I then free up the memory which contained the pHead, pData. Knowing that
*theData now contains the same data as pData did*/

delete removeNode;

removeNode = 0; //Make pointer safe

//cout << "True" << endl;
return true;
}
}

I then read about pointers to pointers and amended my function signature

template <class T>
bool adsQ<T>::Remove (T** theData) ...

and the line where the data is exchanged.

*theData = pHead->pData;

And called the function thus

pQ->Remove(&pD);

I now appear to get the same results from both, the returning of the data
contained in the pointer pData outwith the function.

However in changing the function to take a pointer to pointer am I storing
up trouble for myself in that as the free space pointed to by pData is now
pointed at by pD outwith the function (is this right?) and also pData
within the function, can I now delete it within the function (through
removeNode) safely or is this trouble as I appear to be deleting memory
pointed at by two pointers.


You are getting into trouble with your pointer to pointer method. You are
deleting memory in your Remove function which is still accessible from
outside your Remove function. This is likely to crash your program.

This is a design issue, and the issue is, who owns the pointers. If you
consider that the queue owns the pointers then it should delete them and you
should go back to your original method. Alternatively you could say that the
program using the queue owns the pointers, in this case you can use your
pointer to pointer method, but then the queue should not delete the memory,
that would now be the calling programs responsibility.

You don't say how the memory is allocated originally, is that done in the
queue class, or outside the queue class, but I would guess that the
allocation is being done in the queue (anything else would be poor design).
In that case it seems natural for the queue to own the pointers and do the
deallocation itself. That way the user of the queue doesn't have to worry
about memory allocation at all, which make the queue easier to use.

So, in short, I would say that you should go back to your original idea.

john
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2139
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived classes
110
9926
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object must be an object instead of
3
2357
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
35
2887
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL */ memset(TextureImage,0,sizeof(void *)*1); /* Line 2*/ According to my knowledge in the first line
16
2301
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the subject pointer does not refer to an object suitably aligned in storage. It is guaranteed that a pointer to an object may be converted to a pointer to an object whose type requires less or equally strict storage alignment and back again without change;...
204
13022
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 = {0,1,2,4,9};
16
2508
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
23
7809
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when called, can be a genuine no-op. Consider: typedef int(*polymorphic_func)(int param);
69
5569
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after assigning them their maximum defined values. However, the output of printf() for the long double 'ld' and the pointer of type void 'v_p', after initialisation don't seem to be right. The compiler used was gcc (mingw) with '-Wall', '-std=c99' and
8
2233
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
0
8623
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9192
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8895
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7781
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5879
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.