I working on a program that simulates travel agents booking flights in parallel. It spins up a process for each agent and works against an array of Plane objects held in shared memory.
I'm getting a segmentation fault when I try to push a row of seats back to the plane. The method to parse the input file calls a SetSeats() method on Plane objects. Each Plane contains a vector<map<char, Seat>> (each index of the vector is a row, each key of each map is the letter of a seat on that row). When I call SetSeats() it goes fine through adding seats to the first map, i.e. the first row of seats. It throws the segfault when I try to push the map back to the seats vector.
I saw something online about pushing back custom classes to vectors needing deconstructors, so I added them to Seat.h and Plane.h.
Code for the main program:
#include <iostream>
#include <map>
#include <vector>
#incluce <string>
#include <fstream>
#include "Seat.h"
#include "Plane.h"
void ParseInputFile(ifstream &inFS, int numPlanes, int &numAgents);
int shmid;
int *timer;
int numPlanes, numAgents;
struct sembuf *ops;
Plane *sharedPlanes;
map<string, Plane*> planes;
using namespace std;
int main(int argc, char *argv[])
{
ifstream inFS;
// code to get an input file from command line arguments and get number of planes from it
// set up shared memory segment
long key = XXX; // just a long integer
int nbytes = 1024;
shmid = shmget((key_t)key, nbytes, 0666 | IPC_CREAT);
if (shmid == -1)
{
printf("Error in shared memory region setup.\n");
perror("REASON");
exit(2);
}
// initialize global variables
sharedPlanes = new Plane[numPlanes];
timer = new int;
ops = new sembuf[1];
// attached shared pointers to shared memory segment
sharedPlanes = (Plane*)shmat(shmid, (Plane*)0, 0);
timer = (int*)shmat(shmid, (int*)0, 0);
*timer = 0;
inFS.open(inputFile);
ParseInputFile(inFS, numPlanes, numAgents); // breaks in here
// the rest of main()
}
void ParseInputFile(ifstream &inFS, int numPlanes, int &numAgents)
{
string line = "";
bool foundNumberOfPlanes = false;
bool foundPlanes = false;
bool foundNumberOfAgents = false;
bool lookingForAgent = false;
bool foundAgent = false;
int planeNo = 0;
int agentNo = 0;
int opNo = 0;
map<string, Operation> ops;
vector<Request> agentRequests;
while (getline(inFS, line))
{
if (!CommonMethods::IsWhitespace(line))
{
// code to read first line
if (foundNumberOfPlanes && !foundPlanes)
{
// parse a line from the input file to get details about the plane
Plane *plane = &sharedPlanes[planeNo];
unsigned int rows = xxx; // set based on the plane details
unsigned int seatsPerRow = xxx; set based on the plane details
plane->SetSeats(rows, seatsPerRow); // this is the method where I get the seg fault
// finish defining the plane
continue;
// the rest of the method
}
}
}
}
Code for Plane.h:
#pragma once
#include <iostream>
#include <string>
#include <map>
#include <tuple>
#include <vector>
#include "Seat.h"
#include "Exceptions.h"
#include "ReservationStatus.h"
#include "CommonMethods.h"
using namespace std;
class Plane
{
private:
vector<map<char, Seat>> seats;
unsigned int numberOfRows, numberOfSeatsPerRow;
public:
Plane(unsigned int numberOfRows, unsigned int numberOfSeatsPerRow);
Plane() {}
void SetSeats(unsigned int numberOfRows, unsigned int numberOfSeatsPerRow);
};
void Plane::SetSeats(unsigned int numberOfRows, unsigned int numberOfSeatsPerRow)
{
//cout << "Clearing old seats" << endl;
if (!seats.empty())
{
//cout << "Seats not empty" << endl;
for (int i = 0; i < (int)seats.size(); i++)
{
//cout << "checking row " << i << endl;
if (!seats.at(i).empty())
{
//cout << "Row " << i << " not empty" << endl;
seats.at(i).clear();
}
}
}
cout << "Rows: " << numberOfRows << ", Seats: " << numberOfSeatsPerRow << endl;
this->numberOfRows = numberOfRows;
this->numberOfSeatsPerRow = numberOfSeatsPerRow;
for (unsigned int i = 0; i < this->numberOfRows; i++)
{
map<char, Seat> row;
for (unsigned int j = 0; j < this->numberOfSeatsPerRow; j++)
{
Seat seat;
seat.RowNumber = i + 1;
seat.SeatLetter = j + 'A';
//cout << "Inserting seat " << seat.RowNumber << seat.SeatLetter << endl;
row.insert(pair<char, Seat>(seat.SeatLetter, seat));
}
if (!row.empty())
{
cout << "inserting row " << (i + 1) << endl;
seats.push_back(row);
}
}
}
void Plane::ProcessWaitAny(int t)
{
while (!WaitingList.empty())
{
bool booked = false;
string pass = WaitingList.front();
WaitingList.pop();
for (unsigned int j = 0; j < numberOfRows; j++)
{
if (booked)
break;
for (unsigned int k = 0; k < numberOfSeatsPerRow; k++)
{
Seat *s = &seats.at(j)[k + 'A'];
if (!s->IsBooked)
{
Reserve(s, pass);
booked = true;
string seatNo = to_string(j);
seatNo += (k + 'A');
cout << "Passenger " << pass << " booked into seat " << seatNo << " at time " << t << endl;
break;
}
}
}
if (!booked)
return;
}
}
Code for Seat.h
#pragma once
#include <iostream>
#include <string>
#include <queue>
using namespace std;
struct Seat
{
string Passenger = "";
bool IsBooked = false;
unsigned int RowNumber;
char SeatLetter;
queue<string> WaitingList;
};
I have made a minimum working example of your problem:
#include <vector>
#include <sys/shm.h>
#include <iostream>
class Bar {
public:
Bar() {};
std::vector<int> vec;
};
int main() {
int shmid;
Bar* a = new Bar();
a->vec.push_back(1);
// set up shared memory segment
long key = 0x123455; // just a long integer
int nbytes = 1024;
shmid = shmget((key_t)key, nbytes, 0666 | IPC_CREAT);
if (shmid == -1)
{
printf("Error in shared memory region setup.\n");
perror("REASON");
exit(2);
}
a = (Bar*)shmat(shmid, (Bar*)0, 0);
a->vec.push_back(2);
}
The problem is your wrong usage of shmat. The pointer sharedPlane just points to some unitialized shared memory. You have to make sure that the address provided by key is 'right'. To do this, do the following:
Your other process, call Plane * other_process_sharedPlane = new Plane();. Remove the line sharedPlanes = new Plane[numPlanes]; from your main programm.
In your main process, set key to the value of other_process_sharedPlane
Then you can call shmget and shmadd
Related
I am writing a program to reverse a string and insert random characters in between.
Here is my code:
main.cpp
#include <iostream>
#include <chrono>
#include <thread>
#include "encrypter.cpp"
using namespace std;
int main(int argc, char *argv[])
{
char message[256];
this_thread::sleep_for(chrono::milliseconds(1000));
cout << "Make sure theres no one around you" << endl;
this_thread::sleep_for(chrono::milliseconds(1000));
cout << "Enter secret message ";
cin.get(message, 256);
cout << "message encrypted" << endl;
enc(message);
return 0;
}
encrypter.cpp
#include <iostream>
#include <string>
int getRandom(int a, int b) {
return a + (rand() % static_cast<int>(b - a + 1));
}
using namespace std;
void enc(char message[256]) {
int i = 0;
int len = strlen(message);
int revlen = len - 1;
int wtpselector;
int charselector;
int encsim;
char randchar[6] = "##$%&";
char strreved[256];
char strenc[1024];
while (i < len) {
strreved[revlen] = message[i];
i++;
revlen--;
}
revlen = strlen(strreved);
len = revlen - 1;
i = 0;
encsim = 0;
while (i < revlen) {
wtpselector = getRandom(0, 4);
charselector = getRandom(0, 4);
if (wtpselector == 0) {
strenc[encsim] = strreved[i];
i++;
encsim++;
} else {
strenc[encsim] = randchar[charselector];
encsim++;
}
}
cout << strenc << endl;
}
But the output has many random characters that are not supposed to be there and are not in the program.
Like:
Input: hello world
Output: $%#$#&&d&&#%$%1%row &#$$#%&ol#%##%&1%&#$&#ehe$%%€##8#&%#$#& #%##%&¢&#%#&#$##$#%%##&%##&#&$8%#$###$#$##%&&#&##Q#$$#&¢%#% Q#\#&$##{&&&&$#¢ \ $$$$######&&&%%&&%{ $#¢v#&#&~u####%&%¢
Please help!!!
You have missed two important things:
when you declare an array it's values are not defined (whatever is in memory)
strlen counts to the first occurance of '\0' character
In order for your code to work you must initialize char arrays, which means change those two lines:
char strreved[256];
char strenc[1024];
to:
char strreved[256] = {0};
char strenc[1024] = {0};
Okay so the project is to create a lottery number composed of 10 random positive integers and the user is suppose to guess it until the user guesses the correct number. All of my code looks good but when I run the program and enter in a number it gives me this MSVS Runtime Library error? I dont even know what it means as I am fairly new to programming. Help would be very appreciated!
Main.cpp
#include <iostream>
#include <cmath>
#include <ctime>
#include "Lottery.h"
using namespace std;
int main() {
const int size = 9; //declare variables
int win[size];
int g;
srand(time(NULL));
assign(win, size);
draw(win, size);
g = entry();
if (check(win,size,g) == true) {
cout << "Congradulations! You have won the lottery!" << endl;
}
else {
cout << "Try again!" << endl;
}
printOut(g);
}
Lottery.cpp
#include <iostream>
#include <cmath>
#include "Lottery.h"
using namespace std;
int entry() {
int guess;
cout << "Enter a number from 0 to 99." << endl;
cin >> guess;
return guess;
}
void assign(int w[], int s) {
for (int i = 0; i < s; i++) {
w[s] = -1;
}
}
bool check(int w[], int s, int g) {
for (int i = 0; i < s; i++) {
if (g == w[i]) {
return true;
}
}
return false;
}
void draw(int w[], int s) {
for (int i = 0; i < s; i++) {
int tmp = rand() % 100;
if (check(w, s, tmp)) {
i--;
}
else
w[i] = tmp;
}
}
void printOut(int g) {
cout << "Numbers you have chosen:" << " " << g << endl;
}
Lottery.h
#ifndef LOTTERY_INCLUDED
#define LOTTERY_INCLUDED
void assign(int[], int);
bool check(int[], int, int);
void draw(int[], int);
int entry();
void printOut(int);
#endif //LOTTERY
Debugging tutorials are available elsewhere. But if something bad happens, don't panic and look for instructions.
First, your runtime error:
This has a link "Break and open exception settings" link or a "Break" button. Break which will take you to the end of main if you click it.
The details say we did something bad near win.
Look at this:
void assign(int w[], int s) {
for (int i = 0; i < s; i++) {
w[s] = -1; //<------Oh oops!
}
}
We know the length of the array is s i.e. 9, and are using w[s] where we clearly meant w[i].
The extra details in the error are telling you a possible place to look.
I have been attempting to find a way to sort an array of pointers (pointing to strings) and then display the non-sorted list and the sorted list but no mater what I try the 2nd printed list is always identical to the original non-sorted list. Any help you could offer would be greatly appreciated (and I'm sorry if my code is a mess I'm a new student)
this is my main(lab5.cpp)
#include <cstdlib>
#include <iostream>
#include "student.h"
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
student stu;
stu.list();
system("PAUSE");
return EXIT_SUCCESS;
}
This is my header(student.h)
#include <string>
class student
{
public:
student( );
void setnameage();
int getage(int);
std::string getname(int);
void sort();
void list();
private:
std::string name[50];
std::string nameL[50];
int age[50];
std::string * Pname ;
int * Page;
int amount;
};
This is my object (student.cpp)
#include <iostream>
#include <iomanip>
#include "student.h"
#include <string>
using namespace std;
//constructor
student::student()
{
int i = 0;
amount = 0;
Pname = name;
Page = age;
while (i != 50)
{
age[i] = 0;
name[i] = "A";
i = i +1 ;
}
std::cout << "Enter number of students(max 50) \n" << ">";
std::cin >> amount;
}
//sets the neame and the age
void student::setnameage()
{
int i = 0;
while (i != amount)
{
std::cout << "Enter name " << i+1 <<" (last, first):";
std::cin >> name[i] >> nameL[i];
std::cout << "enter age";
std::cin >> age[i];
i++;
}
}
//get age
int student::getage(int i)
{
return age[i];
}
//get name
std::string student::getname(int i)
{
return name[i];
}
//sorts the aray of pointers
void student::sort()
{
std::string tempL;
int tempN;
i = 0
for (int i = 1; i <= amount-1; i++)
{
for(int j=i+1; j <= amount; j++)
{
if(Pname[i].compare(Pname[j]) > 0)
{
tempN = Page[i];
Page[i] = Page[j];
Page[j] = tempN;
// tempL = Pname[i];
Pname[i].swap(Pname[j]);
//Pname[j] = tempL;
}
}
}
}
//displayes the final results
void student::list()
{
setnameage();
int i = 0;
std::cout << "original list\n-------------";
while(i != amount)
{
std::cout<< "\n" << getname(i) << ">" << getage(i);
i++;
}
sort();
i = 0;
std::cout << "\nAlphabetized list\n-------------";
while(i != amount)
{
std::cout<< "\n" << Pname[i] << ">" << Page[i];
i++;
}
}
First let me say your program has a lot of design problems, but to answer your actual question:
The trouble is you don't have an array of 50 pointers, you just have one pointer to the start of the array. In your sort function you have this line to swap the string pointers:
Pname[i].swap(Pname[j]);
But this doesn't swap the pointers, it swaps the original strings. So instead of ending up with the original array of strings, and a re-ordered array pointing to those strings, you just end up with an array of re-ordered strings.
You should change std::string* pName; to std::string* pName[50];. At the start of your program, initialise the array to point to the strings.
for (int i = 0; i < 50; i++) pName[i] = &name[i];
Then in your sort function you should use std::swap() to swap the pointers themselves:
std::swap(pName[i], pName[j]);
Finally, since pName[i] is now a pointer, whenever you actually want to access the string you have to dereference the pointer. For example,
if(Pname[i].compare(Pname[j]) > 0)
becomes
if(Pname[i]->compare(*Pname[j]) > 0)
The same problem exists with your method of sorting the ages.
A much better design for your program would be to use std::list<std::pair<std::string, int>> to store the names and ages. Then you can use the built in sorting functions to sort the list (and easily make a copy of it if you need to keep the original as well).
It's crashing at the very end of the main() function where it needs to delete the starters objects. The error message that pops up when I run the program says: Debug assertion failed! Expression: _BLOCK_IS_VALID(pHead->nBlockUse). How do i fix it from crashing when deleting the starters objects?
#include <iostream>
#include <fstream>
#include "olympic.h"
using namespace std;
ofstream csis;
int main() {
const int lanes = 4;
Ranker rank(lanes);
csis.open("csis.txt");
// First make a list of names and lane assignments.
Competitor* starters[lanes];
starters[0] = new Competitor("EmmyLou Harris", 1);
starters[1] = new Competitor("Nanci Griffith", 2);
starters[2] = new Competitor("Bonnie Raitt", 3);
starters[3] = new Competitor("Joni Mitchell", 4);
// The race is run; now assign a time to each person.
starters[0]->setTime((float)12.0);
starters[1]->setTime((float)12.8);
starters[2]->setTime((float)11.0);
starters[3]->setTime((float)10.3);
// Put everyone into the ranker.
for (int i = 0; i < lanes; i++)
rank.addList(starters[i]);
// Now print out the list to make sure its right.
cout << "Competitors by lane are:" << endl;
csis << "Competitors by lane are:" << endl;
for (int i = 1; i <= lanes; i++)
rank.getLane(i)->print();
// Finally, show how they finished.
cout << "Rankings by finish are:" << endl;
csis << "Rankings by finish are:" << endl;
for (int i = 1; i <= lanes; i++)
rank.getFinish(i)->print();
for (int i = 0; i < lanes; i++)
delete starters[i];
csis.close();
}
ranker.cpp:
#include "ranker.h"
#include "competitor.h"
#include <stdlib.h>
Ranker::Ranker(int lanes) {
athlete = new Competitor*[lanes];
numAthletes = 0;
maxAthletes = lanes;
}
int Ranker::addList(Competitor* starter) {
if (numAthletes < maxAthletes && starter != NULL) {
athlete[numAthletes] = starter;
numAthletes++;
return numAthletes;
}
else
return 0;
}
Competitor* Ranker::getLane(int lane) {
for (int i = 0; i < numAthletes; i++) {
if (athlete[i]->getLane() == lane) {
return athlete[i];
}
}
return NULL;
}
Competitor* Ranker::getFinish(int position) {
switch(position) {
case 1:
return athlete[3];
break;
case 2:
return athlete[2];
break;
case 3:
return athlete[1];
break;
case 4:
return athlete[0];
break;
}
return NULL;
}
int Ranker::getFilled() {
return numAthletes;
}
Ranker::~Ranker() {
delete [] athlete;
}
competitor.h:
#ifndef _COMPETITOR_H
#define _COMPETITOR_H
class Competitor {
private:
char* name;
int lane;
double time;
public:
Competitor(char* inputName, int inputLane);
Competitor();
void setTime(double inputTime);
char* getName();
int Competitor::getLane();
double getTime();
void print();
~Competitor();
};
#endif
competitor.cpp:
#include "competitor.h"
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
Competitor::Competitor(char* inputName, int inputLane) {
name = inputName;
lane = inputLane;
}
Competitor::Competitor() {
name = 0;
lane = 0;
time = 0;
}
void Competitor::setTime(double inputTime) {
time = inputTime;
}
char* Competitor::getName() {
return name;
}
int Competitor::getLane() {
return lane;
}
double Competitor::getTime() {
return time;
}
void Competitor::print() {
cout << setw(20) << name << setw(20) << lane << setw(20) << setprecision(4) << time << endl;
}
Competitor::~Competitor() {
delete [] name;
}
Call stack:
before crash: http://i.imgur.com/d4sKbKV.png
after crash: http://i.imgur.com/C5cXth9.png
After you've added Competitor class, it seems the problem is that you delete its name in Competitor's destructor. But you assign it from string literal which can't really be deleted. I'm sure the stack trace leading to assertion will prove that.
One way of solving the problem would be using std::string to store the name.
Problem is when deleting the char* value on destructor, which is assigned with const char instead new char. So i have slightly changed the constructor to copy the const char to new char.
Competitor::Competitor(char* inputName, int charlen, int inputLane)
{
name = new char[charlen + 1];
memcpy(name , inputName, charlen );
name [charlen] = '\0';
lane = inputLane;
}
I'm a C++ noob, currently trying to create a text based maze game for degree. I'm replacing my currently working array with a vector (for dynamic size) and reading a txt file of chars to it.
PROBLEM: I get no IDE errors, but get RUNTIME ERROR: Debug assertion Failed! Expression: vector subscript out of range!
I have used the pushback instead of RoomFile=data, but I don't know if I need to amend other functions. I read the array/vector data in another class (player) and will include code if needed.
I have searched everywhere to find solution, but can't find any projects using a similar vector structure. What am I doing wrong?
Room.h
#pragma once
#include <vector>
#include "stdafx.h"
class Room
{
private: //data acceessed by functions
char floor;
char wall;
char door;
int width;
int height;
public:
std::vector <char> data;
Room* North;
Room* East;
Room* South;
Room* West;
Room* Next;
int NorthX;
int NorthY;
int EastX;
int EastY;
int SouthX;
int SouthY;
int WestX;
int WestY;
char RoomFile;
//where functions go
Room(void);
void CreateRoom(int RoomNo);
void PrintRoom();
~Room(void);
};
Room.cpp
#include <iostream>
#include <fstream>
#include <streambuf>
#include <string>
#include <sstream>
#include "Room.h"
#include <cstdlib>
using namespace std;
Room::Room(void)
{
floor = ' ';
wall = '#';
door = 'X';
width = 11;
height = 11;
North=0;
East=0;
South=0;
West=0;
Next=0;
NorthX=0;
NorthY=0;
EastX = 0;
EastY = 0;
SouthX= 0;
SouthY=0;
WestX=0;
WestY=0;
}
void Room::CreateRoom(int RoomNo)
{
std::vector<char> data;
char RoomFile[255];
ifstream infile;
stringstream ss;
//LOAD CURRENT ROOM FROM FILE
ss << RoomNo;
string str = ss.str();
string fname = ("Room");
fname.append(str);
fname.append(".txt");
infile.open(fname);
infile.clear();
infile.seekg(0);
if(infile.is_open())
{
while(!infile.eof()) // To get you all the lines.
{
int i;
for(int row = 0; row < width; row++)
{
infile.getline(RoomFile, 256);
i = 0;
for(int col = 0; col < height; col++)
{
data.push_back(RoomFile[i]);
i++;
}
}
}
}
else
{
cout << "ERROR: infile not open" << endl;
}
infile.close();
//ASSIGN DOORS TO ROOMS IF NEEDED
// treating the array as being one-dimensional.
for(int row = 0; row < width; row++)
{
for(int col = 0; col < height; col++)
{
if(North!=NULL)
{
if(data[row*height+col]=='X')
{
//data[row*height+col] = door;
NorthX=row;
NorthY=col;
}
}
if(East!=NULL)
{
if(data[row*height+col]=='X')
{
//data[row*height+col] = door;
EastX = row;
EastY = col;
}
}
if(South!=NULL)
{
if(data[row*height+col]=='X')
{
//data[row*height+col] = door;
SouthX = row;
SouthY = col;
}
}
if(West!=NULL)
{
if(data[row*height+col]=='X')
{
//data[row*height+col] = door;
WestX = row;
WestY = col;
}
}
}
}
}
void Room::PrintRoom()
{
for(int row = 0; row < width; row++)
{
for(int col = 0; col < height; col++)
{
cout << data[col * height + row];
}
cout << "\n";
}
cout << endl;
}
Room::~Room(void)
{
}
Stack Trace
// throw -- terminate on thrown exception REPLACEABLE
#define _HAS_EXCEPTIONS 0
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <crtdbg.h>
_STD_BEGIN
#ifdef _DEBUG
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const wchar_t *message, const wchar_t *file, unsigned int line)
{ // report error and die
if(::_CrtDbgReportW(_CRT_ASSERT, file, line, NULL, L"%s", message)==1)
{
::_CrtDbgBreak();
}
}
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const unsigned short *message, const unsigned short *file, unsigned int line)
{ // report error and die
_Debug_message((wchar_t *) message, (wchar_t *) file, line);
}
#endif
_STD_END
I think this is the error: What you are printing in PrintRoom is the std::vector<char> data inside your class, while the std::vector<char> data that you filled up is local to CreateRoom method.
void Room::CreateRoom(int RoomNo)
{
std::vector<char> data; // remove this to solve
In your PrintRoom() function, you have
cout << data[col * height + row];
instead of
cout << data[row * height + col];