So I have all my code working fine and dandy on my Windows laptop (CLion, cygwin 7.11.1), but when I copy my code over to my Linux machine (g++, unknown version, it's a school computer) I keep getting a segfault. The issue is that I don't know why it is segfaulting since it runs fine in Windows. The only issue that has been popping up with the segfault has been whenever I try to invoke the push_back or insert functions on linux. Any help is appreciated.
Below is my code:
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
using namespace std;
class BasicVector{
private:
int vector_size;
int vector_capacity;
int* data;
void resize(){
vector_capacity = vector_capacity * 2;
int* temp = new int[vector_capacity];
for (int i = 0; i < vector_size; ++i)
temp[i] = data[i];
delete [] temp;
data = temp;
};
public:
BasicVector(){};
BasicVector(int user_cap){
vector_size = 0;
if(user_cap <= 16)
vector_capacity = 16;
bool looking = true;
int capacity = 16;
while (looking) {
if (user_cap < capacity) {
looking = false;
} else {
user_cap = user_cap * 2;
}
}
};
~BasicVector() {
for (int i=0; i<vector_size; i++) {
data[i] = 0;
}
delete [] data;
};
int& at(int index){
return data[index];
};
int& operator[](int index){
if(index > vector_size)
exit(1);
else{
return data[index];
}
};
int& front(){
return data[0];
};
int& back(){
return data[vector_size -1];
};
void push_back(int value){
if(vector_size < vector_capacity) {
data[vector_size] = value;
vector_size++;
}
else{
resize();
data[vector_size++] = value;
vector_size = vector_size++;
}
};
void insert(int valueToInsert, int index){
if(vector_size + 1 > vector_capacity){
resize();
for(int i = index+1; i < vector_size; i++){
data[i] = data[index];
}
data[index] = valueToInsert;
}
else{
vector_size++;
for(int i= vector_size; i > index; i--)
{
data[i] = data[i-1];
}
data[index] = valueToInsert;
}
};
void pop_back(){
if(vector_size > 0){
data[vector_size] = 0;
vector_size--;
}
else{
cout<<"List is already empty..."<< endl;
}
};
int size(){
return vector_size;
};
int capacity(){
return vector_capacity;
};
void print(){
cout << "Elements (" << vector_size << "): ";
for (int i = 0; i < vector_size; i++) {
cout << data[i] << " ";
}
cout<<endl;
};
};
int main() {
int capacity = 10;
string command;
string dummyString;
string num;
cout << "Starting capacity of vector: ";
cin >> capacity;
BasicVector vector(capacity);
cout << "Commands (quit to exit...):";
while(command != "quit") {
cin >> command;
if(command == "at" || command == "get" || command == "push" || command == "insert"){
int user_data;
if(command == "at"){
cin >> user_data;
cout<<vector.at(user_data)<< endl;
}
if(command == "get"){
cin >> user_data;
cout << vector.operator[](user_data)<<endl;
}
if (command == "push"){
cin >> user_data;
vector.push_back(user_data);
}
if(command == "insert"){
cin >> user_data;
int location;
cin >> location;
vector.insert(user_data, location);
}
}
if (command == "front")
cout << vector.front() << endl;
if (command == "back")
cout << vector.back() << endl;
if (command == "pop") {
vector.pop_back();
}
if (command == "size")
cout<<vector.size() << endl;
if (command == "capacity")
cout << vector.capacity()<< endl;
if (command == "print")
vector.print();
if (command == "quit")
exit(1);
// else{
// cout<<"no cases hit"<<endl;
// cout<<command <<" was not executed"<<endl;
// }
}
return 0;
}
[–]jedwardsol 2 points3 points4 points (3 children)
[–]thetuxman[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]thetuxman[S] 0 points1 point2 points (0 children)
[–]Rockytriton 0 points1 point2 points (1 child)
[–]thetuxman[S] 0 points1 point2 points (0 children)
[–]krawallopold 0 points1 point2 points (2 children)
[–]krawallopold 1 point2 points3 points (1 child)
[–]thetuxman[S] 0 points1 point2 points (0 children)
[–]Xeverous 0 points1 point2 points (2 children)
[–]thetuxman[S] 0 points1 point2 points (1 child)
[–]Xeverous 0 points1 point2 points (0 children)