I need to write a function that will use the delete command on a pointer. I'm in left field. See what I have so far any suggestions. I don't think I'm quite getting what I need to pass to the function and pass back.

#include <iostream>
#include "cdrom.h"

using namespace std;

int main ()
{
        // Declares 3 CDROM Pointers
    CDROM *pCDROM1;
    CDROM *pCDROM2;
    CDROM *pCDROM3;
    pCDROM1 = new CDROM;
    pCDROM2 = new CDROM;
    pCDROM3 = new CDROM;


        //Check For Dynamic Memory Allocation
    if (pCDROM1 == NULL)
        {
        cout << "Dynamic Memory Allocation FAILED" << endl;
        }

    if (pCDROM2 == NULL)
        {
        cout << "Dynamic Memory Allocation FAILED" << endl;
        }

    if (pCDROM3 == NULL)
        {
        cout << "Dynamic Memory Allocation FAILED" << endl;
        }


        // Loads each CDROM with data
    pCDROM1->loadInfo();
    pCDROM2->loadInfo();
    pCDROM3->loadInfo();

    pCDROM1->displayInfo();
    pCDROM2->displayInfo();
    pCDROM3->displayInfo();


        // calls global function show cost
        // showcost();

        // Changes data on CDROM 1 and CDROM 2
    pCDROM1->changeData();
    pCDROM2->changeData();

        // Redisplay CDROM with New Data
    pCDROM1->displayInfo();
    pCDROM2->displayInfo();
    pCDROM3->displayInfo();

        // Call giveMemoryBack function
    int giveMemoryBack(int *pCDROM1, int *pCDROM2, int *pCDROM3);
    return 0;
}

int giveMemoryBack (int *pCDROM, int *pCDROM2, int *pCDROM3)
{
    delete *pCDROM1  
    delete *pCDROM2
    delete *pCDROM3
    return 0;
}

Your giveMemoryBack function should be

int giveMemoryBack (int *pCDROM, int *pCDROM2, int *pCDROM3)
{
delete pCDROM1
delete pCDROM2
delete pCDROM3
return 0;
}

Note: Remember to set the original pointers back to NULL.

Found something else, your function call

// Call giveMemoryBack function
int giveMemoryBack(int *pCDROM1, int *pCDROM2, int *pCDROM3);

is incorrect. It should be.

// Call giveMemoryBack function
giveMemoryBack(pCDROM1, pCDROM2, pCDROM3);

I thought CDROM was an object not integer? You should use

int deleteCD(CDROM *one, CDROM *two) { 
delete one;
delete two;
return 0;

I thought CDROM was an object not integer? You should use

int deleteCD(CDROM *one, CDROM *two) { 
delete one;
delete two;
return 0;

Ooops missed that.

Sorry guys I guess I am not following. Yes CDROM is an object. So based on what I understand this would be correct but dose not seem to work

// Call giveMemoryBack function
    int giveMemoryBack(CDROM int *pCDROM1, int *pCDROM2, int *pCDROM3);    return 0;

    return 0;
}

 int giveMemoryBack (CDROM int *pCDROM1, int *pCDROM2, int *pCDROM3)
{
    delete CDROM;
    delete pCDROM1;
    delete pCDROM2;
    delete pCDROM3;
    return 0;
}

As I understand it in this case. Not only would one want to delete the pointers but the object as well.

Your function prototype is incorrect..You have

int giveMemoryBack (CDROM int *pCDROM1, int *pCDROM2, int *pCDROM3)

and it should be

int giveMemoryBack (CDROM *pCD1, CDROM *pCD2, CDROM *pCD3)
{
delete pCD1;
delete pCD2;
delete pCD3;
return 0;
}

and calling your function should be

// Call giveMemoryBack function
giveMemoryBack(pCDROM1, pCDROM2, pCDROM3);

Ah ok gotcha, This is because in this case the "CDROM" becomes the type right? This makes since but I"m still get an error

giveMemoryBack(pCDROM1, pCDROM2, pCDROM3);
    return 0;

}

int giveMemoryBack (CDROM *pCD1, CDROM *pCD2, CDROM *pCD3)
{
    delete pCD1;
    delete pCD2;
    delete pCD3;
    return 0;
}

on the call I get
use of undeclared identifier

on the prototype I get
no previous prototype function

Member Avatar for MonsieurPointer

Did you declare your prototype in your header file cdrom.h or in your cpp file?

int giveMemoryBack (CDROM *pCD1, CDROM *pCD2, CDROM *pCD3);
//Check For Dynamic Memory Allocation
if (pCDROM1 == NULL)
        {        cout << "Dynamic Memory Allocation FAILED" << endl;        }

When new fails, it does not set the pointer to NULL. It throws an exception. There is a nothrow version of new that will return NULL.

http://www.cplusplus.com/reference/std/new/operator%20new/

Did you declare your prototype in your header file cdrom.h or in your cpp file?

int giveMemoryBack (CDROM *pCD1, CDROM *pCD2, CDROM *pCD3);

Thanks this is exactly what I forgot. All is working now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.