C++ :: Prevent Destructor Delete Member Pointer From Constructor
Dec 3, 2013
I have little problem which causing memory leaks.
Parent > Multiple Child(Parent parent) > Child destructor deleting parent => next Child destructor crash
Example code: without using:
class Parent {
public:
Parent() {
for(int i = 0; i < x; ++i) {
for(int j = 0; j < y; ++j)
childs[i][j] = new Child(this);
[Code] ....
If you read code, on Parent destructor i = 0 & j = 1 its going crash.
Parent will be deleted aswell, but it give me assert: _block_type_is_valid(phead- nblockuse)
View 3 Replies
ADVERTISEMENT
Feb 25, 2015
An attempt to create a class which is basically a mimic of vector<int> i don't seem to know how to delete pointer x in a destructor to free memory, also on pushback and pushfront methods, i can't free y when i implement delete[] y; y=NULL; i get some NULL out put when cout 'ing the object in main, why is that happening and how do i free memory y.
#include<iostream>
using namespace std;
class vectorOfint{
int* x;
int size;
public:
vectorOfint();
[Code] .....
View 6 Replies
View Related
Apr 17, 2013
I have following:
struct Point {int* a; int b;};
vector<vector<Point> > numbers;
vector<int> example;
The numbers vector has a matrix of a sort and each of the members are pointing to one member in the example vector. A member numbers.at(2).at(3).a is pointing at example.at(3). Now, can I remotely delete a member in the example vector using the pointers? Like so:
delete (*(numbers.at(2).at(3).a));
I know there is a more convenient way to delete members, but this is a very specific case I'm working on.
View 3 Replies
View Related
Nov 21, 2014
Class Car{
Private:
string* _ID;
bool _isFaulty;
int _location;
int _timer;
int _order;
vector<Road*> _roadPlan;
}
I want to implements Copy constructor (deep copy) and destructor, and I don't know how to treat the vector of pointers (Road is an object too)
Mor
Another thing, maybe I'll prefer using List instead of Vector, so i would like to see an implements of Copy constructor and destuctor with List too,
View 3 Replies
View Related
Apr 7, 2014
How can I use "delete[]" on all pointers on the stack, using a mixture of top and pop functions or variables
View 2 Replies
View Related
Aug 29, 2014
I have a query regarding virtual destructor functionality. So below is sample code:
Code:
#include<iostream>
#include<stdlib.h>
using namespace std;
class Base
Code: Base *b = d;
Here b and d now pointing to same memory location.
Now below statement:
Code: delete (b);
Here since my destructor is virtual it will call derived class and base class destructor.
Now my question is, if I use this:
Code: delete (d); // And without virtual keyword in ~Base() {} This call both derived and base class destructor.
So which one is correct form to call and why? Is delete(b) is standard in virtual function mechanism.
Output is:
D1 :: function1()
Base :: function2()
INSIDE D1 DES
INSIDE BASE DES
View 2 Replies
View Related
Mar 20, 2013
Lets say we have a class that holds a pointer member to another object. If I delete that pointer in the destructor I get an error (and I understand why). My question is : is it possible to overcome that without memory leaks ?
1 #include<iostream>
2 using namespace std;
3
4 class A {
5 public:
6 ~A() {
7 cout<< "~A()" <<endl;
[Code] ....
View 5 Replies
View Related
Jan 25, 2014
VS 2012 / Windows 7 64 bit
class
class myclass {
public:
myclass(){/*stuff here*/}
myclass(int* p) {MyPointer = p; myclass()}
[Code] ....
it works until the myclass(int* p) calls myclass()
then the MyPointer will become CCCCCCCC (NULL value)!
is there a way to stop the second constructor from resetting the pointer value to null?
View 3 Replies
View Related
Jun 27, 2013
Basically, I need to set a variable outside of the constructor and make it accessible to the entire class.
It would need to work something like this:
#include <iostream>
#include <string>
template <typename MT> class CallbackFunction
{
[Code].....
View 5 Replies
View Related
Nov 7, 2014
class Date
Date(int=1, int=1, int=1990);
class Person
Person(string="", string="", Date=NULL);
class RealEstateAgent:Public Person
RealEstateAgent(string="",string="",Date=NULL,Date=NULL,int=NULL, double=0.0);
}
[code]....
how can I assign default values with Customer object and RealEstateAgent?
View 4 Replies
View Related
Jan 23, 2015
I have a class that defines a window (a popup dialog of sorts), and I want the name of that window to be constant. The only problem is that the name of the popup needs to match the title of the parent window, and I get the name of the parent in the constructor. So how do I go about defining this member variable to be constant and initializing it with a value in the constructor?
I want to do something like this, but I know this isn't allowed:
/* class.h */
class foo {
public:
foo(*parentWindowPtr);
[Code] .....
I should mention that yes the name of the parent window is const char *, and I would like to keep it this way.
View 4 Replies
View Related
Jan 4, 2015
void myfuncion(){
int *a = new int[10];
int b[10];
int *p = b;
delete a
delete p
}
I cant delete b and/or p but how can i check it if i can use delete or not?
i want to check if the pointer is pointing on function temp variable ( those what gets deleted after function ends .
View 5 Replies
View Related
Sep 2, 2014
int * ptr = new int[5];
p += 2; //p now stores address of 3rd element in array
delete [] p;
what will be deleted??? all 5 elements or just 3rd, 4th and 5th elements of array(in result making a memory leak)
View 3 Replies
View Related
Apr 15, 2013
let's say I have class LinkedList:
class LinkedList
{
public:
class Node
[Code]....
Do delete newL:
1) (*newL).~LinkedList
2) delete newL
3) delete[] newL
4) none of the above ;)
If the answer is 4, what should I do?
View 5 Replies
View Related
Nov 24, 2014
I want to copy over values from x to y, and its not possible as i cant use "y" as a index in my pointer array.
Code:
CellularPhoneStock::CellularPhoneStock(CellularPhoneStock &origObj){
this->phonez[nrOfPhones]->setColour = origObj.getColour();
/*this->model = origObj.model;
this->nrOfType = origObj.nrOfType;
this->price = origObj.price;*/
nrOfPhones++;
}
My questions is, how can I use origObj and phonez combined to use the function getColour() ?
Btw, phones = CellularPhone * phonez[30];
View 14 Replies
View Related
Aug 7, 2014
In jumping into C++ it says something like this: It's not necessary but when you delete a pointer it's a good idea to reset it as a null pointer. That if your code try's to dereference the pointer after being freed, your program will crash. This happens to a lot of experienced programmers.
This could corrupt users data. delete p_int;
p_int = NULL;
1. If you can deference a pointer after the memory is freed, why can't you just delete the pointer?
2. If you can do 1, how do you delete the pointer using code?
3. Every thing I've read says that free memory is handed out in a sequenced order. I don't believe that is true at all. I may be wrong. Why can't you put the data in any number of places if it will fit. Isn't the compiler smart enough to know where bytes (bits)and pieces are stored?
4. If you storing anything in free memory must use a pointer to it?
5. Can a pointer or something similar be used with stack memory?
View 13 Replies
View Related
Feb 22, 2012
environment : qt creator 4.7
code:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
class singleTon {
[Code] ....
this is a singleton pattern first,it doesn't matter, why I could delete this pointer twice?because the gcc compiler?That mean in the surface, "delete pInstance1;" this movement just mark the memory pInstance1 has been deleted but not real?does any one encounter this phenomenon?
View 7 Replies
View Related
Jul 29, 2013
I'm tying to create a program that evaluates all possible actions for a certain problem.
So what i'm basically trying to do is to create a sequence of actions, evaluate them to check if it's the best sequence, change the sequence, evaluate again and so on until all possible scenarios are exhausted, leaving the best one in the end.
My approach to this at first was to create a tree of all possible options and then evaluate each branch. Since there are a lot of possible cases i ran out of memory while the program was still creating the tree. I changed this to create just a branch, evaluate it and then modify it.
I was still getting memory problems. I declared a class tNode and declared a vector<tNode*> branch. Then i created all the nodes i needed for that branch with branch.push_back( new tNode() ). When i wanted to modify the branch i simply used branch.pop_back() and again a branch.push_back( new tNode() ). I figured i was getting the problem because although i clear the vector, i don't actually clear the reference in memory. Is this correct? If so, how can i actually delete the memory space and not just the pointer in the vector?
View 6 Replies
View Related
Aug 27, 2013
I have a program that has a base class 'control' and there are 2 dervied classes 'button' and 'textbox'. How do i make a constructor in the 'button' or 'textbox' that initializes a pointer of the data type 'control' to point to the object that invokes the constructor. the code should look like this
class control {
//data
} class button:public control {
buton() {
//code for the constructor
}
}
actually i have an array of pointers of the type 'control' and as soon as any instance of a control like button or textbox is created the constructor should make an element of the array to point to the instance
View 7 Replies
View Related
Feb 19, 2013
I know my product.cpp constructor is wrong but how can I set my private variables.
//PRODUCT.h
#ifndef PROJECT1_PRODUCT_H
#define PROJECT1_PRODUCT_H
[Code].....
View 2 Replies
View Related
Dec 22, 2012
Goal: To allocate some memory as a char*, read in some binary data, re-interpret it as a float* and then free the memory.
My code looks like:
void someFunction(float* &result) {
char * tmp = new char[1000];
//...Fill the char buffer here...
result = (float*)tmp; //Reinterpret binary data as floats
[Code] ....
Is the cast back to char* necessary on the red line (or could I have validly left it as float*)? Would it be different if I had written char * tmp = (char*)malloc(sizeof(char)*1000); on the blue line (and correspondingly used free (char*)floatData on the red line?
View 9 Replies
View Related
Jun 6, 2012
I am trying to use "remove_if" with a predicate function inside a class. The code intends to remove the grid cells which an agent cannot move into (from among all possible cells).
Code:
void classname::function1()
{
vector<MoorePoint> neighbors;
....
[Code]....
That code would work if it was not in a class and the predicate was not a member function. However, now I receive long error messages which I guess refer to incompatibility of remove_if template with the predicate parameter (one error includes : error C2064: term does not evaluate to a function taking 1 arguments).
View 2 Replies
View Related
Jan 10, 2015
I have the following scenario :
Code:
class test {
public :
int num;
};
int main() {
test t1;
test *ptr = &t1;
int test :: *mem_ptr = &test::num;
}
I need to access mem_ptr (pointer to a class member) through :
a. object (t1)
b. pointer to the object (ptr).
How can i do that ?
View 4 Replies
View Related
May 15, 2013
I have a class with member functions and a pointer like this:
Code:
class MyClass{
private:
typedef void(MyClass::*memFnPtr_t) ();
public:
memFnPtr_t fnptr;
void fn1();
void fn2();
};
Now in a regular function I create an instance of the class and try to assign the pointer:
Code:
MyClass mc;
mc.fnptr = &mc.fn1;
The compiler does not like it and suggests this instead:
Code:
mc.fnptr = &MyClass::fn1;
This seems to work but what if I have two instances of the class:
Code:
MyClass mc1, mc2;
How does the compiler know to distinguish between
Code:
mc1.fn1
and
Code:
mc2.fn1
when the assignment now looks identical:
Code:
mc1.fnptr = &MyClass::fn1;
mc2.fnptr = &MyClass::fn1;
View 14 Replies
View Related
Jun 22, 2014
So, I've got this class in SDL Player that has, among other things, an SDL_Texture* to hold an image that represents the player on the screen. I'd assume it's good practice to do get() and set() functions for the class; but because textures are handled via pointers, when I write a get() function I end up returning a pointer to an internal resource; which isn't good practice I hear as it "breaks" encapsulation.
Find my code below:
#ifndef PLAYER_H
#define PLAYER_H
#include "SDL.h"
#include "SDL_image.h"
#include "CTexture.h"
class Player {
[Code] .....
View 1 Replies
View Related
Aug 19, 2014
I have the following problem: I am using NLOpt for optimization. The API provides functions to set the objective. This is done as follows:
double objective(const vector<double> &x, vector<double> &grad, void *data)
{
return x[1]*x[0];
}
int main(){
nlopt::opt opti(nlopt::LD_MMA,2);
opti.set_min_objective(objective,NULL);
vector<double> x(2);
[Code]....
Now I want to make the function objective a member of a class:
class Foo {
public:
double objective(...){..}
};
How can I give this method to opti.optimize? If I make objective static I can use
opti.optimize(Foo::objective,NULL);
but I do not want to have a static member. Is it possible to create an object of type Foo and give it to opti.optimize?
View 1 Replies
View Related