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,166
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
  • Psionic Roshambo @ Psionic Roshambo:
    OJ now featured in Mortal Kombat?
  • Psionic Roshambo @ Psionic Roshambo:
    He doesn't have a fatality but the lights go out and the opponents head falls off
  • Psionic Roshambo @ Psionic Roshambo:
    With him claiming he didn't do it.
  • S @ salazarcosplay:
    how is everyone doing
  • S @ salazarcosplay:
    @realtimesave the samsung cable where did you buy it from
  • S @ salazarcosplay:
    from samsung website or local walmart/best buy/target
  • realtimesave @ realtimesave:
    probably 99cent only
  • realtimesave @ realtimesave:
    I have a new samsung one here
  • S @ salazarcosplay:
    amazon sells fake apple and Samsung cables because they are sourced thru third party disturbers
  • S @ salazarcosplay:
    @realtimesave the cable i have been after is the usb c cable
  • S @ salazarcosplay:
    the official one for the pro cotnroller
  • S @ salazarcosplay:
    cables get lost when you have little kids in the home, little brothers ect
  • S @ salazarcosplay:
    i know i can use any usb c cable but the ocd ect of wantign to have everything with th ebrand
  • realtimesave @ realtimesave:
    I flipped over the cable and it worked so maybe it's the cable.. I could just plug in the one the phone came with
  • Psionic Roshambo @ Psionic Roshambo:
    USB C cables vary wildly in quality and price doesn't seem to factor in. I have had expensive ones and cheap ones and they either last forever or go bad in a couple of months.
  • BakerMan @ BakerMan:
    you guys hear there's a whole group of anti-pedo vigilantes on roblox?
  • Anrei @ Anrei:
    are they actual vigilantes or some kids yelling I hate pedos
  • Psionic Roshambo @ Psionic Roshambo:
    Roblox? Is that like robot Legos?
  • Anrei @ Anrei:
    i'd say more playmobile
  • BakerMan @ BakerMan:
    actual vigilantes
  • BakerMan @ BakerMan:
    like, get them banned
  • Anrei @ Anrei:
    oh
  • Anrei @ Anrei:
    was hoping you meant banned irl
  • K3Nv2 @ K3Nv2:
    @Psionic Roshambo, what's a type c cable
    K3Nv2 @ K3Nv2: @Psionic Roshambo, what's a type c cable