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
Sep 28, 2013
printf is a part of code segment but found in data segment.....why is it so ?
View 7 Replies
View Related
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
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
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
View Related
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
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
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
Jun 4, 2013
i want to enter a URL in as a string, and extract just the URL part.
eg: Code: input: URL....
output: URL....
Input: URL....
output: URL....
part of our header file which we cannot edit (the url is always less than 140 characters):
Code:
char url[140];
function i am having problems with
Code: void init_url(char new_url[])
{
/*
This function sets the variable url to new_url and url_size to the number of characters in the url
It also initializes url_error_flag
}
[code]....
whenever i try a URL like the second one above with no '?' i'm getting a segmentation fault error which is becase i'm trying to access an invalid index. i'm not sure what an array is initialized to when i do not explicitly assign it anything, for example
Code:
char values[140] = {0};
i've read it depends on whether it is a global vs local array or something? i know this could all be fixed with simply assigning the array values to 0 but i'm not allowed to edit the header file.
View 11 Replies
View Related
Oct 29, 2014
Code:
cout<<"Enter Filename for input e.g(inp1.txt .... inp10.txt):"<<flush;
cin>>filename;
ifstream inpfile;
inpfile.open(filename,ios::in);
if(inpfile.is_open())
[Code] .....
View 8 Replies
View Related
Jan 25, 2013
This function should replace all instances of a character in a given character array, while returning the amount of characters changed, but I keep getting a segmentation fault at the highlighted area.
I'm only supposed to use pointers so arrays are out of the question, and I don't think we are allowed to use the string.h library as well. How I could avoid something the segmentation fault or ways to fix it?
Code:
int replaceChars(char replace, char find, char *input) { int i, j;
//Finds length
for(i = 0; *(input + (i + 1)) != '