C++ - why does this cause a segfault?

Nyap

HTML Noob
OP
Banned
Joined
Jan 13, 2016
Messages
971
Trophies
0
Age
55
Location
That Chaos Site
XP
483
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,082
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
771
Trophies
1
XP
2,193
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
1
Age
44
XP
675
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
55
Location
That Chaos Site
XP
483
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
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • AncientBoi @ AncientBoi:
    ooowwww a new way for me to beat NFS 510 :D @SylverReZ
    +1
  • SylverReZ @ SylverReZ:
    @AncientBoi, Yeah, believe you can do PSP games as well. But a Pi5 is much powerful in comparison.
    +2
  • Psionic Roshambo @ Psionic Roshambo:
    Not sure about other models of Pi4 but the Pi 4 B with 8GBs OCed to 2Ghz handles PSP really great except like 1 game I found and it is playable it just looks bad lol Motor Storm Arctic something or other.
  • Psionic Roshambo @ Psionic Roshambo:
    Other games I can have turned up to like 2X and all kinds of enhancements, Motorstorm hmmm nope 1X and no enhancements lol
  • Veho @ Veho:
    Waiting for Anbernic's rg[whatever]SP price announcement, gimme.
    +1
  • Psionic Roshambo @ Psionic Roshambo:
    I will admit that one does seem more interesting than the usual Ambernic ones, and I already liked those.
  • Veho @ Veho:
    I dread the price point.
    +1
  • Veho @ Veho:
    This looks like one of their premium models, so... $150 :glare:
    +1
  • Psionic Roshambo @ Psionic Roshambo:
    To me that seems reasonable.
  • Psionic Roshambo @ Psionic Roshambo:
    I mean since basically all the games are errmmm free lol
  • Veho @ Veho:
    I mean yeah sure but the specs are the same as a $50 model, it's just those pesky "quality of life" things driving up the price, like an actually working speaker, or buttons that don't melt, and stuff like that.
    +1
  • Psionic Roshambo @ Psionic Roshambo:
    I think all in my Pi 4 was well north of 200 bucks 150ish for the Pi 4 the case the fancy cooler, then like 70 for the 500GB MicroSD then like 70 for the Xbox controller. But honestly it's a nice set up I really enjoy and to me was worth every penny. (even bought more controllers for 2 or 4 player games.) hmmm have never played any 2 player games yet :(
  • Veho @ Veho:
    Yeah that's what I hate about the RPi, it's supposedly $30 or something but it takes an additional $200 of accessories to actually turn it into a working something.
  • Psionic Roshambo @ Psionic Roshambo:
    yes that's the expensive part lol
  • Veho @ Veho:
    I mean sure it's flexible and stuff but so is uremum but it's fiddly.
  • Psionic Roshambo @ Psionic Roshambo:
    Yeah a lot of it I consider a hobby, using Batocera I am constantly adjusting the collection adding and removing stuff, scraping the artwork. Haven't even started on some music for the theme... Also way down the road I am considering attempting to do a WiiFlow knock off lol
  • Veho @ Veho:
    I want everything served on a plate plz ktnx, "work" is too much work for me.
  • Veho @ Veho:
    Hmm, with that in mind, maybe a complete out-the-box solution with all the games collected, pacthed and optimized for me would be worth $150 :unsure:
  • Psionic Roshambo @ Psionic Roshambo:
    Yeah it's all choice and that's a good thing :)
  • Bunjolio @ Bunjolio:
    animal crossing new leaf 11pm music
  • Bunjolio @ Bunjolio:
    avatars-kKKZnC8XiW7HEUw0-KdJMsw-t1080x1080.jpg
    wokey d pronouns
  • SylverReZ @ SylverReZ:
    What its like to do online shopping in 1998: https://www.youtube.com/watch?v=vwag5XE8oJo
    SylverReZ @ SylverReZ: What its like to do online shopping in 1998: https://www.youtube.com/watch?v=vwag5XE8oJo