473,378 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Pointer deletion question

Hi, when I try and delete a pointer in another class I get out of
memory errors. Why does this fail? Is it not ok to do this?

A.cpp
void A::foo(){
new MyObject *pObj = new MyObject;
B b;
b.foo2(pObj);
}
///
B.cpp
void B:foo2(MyObject *pObj){
//do something
delete pObj;
}

Feb 16 '07 #1
4 1087
Marcus wrote:
Hi, when I try and delete a pointer in another class I get out of
memory errors. Why does this fail? Is it not ok to do this?

A.cpp
void A::foo(){
new MyObject *pObj = new MyObject;
B b;
b.foo2(pObj);
}
///
B.cpp
void B:foo2(MyObject *pObj){
//do something
delete pObj;
}
It's legal to do it, although it's not usually considered good style.
But it fails for you because you have a bug somewhere in your program.

As is commonly the case the bug is in the code you didn't post. See
Thomas Tutone's post in the thread 'Multiple substrings = Error' for a
very good explanation of how to post programming problems (and look at
the rest of the thread for how much time gets wasted when you don't
follow these guidelines).

john
Feb 16 '07 #2
On Feb 15, 11:39 pm, John Harrison <john_androni...@hotmail.com>
wrote:
Marcus wrote:
Hi, when I try and delete a pointer in another class I get out of
memory errors. Why does this fail? Is it not ok to do this?
A.cpp
void A::foo(){
new MyObject *pObj = new MyObject;
B b;
b.foo2(pObj);
}
///
B.cpp
void B:foo2(MyObject *pObj){
//do something
delete pObj;
}

It's legal to do it, although it's not usually considered good style.
But it fails for you because you have a bug somewhere in your program.

As is commonly the case the bug is in the code you didn't post. See
Thomas Tutone's post in the thread 'Multiple substrings = Error' for a
very good explanation of how to post programming problems (and look at
the rest of the thread for how much time gets wasted when you don't
follow these guidelines).

john- Hide quoted text -

- Show quoted text -
Sorry if you misunderstood... my intention wasn't to have the group
debug my code, I was just wondering if it was legal to do that. Now
that I know that it's ok, I can look elsewhere for my problem. Thank
you for the information, very much appreciated.

Feb 16 '07 #3
"Marcus" <mc*******@walla.comwrites:
A.cpp
void A::foo(){
new MyObject *pObj = new MyObject;
^^^
Is this "new" a typo or have you used it deliberately?

Thomas
Feb 16 '07 #4
On Feb 16, 8:29 am, Thomas Hühn <newsgro...@thomas-huehn.dewrote:
"Marcus" <mcdesi...@walla.comwrites:
A.cpp
void A::foo(){
new MyObject *pObj = new MyObject;

^^^
Is this "new" a typo or have you used it deliberately?

Thomas
Yeah, sorry that's a typo.

Feb 16 '07 #5

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

Similar topics

2
by: Bryan Parkoff | last post by:
I create one 32 Bits variable and four pointer variables. Four pointer variables link or point to one 32 Bits variable. Each pointer variable is 8 Bits. Look at my example below. unsigned int...
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
6
by: m sergei | last post by:
main(int argc, char *argv) the second parameter to function main takes a pointer to an array. can we also write it in terms of a reference rather than a pointer ? an example of using (usage)...
25
by: No Such Luck | last post by:
Hi all: Below are two pieces of code that basically perform the same task. Version A produces a segmentation fault, while version B works correctly. I understand why version B works correctly,...
23
by: Kenneth Brody | last post by:
Given the following: char *ptr1, *ptr2; size_t n; ptr2 = ptr1 + n; Assuming ptr1 is a valid pointer, is the following guaranteed to be true? (ptr2 - ptr1) == n
6
by: haloman | last post by:
Why does: char *myString = (char *) malloc(WORDLIMIT*sizeof(char)); fscanf(openFile, "%s", myString) != EOF; work yet char *myString = (char *) malloc(WORDLIMIT*sizeof(char));...
4
by: srini4vasan | last post by:
Can we delete 'this' pointer in C++. If so how can we delete this pointer? If not why?
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
4
by: jthep | last post by:
If I have a pointer say int **pa that stores the address to another pointer int *pb, pa would be storing the address of pb which contains an address to lets say int c = 10. Picking an arbitrary...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.