C++ :: Deleting Vector Of Pointers?

Oct 14, 2014

I made a vector of pointers and the problem is that I have trouble deleting the pointers in the vector. I used to simply do vector.clear() or vector.erase() however it does not clear the actual memory. And I tried something like this:

std::vector<Foo*> Vector;
std::vector<Foo*>::iterator i;
for (i = Vector.begin(); i < Vector.end(); i++)
delete *i;

View 5 Replies


ADVERTISEMENT

C++ ::  Deleting Vector Of Pointers?

Nov 23, 2013

Currently I am implementing the A* algorithm in C++. I have chosen to use a hybrid of a '2D vector grid' and two 1D pointer vectors to specific places in the '2D vector grid'. I have chosen to do it this way, so I can access the nodes using coordinates and also for iterating over the appropriate nodes(rather than the whole '2D vector grid').

In the below examples I have not included the full code because I deemed it irrelevant to the question.

vector <int> CInGame::AStarAlgorithm(vector<vector<bool>>& resistanceMap, int startX, int startY, int targetX, int targetY, int cutOff) {
vector <int> returnVec;
vector <vector<CNode>> twoDimNodes;
vector <CNode*> openSet;
vector <CNode*> closedSet;

[code].....

The error is:

_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

do I need to free the pointers or is it done automatically? If not then how would I do this?

View 3 Replies View Related

C++ :: SDL Deleting From Vector

Apr 13, 2013

Simple rect collision detection

bool check_collision(SDL_Rect *box1, SDL_Rect *box2){
if(box1->x+box1->w<=box2->x)return false;
if(box1->x>=box2->x+box2->w)return false;
if(box1->y+box1->h<=box2->y)return false;
if(box1->y>=box2->y+box2->h)return false;

return true;

[Code] ...

The order of the game is of course standard.

1.handle input
2. handle logic
3. render
4. update

It's all pretty simple and straight forward, but oftentimes (not always) when the weapon hits the asteroid, the program crashes. If for example I turn off the delete main_weapon[i] and main_weapon.erase(main_weapon.begin()+i); the program works perfectly, but of course the bullets go right though the asteroid object, which is not what I want. It's only the weapon deletion which is causing no end of trouble.

View 7 Replies View Related

C++ :: Repeat Found - Deleting In A Vector

Oct 12, 2013

I created a very basic program which contains a vector (my vector) that holds 0, 1 and 1. This program compares each element in the vector. If the element after the one currently being compared is equal to it, it outputs "repeat found!" Now this all works perfectly, but my next step was to erase the repeated one. Everything was working up until the delete step. I get a weird error that says "vector iterator not dereferencable" .

// vector::begin/end
#include <iostream>
#include <vector>
using namespace std;
int main () {
vector<int> myvector;

[Code] ....

View 6 Replies View Related

C++ :: Deleting A String Object In Vector?

Sep 21, 2013

Here's my code so far:

case DELETE_TITLE:
std::cout << "Game to remove from list: ";
std::cin.ignore();
getline(std::cin, gameTitle);
for (iter = gameList.begin(); iter != gameList.end(); ++iter) {

[code]....

It deletes the text from the string but not the index it self.

It deletes the text but when I print the list of game titles it has it there blank. I want it to completely remove the object from the vector from where it was deleted

View 1 Replies View Related

C++ :: Dynamically Deleting Items Out Of Vector

Aug 31, 2014

I have the following code in which I wish to dynamically delete items out of a vector based on the condition of the item in the vector.

The below code works but throws the error: "vector iterator not incrementable" at certain times.

It seems like this happens when the last item in the vector get's deleted. How do I solve this problem?

for (std::vector<PhysicsObject*>::iterator it = CurrentBoxes.begin(); it != CurrentBoxes.end();/*it++*/) {
(*it)->CalculatePhysicsOrientation();
(*it)->DrawMe();
glm::vec3* CurrentBoxPosition = (*it)->GetCurrentPosition();
if (CurrentBoxPosition->y < -750.0f) {
it = CurrentBoxes.erase(it);
}
++it;
}

View 9 Replies View Related

C++ :: Deleting Vector With Char - Segment Fault

Apr 19, 2012

The below code is giving me segment fault during delete [] (*iter) sometimes. Is it not the proper way to delete the vector with char *

Code:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<char*> nameList;
for (int i=0;i<1000;++i)

[Code] ....

View 7 Replies View Related

C++ :: Smart Pointers In A Vector

Aug 21, 2014

I am new to smart pointers and have a question.If you push a smart pointer onto a vector and then some where later pop it back off it will delete the memory right?

View 1 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++ :: Iterating Through A Vector Of Pointers

Sep 4, 2014

If I have a Vector of pointers, how can I iterate through that vector, to access the name function of each object in the vector? There seems to be a problem with my implementation, the output returns only random symbols.

Implementation of the name() function in Drug.cpp:

//name() is a function returning the name in the parent class
string Drug::name() {
string out = "Drug: ";
out += mName + " [ ";
//The problem is with this loop
for (int k = 0; k < mComponents.size(); k++)

[Code] .....

View 10 Replies View Related

C++ :: Calling Functions - Vector Of Pointers

Mar 27, 2013

I have a vector of pointers inside a seperate Exam class.

vector <Question* > question_list

The Question class is my base class in which I have derived sub classes for the different types of questions (MultipleChoice, LongAnswer, etc.). I am using my vector to hold the different types of questions.

in each of those classes I have virtual "write" functions in both the base and the derived classes, that write to a file differing for each type of question.

My problem now is calling the write function from a Exam function. I've tried several methods, such as:

for (size_t i = 0; i < question_list.size(); i++) {
question_list[i].write(testfile.c_str());
}

but it comes with two errors: "error C2228:left of '.write' must have class/struct/union" along with "IntelliSense: expression must have class type"

I have made a write function for the exam class as well but am not sure what it should include since the Exam class is not a derived class of the Question class.

View 15 Replies View Related

C++ :: Vector Of Pointers - Abstract Class

May 13, 2014

I need to create a vector of pointers and hold the book objects in it. then i have a virtual function in my books which is a pure virtual in LibraryItems. When i try to add the books object in my code, i understand that since the scope runs out there is no object that is added. so when i run print it gives me an error.

#include<iostream>
#include "books.h"
#include "library.h"
#include <vector>
using namespace std;

int main(int argc, char * argv[]) {
vector<LibraryItems* >libraryInfo;

[Code] .....

View 4 Replies View Related

C++ :: Sort Vector Of Pointers To Objects

Nov 5, 2014

Im creating a program for a race. The Race class has a vector of results and each element of that vector is a pointer to a result. The Result class has a Time and a pointer to a Participant. So in each race there are various results and it is a result for each participant.The Time is a class that has hours, minutes and seconds. How can I sort the vector of results from the result of the participant with the fastest time to the result of the participant with the slowest time?My code is like this:

//.h file:
class Time {
unsigned int hours;
unsigned int minutes;
unsigned int seconds;

[code]....

What am I missing to get my code to work?

View 9 Replies View Related

C/C++ :: Sort Vector Of Pointers To Objects?

Nov 5, 2014

Im creating a program for a race. The Race class has a vector of results and each element of that vector is a pointer to a result. The Result class has a Time and a pointer to a Participant. So in each race there are various results and it is a result for each participant. The Time is a class that has hours, minutes and seconds. How can I sort the vector of results from the result of the participant with the fastest time to the result of the participant with the slowest time?

Im getting some errors in my code. I put the error as comments in the code. Each error is after the line where it occurs. My code is like this:

//.h file:
class Time
{
unsigned int hours;

[Code]....

View 8 Replies View Related

C/C++ :: Creating A Temporary Vector Of Pointers?

Sep 10, 2014

Is it possible to create a temporary

std::list of pointers

I would like to pass a temporary

std::list

to the constructor of a class to initialize its own one.

For example, using a

std::vector
:
#include <iostream>
#include <vector>
void func(const std::vector<int*>& myVec) {
for(int i=0; i<myVec.size(); ++i){

[code]....

Can we do this? What are other possible problems in addition the ones I have just mentioned above?

View 14 Replies View Related

C++ :: Looping Through A Vector Of Object Pointers

Sep 4, 2014

Lets say for example I have the following vector:

vector<Component*> mComponents;

and a function print() inside of class Component that prints the objects name.

How do I loop/iterate through the vector to access the print function in each object.

View 1 Replies View Related

C++ :: Accessing Pointed-to Value In A Struct Vector Of Pointers?

Apr 30, 2013

I have a vector (structures) in a struct (instances). I make a declaration of this struct called instance. The vector is a 3-layer vector of pointers, like so:

vector < vector < vector<scene::IAnimatedMeshSceneNode*> > > structures; (The type is from Irrlicht 3D). I have 3 nested "for" loops which looks similar to the following:

for (int a = 0; a < instance.structures.size(); a++) { /*note:vector size previously set*/
for (int b = 0; b < instance.structures[a].size(); b++){
for (int c = 0; c < instance.structures[a][b].size(); c++) {

if (1) { //checking value of variable not included in snippet

(instance.structures)[a][b][c] = smgr->addAnimatedMeshSceneNode(fl);
(instance.structures)[a][b][c]->setPosition(renderPos);
}
}
}
}

The problem is in these two lines, I think:

(instance.structures)[a][b][c] = smgr->addAnimatedMeshSceneNode(fl);
(instance.structures)[a][b][c]->setPosition(renderPos);

These are currently referencing the pointers, it seems. The program compiles but crashes at this point. I need them to reference the values of the pointers. Problem is, I don't know where to put the dereference operator (*). Where should it go?

View 4 Replies View Related

C++ :: Hold Vector Of Pointers In Same Class As Instances Of Those Objects?

Feb 10, 2015

Using SFML, I had a Board class which held multiple vectors of all of my object types in the game, and then it also held a vector of pointers to the memory addresses of these object instances, like this

class Board{
//...
std::vector<AbstractObject*> GetAllLevelObjects(){ return allLevelObjects; }
//so these are used to hold my object instances for each level

[Code]....

When looping through this vector and drawing the sprites of the objects, I get the runtime error 0xC0000005: Access violation reading location 0x00277000. I solved this error by storing the vector of pointers in the class that holds my Board instance, but I'm wondering why only this solution worked? Why couldn't I just have my vector of pointers in the same class that the instances of those objects were in?

View 2 Replies View Related

C++ :: Vector Of Void Pointers Which Point To Array Of Characters

Jan 21, 2014

This code work perfectly, as follows.

Code #A:

#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
typedef std::vector <void *> gr_vector_void_star;
gr_vector_void_star output_items;

[Code] .....

Output of above code #A:

char * sentence = "Angel";
for (int i=0; i < 5; i++)
{ out[i] = sentence[i]; } // error: invalid conversion from 'char' to 'char*' [-fpermissive]

It fails to compile with error message "invalid conversion from 'char' to 'char*'".

View 19 Replies View Related

C++ :: Vector Of Pointers - Push Back Objects / Constant Parameters

Apr 11, 2014

I have a class called Question:

#include <string>
#include <vector>
using namespace std;
class Question {
string title;
vector<Thing*> posAns;
vector<Thing*> negAns;

[Code] ....

error: no instance of overloaded function 'std::vector::push_back()' matches the arguments list
argument types are (const Thing *)
object type is: std:: vector<Thing *, std::allocator<Thing *>>

So it cannot be constant, what if I just leave it non-constant? Will it be safe?

View 2 Replies View Related

C :: Create Array Of Pointers To Pointers Which Will Point To Array Of Pointers

Feb 28, 2014

I'm trying to create an array of pointers to pointers which will point to array of pointers (to strings) I tried

Code:

int i;
char *string[]={
"my name is dave",
"we like to dance together",
"sunny day",
"hello",

[code]...

the app keeps crashing , I don't know how to make the array-elements to point to another array-elements..

View 4 Replies View Related

C++ :: Comparing Char Pointers To Integer Pointers

May 21, 2013

I am a little confused while comparing char pointers to integer pointers. Here is the problem:

Consider the following statement;
char *ptr = "Hello";
char cArr[] = "Hello";

When I do cout << ptr; it prints Hello, same is the case with the statement
cout << cArr;

As ptr and cArr are pointers, they should print addresses rather than contents, but if I have an interger array i.e.
int iArr[] = {1, 2, 3};

If I cout << iArr; it displays the expected result(i.e. prints address) but pointers to character array while outputting doesn't show the address but shows the contents, Why??

View 2 Replies View Related

C++ :: Deleting Node In BST

Nov 11, 2014

learning deleting BST node through recursion. In my class my tutor wrote something like this for BST delete node struc:

struct node{
//data
node* root, *left, *right;
}

why not

struct node{
//data
node* *left, *right;
}
node * root;

whats the difference?

View 5 Replies View Related

C# :: Deleting LIST From CSV?

Mar 7, 2014

Basically, I have a LIST that is saved in a CSV file, and I need to remove a particular record after using the search feature (which populates the fields from the CSV file... i.e. ID, Name, etc...).

I am able to get the details that are in the text fields by using a search feature. This is then put in:

logic.remove(tempIndividual)

In class myLogic.cs, then I have this method:

this.individual.Remove(tempIndividual)

Upon debugging, I can see that tempIndividual is populated with the correct data. However, I need to remove this data from the CSV file. Am I on the right track, or totally off? Maybe it is not complete, cause I can't get to the part where it actually removes the data from the CSV, or at least I'm not sure if .Remove is able to do it on its own.

View 9 Replies View Related

C :: Self Deleting Of EXE File By Program Itself

Sep 10, 2014

How to do self deleting of exe file when it is run'd.

View 2 Replies View Related

C++ :: Adding And Deleting From List

Dec 11, 2014

I am writing a program to list the 8 planets, then you select which planet you want. it gives you the mass of the planet, the radius, then you use the mass and radius of the planet to find the surface area (sphere formula) and the density of the planet. Along with those options, you have to add and delete a planet from the list and then sort them alphabetically. All i am having trouble with is the adding and deleting part. The code the adding and deleting from the list would go in cases 9 and 10.

heres my code as of this point.

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
void print(const std::string& item) {

[Code] ....

View 5 Replies View Related







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