C++ - why does this cause a segfault?

Nyap

HTML Noob
OP
Banned
Joined
Jan 13, 2016
Messages
971
Trophies
0
Age
53
Location
That Chaos Site
XP
482
Country
Here's the code:
/*C++toSB v0.0.1 by Nyap
*C++ to SmileBASIC compiler
*/

#define VERSTRING "0.0.1"
#include "declerations.h"
#include <cstdlib>
#include <fstream>

int main(int argc, char **argv)
{
argv[1] = "lol.txt";

//Print intro
cout << "CPPtoSB " << VERSTRING << " by Nyap\n"
<< "C++ to SmileBASIC compiler\n"
<< "This build was compiled on " << __DATE__ << " at " << __TIME__ << '\n'
<< "#Checking compilation conditions...\n";
//Check if filename argument was passed
if (!argv[1])
{
cerr << "#No filename argument detected! Aborting!\n";
exit(-1);
}
//Make input stream for input file
ifstream input(argv[1]);
//Make output stream for output file
ofstream output("output.txt");
//Check if the input filename even exists
if (!input.is_open())
{
cerr << "#No file of name " << argv[1] << " found.\n";
exit(-1);
}
//Print message to say everything went ok
cout << "#Everything OK!\n";
//Call the compiler
compile(input, output);
//End the program
return 0;
}
#include "declerations.h"
#include "buffer.h"

void compile(ifstream& input, ofstream& output)
{
cout << "#Compiler called\n";
buffer line{"Hello World!"};
buffer token;
line.extractToken(token);
cout << token;
}
#ifndef DECLERATIONS_H
#define DECLERATIONS_H

#include <fstream>
#include <iostream>
#include <sstream>

using std::cout;
using std::cerr;
using std::cin;
using std::ios;
using std::ifstream;
using std::ofstream;
using std::string;
using std::getline;
using std::stringstream;

void compile(ifstream&, ofstream&);

#endif
class buffer
{
protected:
int bufferSize{};
char* Buffer;
public:
buffer() {}
buffer(char *stringvalue) : Buffer(stringvalue)
{
for (int i{}; Buffer!='\0'; ++i)
++bufferSize;
}
void extractToken(char* target, int targetsize=-1)
{
if (targetsize<0)
cout << "#extractToken() - Target size unkown!";
if (bufferSize==0)
{
cout << "#extractToken() - Nothing in buffer to extract!";
return;
}

int tokenSize;
for (int i{}; Buffer!='\0' && Buffer!=' '; ++i)
++tokenSize;

if ((targetsize>-1 && targetsize>tokenSize) || targetsize<0)
{
for (int i{}; i<=tokenSize; ++i)
target=Buffer;
}
else
{
cout << "#extractToken() - Target too small";
}
}
void extractToken(buffer& target)
{
if (bufferSize==0)
{
cout << "#extractToken() - Nothing in buffer to extract!";
return;
}

target.bufferSize=0;
for (int i{}; Buffer!='\0' && Buffer!=' '; ++i)
{
target.Buffer=Buffer;
++target.bufferSize;
}
}
int size() {return bufferSize;}
char* get() {return Buffer;}
char get(int element) {return Buffer[element];}

buffer operator=(char*);
friend std::ostream&::operator<<(std::ostream&, buffer&);
};

buffer buffer::operator=(char* stringvalue)
{
Buffer=stringvalue;
bufferSize=0;
for (int i{}; Buffer!='\0'; ++i)
++bufferSize;
return *this;
}

std::ostream& operator<<(std::ostream& out, buffer& output)
{
out << output.Buffer;
return out;
}
There's a segmentation fault in the extractToken(buffer&) member function (buffer.h) and for the love of god I can't figure out why
Plz help
 

mbcrazed

Well-Known Member
Member
Joined
Nov 10, 2012
Messages
695
Trophies
1
Location
GBATemp
XP
2,027
Country
United States
Hm... Well, I really don't know if I'm right because I stopped programming a while ago, but giving a quick look at your code there could be an out of bounds access of one of your arrays. I'm not too sure though! You're going to have to ask someone who knows much more than me! Sorry I can't help that much! :/
 

PewnyPL

Well-Known Member
Member
Joined
Feb 2, 2014
Messages
767
Trophies
0
XP
1,924
Country
Poland
I think the issue is the very first line in main. If you launch the exe without ANY arguments, then argv is void, therefore, accessing/writing to an uninitialised array will cause a segfault. If you want to do such a workaround as you did here, try to use new to initialise a 2 element argv and then assign "lol.txt" to it's second element.
 

sandytf

Well-Known Member
Member
Joined
May 5, 2013
Messages
133
Trophies
0
Age
43
XP
653
Country
United States
Your problem is related to the first line in the main function. The two parameters argc and argv should be treated as read only values. You should never edit them. The fix for your program is rather simple. Create a new variable, probably a std::string, and set that to the desired file name. If you want the user to optionally set a filename from the commandline, then use a simple if statement. If argc equals 1, set the string to the value in argv [1], otherwise set it to your default value.
 

Nyap

HTML Noob
OP
Banned
Joined
Jan 13, 2016
Messages
971
Trophies
0
Age
53
Location
That Chaos Site
XP
482
Country
Your problem is related to the first line in the main function. The two parameters argc and argv should be treated as read only values. You should never edit them. The fix for your program is rather simple. Create a new variable, probably a std::string, and set that to the desired file name. If you want the user to optionally set a filename from the commandline, then use a simple if statement. If argc equals 1, set the string to the value in argv [1], otherwise set it to your default value.
I edited one of the arguments for debugging purposes
That's not the problem, the problem is within the extractToken member function from the buffer class. The function would attempt to copy a string to the "Buffer" pointer, instead I should have made a dynamically allocated array, copy the string there and set the pointer to that

I think the issue is the very first line in main. If you launch the exe without ANY arguments, then argv is void, therefore, accessing/writing to an uninitialised array will cause a segfault. If you want to do such a workaround as you did here, try to use new to initialise a 2 element argv and then assign "lol.txt" to it's second element.
The program checks to make sure the user input a filename
 

You may also like...

General chit-chat
Help Users
  • No one is chatting at the moment.
    M4x1mumReZ @ M4x1mumReZ: @BentlyMods, Priiloader and BootMii as boot2 and you're all set