C++ :: Vector Of Int Class - How To Delete Pointer X In Destructor To Free Memory

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


ADVERTISEMENT

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 View Related

C++ :: Validity Of New / Delete Pair (or Malloc / Free) After Pointer Casting

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

C :: Malloc Is Used To Allocate Free Memory To A Pointer

Nov 5, 2014

There is a part in the lesson that explains how malloc is used to allocate free memory to a pointer and gives us 2 examples:

Code:

float *ptr = malloc( sizeof(*ptr) ); and Code: float *ptr;
ptr = malloc( sizeof(*ptr) );

From my logic in the first case we allocate the memory to *ptr and in the second to ptr.

It's a bit confusing, am I missing something?

View 14 Replies View Related

C++ :: Base Class Pointer Destructor

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

C++ :: Destructor For Class That Holds Pointer To Object

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

C++ :: Sequence Of Actions - Delete Pointer In Vector

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

C++ :: Delete Member Of Vector That Is Pointed By Pointer?

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

C++ :: Merge Sort Destructor - Using Delete On All Pointers On Stack

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

C :: Free A String Literal From Memory?

Jan 8, 2014

This compiles o.k.:

Code:

int main(void){
char *a;
a = (char*) malloc (100*sizeof(char));

[Code]....

I get an error saying "pointer being freed was not allocated". This happens for free(a), free(*a), free(&a), free(&*a).

So if I no longer need "1234567"... how do I get rid of this memory element?

View 7 Replies View Related

C :: Free Allocated Memory For Structure?

Mar 16, 2014

I'm trying to free allocated memory for structure. It seems like free() gets only pointer and not regular types . my question is basic and simple – is passing pointer to free() frees the pointer or the variable it points at? or both?

View 2 Replies View Related

C :: Free Not Working After Malloc Was Used To Allocate Memory

Jun 13, 2014

Consider this program:

Code:

// sb_string class v1.04

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct sb_string {

[Code] ....

And here is the output I got:

Code:
[harshvardhan@hari-rudra] ~/Desktop% gcc49 -o test test.c
[harshvardhan@hari-rudra] ~/Desktop% ./test
-before Value of len = 1
(in_function)-before Value of len = 1
(in_function)-after Value of len = 1

-after Value of len = 1 I was trying to make a little easier to work with string. Once the memory is allocated by malloc via sb_init() function, the sb_massacre function wasn't working to deallocate the memory. I had used multiple versions of gcc and clang but the result is same.

View 4 Replies View Related

C++ :: Free Up Memory In Client Server Program

Nov 22, 2013

How to free the memory and address,value

Server:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>

[Code] .....

Client:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>

[Code] .....

View 1 Replies View Related

C++ :: Using Malloc / Free Multiple Times Leaves Less Memory?

Apr 17, 2014

My application calls malloc in multiple subroutines, finally releasing all using free. This is done using my zalloc library (see my other post: [URL] .....

Somehow, when the applications tries to detect the available ammount of memory at the end of the test (allocating, freeing, testing), the freemem function gives me about 4-6MB less memory than at the start of the test? (out of 21MB available on the device at the start).

All memory is allocated and freed using the malloc/free routines within the library, with the exception of the SDL functions, which are registered externally on allocation and release.

View 3 Replies View Related

C :: Free Memory From A Dynamically Allocated Array Of Pointers To Linked Lists

Feb 26, 2013

Having some frustrating issues trying to free memory from a dynamically allocated array of pointers to linked lists. I think the problem is in how I initialize the pointers to NULL. Is there a more elegant way to have the program recognize that the list is empty so it knows to create a head node for the linked list in the function 'add_end_stub_to_array'?

I ran the code through Valgrind and it says that memory is definitely lost from this array.

This is the structure definition.

Code: struct stub_edge {
int loc_id;
int anim_type;
int mkt;
struct stub_edge *next_node;
};

Here is the code snippet from main allocating and deallocating memory to the array.

Code:

struct stub_edge **stub_list = (struct stub_edge **)malloc( sizeof(struct stub_edge *) * 12);
for (i = 0; i < 12; i++)
{
stub_list[i] = (struct stub_edge *)malloc(sizeof(struct stub_edge));
stub_list[i] = NULL;
}
stub_list = add_end_stub_to_array(end_stubs, stub_list);
destroy_end_stub_array(stub_list);

Here the function for adding nodes to the lists by reading through a dynamically allocated 2D array. (The end_stubs array is ordered by month and each linked list represents events occuring within the month).

Code:

struct stub_edge **add_end_stub_to_array(int **end_stubs, struct stub_edge **list)
{
long int i = 0;
int mon = 0;
struct stub_edge *current_node1;
struct stub_edge *new_node1;
int break1 = 0;
while(i < num_edges && break1 == 0 && mon < 12)

[Code]...

Here is the function for freeing memory from the list.

Code:

void destroy_end_stub_array(struct stub_edge **list)
{
if(list != NULL)
{
int mon = 0;
struct stub_edge *current_node1;
struct stub_edge *new_node1;
for(mon = 0; mon < 12; mon++)

[Code]...

View 1 Replies View Related

C :: Way To Free Unsigned Pointer Array In Best Way

Feb 24, 2013

how is the best way to free unsigned pointer array allocated with cmallc?

Code:

uint8_t *buf;
buf = cs_calloc(ca->len + 13);

i do like this , but i know this is not quite right , since i got compile warning passing argument 1 of ‘free’ makes pointer from integer without a cast

Code:

for (i = 0; i < ca->len + 13; i++)
{
free(buf[i]);
buf[i] = NULL;
}

free(buf); do i need to free each element of array like above?

View 6 Replies View Related

C++ :: Declaring Vector Of Pointer As Private Class Member

Mar 6, 2013

Below is a working program. Please note that the vector <int*> pointer is declared as a public member of the class A.

The problem arises when I try to make it a private (instead of public). First, let's look at the File 1 below. It compiles and works fine.

File 1: main.cpp (working fine)

#include <vector>
#include <iostream>
using namespace std;

[Code].....

View 19 Replies View Related

C++ :: Pointer Usage To Base Class Object In Vector Int Main Function

Aug 12, 2013

I know what are pointer's and how to use them but there is one point i am not able to understand. Below is the example code

I understand everything in the below code except 1 thing why i am using pointer to base class object in vector int the main() Function?

Code:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
// base class

[Code] ...

Here is the lines of code i want to understand.

Code:
vector<Employee*> employees;
employees.push_back(&emp1);
employees.push_back(&mgr1);

I know if i will not use the pointer base class function "virtual double grossPay" will be called for both base class object and derived class object and when i will use pointer with reference to the object because base class function is virtual it will look for same function in derived class and if available it will execute it.

View 3 Replies View Related

C/C++ :: Class Square Composed Of Two Points - Destructor Called Twice

Nov 3, 2014

I have a class Square that is composed of two Points, I pass the former to the Square as references (second ctor) and two Points are created.

The problem is, at the end of the program, 4 Points are now being deleted which suggests that somewhere copies were made (regardless of the references) and the m_p1, m_p2 have different addresses than p1 and p2.

#include <iostream>
using namespace std;
class Point {
public:
Point();
Point(double x,double y);
double printCor() const;

[Code] .....

Even though, the objects were passed to the ctor by references, the copy constructor (compiler generated) for Point was called and now we have two points and an object square with distinct Point objects.

View 3 Replies View Related

C++ :: How To Delete A Memory Leak

Apr 9, 2014

is there a way to delete a memory leak ? like when the pointer is no longer pointing to that memory address, and that allocated memory is not accessible, how can i delete that ?

sample:

int *p = new int;
*p = 5;
//if i dont't delete p here, is that a way to deallocate the dynamically allocated variable above ?
p = new int; //since p is no longer pointing to 5, and there's no way of accessing it
*p = 10
delete p; //i know this only deallocates the memory address which holds 10, not the one before

View 6 Replies View Related

C++ :: Dynamic Memory - Create Multiple Instances Without Explicitly Call Delete

Mar 28, 2014

look at this code:

#include <iostream>
using namespace std;
class teste

[Code]....

Each time i call "x = new teste();" the previous object is deleted? Does this code cause any sort of memory leak?

View 1 Replies View Related

C++ :: Vector Delete Not Functioning

Dec 1, 2013

My delete is not working. Why?

Code:
struct MatchName{
MatchName(string& searchName) : s_(searchName) {}
bool operator()(const clsStudent* student) const {
return student->getName() == s_;

[Code] .....

View 4 Replies View Related

C++ :: How To Delete The Objects Within A Vector

Nov 10, 2013

here's the problem. I want to delete the objects within a vector, although I'm not sure whether I should clear the vector afterwards. Is it really necessary?

Code:

for (i = 0; i < allSales.size(); i++)
delete allSales[i];

allSales.clear(); //Is this step necessary?

View 5 Replies View Related

C++ :: Delete Pointers From Vector

Jul 25, 2013

I have something like this:

class A {
};

class B : public A {
};

class C : public A {
};

B*b1;
B*b2;
C*c1;
C*c2;

vector<A*>vec;

int main() {
vec.push_back(b1);
vec.push_back(b2);

[Code] ....

And it don't works at all. all i get (when playing with variations of this stuff until it compiles correctlly) is a memory leak.

For example, let say i have b1 address = 1234

I will effectively free the memory at 1234, but for a strange reason, the memory leak is elsewhere, for example, at 2345, and the memory value at this address is equal to... 1234, the address of the pointer i wanted to delete.

View 7 Replies View Related

C++ :: Dynamic Memory (array Of Char) - Delete All White Spaces In Text File

Feb 8, 2014

General Purpose: Delete all "white spaces" in text file, until the read-in char is _not_ a whitespace (mark as start of text file).

Problem: Cannot seem to shift char's over properly. (I think my problem is the inner loop - however other code may lead to this problem - I do not know)

Code:

#include <fstream>
#include <iostream>
using namespace std;

bool trimWhiteSpace(fstream &file, char * charMemoryBlock) {
if (!file.is_open()) {

[Code] ....

View 3 Replies View Related

C++ :: Cast Base Class Pointer To Derived Class Pointer

Feb 6, 2015

I create an instance of a base class (not derived class) and assign it to base class pointer. Then, I convert it to a pointer to a derived class and call methods on it.

why does it work, if there is a virtual table?

when will it fail?

// TestCastWin.cpp : Defines the entry point for the console application.//

#include "stdafx.h"
#include <iostream>
class B
{
public:
B(double x, double y) : x_(x), y_(y) {}
double x() const { return x_; }

[Code] ....

View 14 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved