C++ crash Segmentation fault: 11 - c++

I have been working on a program to test out a few string manipulation possibilities. It is basically supposed to read a string list and be able to find a character's neighbors to go through the strings as a circuit. Here's the code:
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
std::string grid[20]={" "};
std::string get(int string, int member){
return grid[string].substr(member,1);
}
std::string* getNeighbors(int string, int member){
std::string neighbors[4];
neighbors[0]=grid[string-1].substr(member,1);//up
neighbors[1]=grid[string+1].substr(member,1);//down
neighbors[2]=grid[string].substr(member-1,1);//left
neighbors[3]=grid[string].substr(member+1,1);//right
std::string* p=neighbors;
return p;//Returns up,down,left,right.
}
int main(int argc, char** argv){
grid[1]="#----^---0";
grid[2]="abcdefghi0";
grid[3]="jklmnopqr0";//TODO Change to read of txt*/
std::string* neighbors;
for(int i=0;grid[1].length()>i;i++){
neighbors=getNeighbors(2,1);
if(neighbors[3]=="-" | neighbors[3]=="^"){
std::string r=get(1,i);
(r!="0") ? std::cout<<r:0;//Dangerous. TODO Unknown symbol handling
std::cout<<neighbors[3];
}
}
}
This compiles well, but has the runtime error "Segmentation fault: 11". I am using several subjects and techniques that I am not used to and am likely misusing. Any help would be great.

std::string neighbors[4]; is stack allocated. When you go out getNeighborsit looses scope. Try to put it other place (even globaly, just as a proof of concept). A better design should be pass this as parater to your function.
void getNeighbors(int string, int member, std::vector<std::string>& neighbors){
;
neighbors[0]=grid[string-1].substr(member,1);//up
neighbors[1]=grid[string+1].substr(member,1);//down
neighbors[2]=grid[string].substr(member-1,1);//left
neighbors[3]=grid[string].substr(member+1,1);//right
}
EDIT:
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
std::string grid[20]={" "};
std::string neighbors[4]; //<---------------------------
std::string get(int string, int member){
return grid[string].substr(member,1);
}
std::string* getNeighbors(int string, int member){
neighbors[0]=grid[string-1].substr(member,1);//up
neighbors[1]=grid[string+1].substr(member,1);//down
neighbors[2]=grid[string].substr(member-1,1);//left
neighbors[3]=grid[string].substr(member+1,1);//right
std::string* p=neighbors;
return p;//Returns up,down,left,right.
}
int main(int argc, char** argv){
grid[1]="#----^---0";
grid[2]="abcdefghi0";
grid[3]="jklmnopqr0";//TODO Change to read of txt*/
std::string* neighbors;
for(int i=0;grid[1].length()>i;i++){
neighbors=getNeighbors(2,1);
if(neighbors[3]=="-" | neighbors[3]=="^"){
std::string r=get(1,i);
(r!="0") ? std::cout<<r:"0";//Dangerous. TODO Unknown symbol handling
std::cout<<neighbors[3];
}
}
}
The neighborsnow is global (I don´t like this, but do the job for the POC).

getNeighbors() is returning a pointer to a local variable.

Related

Need help using strncpy() in C++

I am stuck for hours during my assignment. Specifically, on this part:
The constructor should take a const-qualified C-Style string as its argument. Use the strncpy() function from the <cstring> library to copy it into the underlying storage. Be sure to manually null-terminate the attribute after you copy to assure that it is a valid C-String (in case the parameter contained a much larger string).
Where am I making mistakes, and how should I change my code?
#ifndef STRINGWRAPPER_H
#define STRINGWRAPPER_H
class StringWrapper{
public:
StringWrapper (const char myString);
const static int max_capacity = 262144;
private:
int size = 1;
char myString [40];
};
#endif
#include "StringWrapper.h"
#include <cstring>
StringWrapper::StringWrapper (const char myString){
strncpy(StringWrapper::myString, myString, sizeof(myString));
}
#include <iostream>
#include "ThinArrayWrapper.h"
#include "ArrayWrapper.h"
#include "StringWrapper.h"
#include <stdexcept>
int main(){
char myString[]{ "string" };
StringWrapper StringWrapper('h');
return 0;
}
First of all, your call to strncpy is wrong. Please check the reference regarding the strncpy from here.
According to the definition of strncpy :
char *strncpy(char *dest, const char *src, std::size_t count);
In your case, you are calling strncpy like this:
strncpy(StringWrapper::myString, myString, sizeof(myString));
Here, myString is a const char type variable. You need to make it to const char *. If you like, you can check my modification of your code from here.

declaration of ‘args’ as array of references Error [duplicate]

This question already has answers here:
Why are arrays of references illegal?
(14 answers)
Closed 8 years ago.
I am newbie in c++ boost , I having a program trying to compile it

#include "Program.h"
#include <boost/asio/io_service.hpp>
#include <boost/asio/streambuf.hpp>
#include <boost/asio/ip/address.hpp>
#include <boost/asio/ip/udp.hpp>
namespace ConsoleApp
{
void Main(std::wstring& args[])
{
.
.
}
}
the error appear is
Program.cpp:11:31: error: declaration of ‘args’ as array of references
void Main(std::wstring& args[])
anyone here can help me , is this code error ?
thanks
The error is pretty much saying everything. std::wstring& args[] is array ([]) of wstring (std::wstring) references (&). You cannot have array of references - see Why are arrays of references illegal?.
Note: you're coding in C++, main function should be following:
int main(int argc, char *argv[])
{
// Your code
return 0;
}
EDIT:
And AFAIK main function cannot be in any namespace.
Also, there is one more problem with your code - even if we could create array of references, there is not stored information about length of the array. You couldn't use it except first element!
Anyway, you can do following (replaced wstring with string because I'm lazy):
#include <vector>
#include <string>
namespace ConsoleApp
{
void Main(std::vector<std::string> &args)
{
}
}
int main(int argc, char *argv[])
{
std::vector<std::string> args;
args.resize(argc);
for(int i = 0; i < argc; ++i)
{
args[i] = argv[i];
}
ConsoleApp::Main(args);
return 0;
}

AddObject in TStringList

This is the first time I use AddObject method in C++ Builder 6 in a TStringList
but I can not add an integer to the object list for example. Of course I did it by means of casting different types. But it's not what I want. Please help me do it simpler
and why the objects must be Tobject* in object list
this is my simple program...
#include <vcl.h>
#include <iostream.h>
#include <conio.h>
#pragma hdrstop
#pragma argsused
int main(int argc, char* argv[])
{
int r=random(100+1);
TStringList *mylist=new TStringList;
mylist->AddObject("r",(TObject *)r);
int i=mylist->IndexOf("r");
int a=(int)(mylist->Objects[i]);
cout<<a<<endl;
getch();
return 0;
}
Use a std::map or other suitable container instead, eg:
#pragma hdrstop
#include <iostream.h>
#include <conio.h>
#include <map>
#pragma argsused
int main(int argc, char* argv[])
{
int r = random(100+1);
std::map<std::string, int> mylist;
mylist["r"] = r;
int a = mylist["r"];
cout << a << std::endl;
getch();
return 0;
}
why the objects must be Tobject*
Because of Borland's design of VCL.
mylist->AddObject("r",(TObject *)r);
Do not do this, because there's no guarantee that TStringList wouldn't call some methods of TObject* inside AddObject. For example it can call objectName() or incrementReference() (I know, that there's no TStringList::incrementReference() but it's just an example).
IMHO, all that you need is std::map:
#include <map>
int main()
{
int r=random(100+1);
std::map< AnsiString, int > myList;
myList[ "r" ] = r;
int a = myList[ "r" ];
}
It's very ugly but the cast will work fine. The pragmatic approach is to stick with that method if you must use TStringList.
If you wanted, you could add TObject derived wrappers that hold 'int's to another vector and use these pointers but it require more code to maintain that second list, is more prone to errors and a lot slower.
Alternatively new these wrappers and add them to the stringlist and then manage deletion of the objects manually. This is error prone.
The best approach may be to ditch TStringList if that is possible. Use a struct/class or a std::pair and std::vector or std::deque.
e.g.
typedef std::pair<AnsiString, int> MyValue;
typedef std::vector<MyValue> MyValueList;
MyValueList list;
list.push_back(MyValue("hello", 1));
AnsiString const& s = list[0].first;
int i = list[0].second;

simple c++ function inclusion failure

I am trying to include a function from another file inside a "main" file. I'm following this paradigm:
http://www.learncpp.com/cpp-tutorial/18-programs-with-multiple-files/
Here is my main file, digispark.cpp:
#include <iostream>
using namespace std;
int send(int argc, char **argv);
int main()
{
char* on;
*on = '1';
char* off;
*off = '0';
send(1,&on);
return 0;
}
And here is my send.cpp:
#include <stdio.h>
#include <iostream>
#include <string.h>
#if defined WIN
#include <lusb0_usb.h> // this is libusb, see http://libusb.sourceforge.net/
#else
#include <usb.h> // this is libusb, see http://libusb.sourceforge.net/
#endif
// I've simplified the contents of send for my debugging and your aid, but the
// complicated arguments are a part of the function that will eventually need
// to be here.
int send (int argc, char **argv)
{
std::cout << "Hello";
return 0;
}
I'm compiling on Ubuntu 12.10 using the g++ compiler like so:
g++ digispark.cpp send.cpp -o digispark
It compiles successfully.
However, when I run the program, "Hello" does not come up. Therefore I don't believe the function is being called at all. What am I doing wrong? Any help would be great! Thanks!
EDIT:
How I dealt with the issue:
int send(int argc, char **argv);
int main()
{
char* on[4];
on[0] = (char*)"send";
on[1] = (char*)"1";
char* off[4];
off[0] = (char*)"send";
off[1] = (char*)"0";
send(2,on);
return 0;
}
For those of you who were confused as to why I insisted doing this, as I said before, the send function was already built to accept the char** argv (or char* argv[]). My point was to try to mimic that in my main function.
It would have been much more difficult to rewrite the function that actually goes in the send function to take a different type of argument than just to send in what it wanted. Thanks everyone!
So if this helps anyone trying something similar feel free to use it!
Your problem is not the one you think it is. It's here:
char* on;
*on = '1';
You declared a char pointer, but did not initialize it. Then you dereferenced it. Bang, you're dead. This is what is known as Undefined Behavior. Once you invoke U.B., anything can happen. If you're lucky, it's a crash. But I guess you weren't lucky this time.
Look, if you want to start storing things in memory, you have to allocate that memory first. The best way, as hetepeperfan said, is to just use std::string and let that class take care of all the allocating/deallocating for you. But if for some reason you think you have to use C-style strings and pointers, then try this:
char on[128]; //or however much room you think you'll need. Don't know? Maybe you shoulda used std::string ...
*on = '1';
*(on+1) = '\0'; //if you're using C-strings, better null terminate.
char off[128];
*off = '0';
*(off+1) = '\0';
send(1,&on);
ok I think you try to do something like the following, I tried to make it a bit more in the Style of C++ and prevent the use of pointers since they should not be necessary in the code that you showed.
digispark.cpp
#include "send.h"
int main (int argc, char** argv){
string on = "1";
string off = "0";
send ( on );
send ( off );
return 0;
}
send.cpp
#include <iostream>
#include <string>
void send( const std::string& s) {
std::cout << s << std::endl;
}
send.h
void send(const std::string& s);

C++ STL remove error

I'm having trouble understanding where I went wrong with my code:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[]) {
string str = "";
cin >> str;
remove(str.begin(), str.end(), ' ');
cout << str;
cin.ignore();
}
The error says "'remove': function does not take 3 arguments (C2660)"
Try adding
#include <algorithm>
"algorithm" is an STL header containing a lot of functions, including std::remove, which the OP is trying to call. The error he got was because there is another function that takes a single argument, called "remove", which deletes a file.