C++ map segmentation fault (core dumped) - c++

I'm trying to use an map with string key, but it's not working and I couldn't figure out why.
I would like to have some help to understand C++ fundamentals about the usage of this so essential structure.
model.hpp
#pragma once
#include <map>
#include <utility>
#include <string>
#include <iostream>
#include "../prs/ast.h"
using namespace std;
using namespace ast;
typedef map<string, Variable> Map;
typedef pair<string, Variable> Element;
namespace model{
class Warehouse {
public:
Map stock;
Warehouse(){
Map* _stock = new Map();
stock = *_stock;
}
Variable* get(string id){
Map::iterator it = stock.find(id);
if (it != stock.end()){
return &(*it).second;
}
else {
return __define__(id);
}
}
Variable* __define__(string id){
Variable* defined = new Variable(id);
stock.insert(Element(id, *defined));
return defined;
}
};
static Warehouse* WAREHOUSE;
};
model.cpp
#pragma once
#include "model.hpp"
using namespace std;
namespace model {
Warehouse* WAREHOUSE = new Warehouse();
}
In this context, Variable is a project object defined in ast
namespace already tested, as well WAREHOUSE pointer is working accordingly, with class
initialized
The instruction stock.find(id) is throwing the mentioned error message: Segmentation fault (core dumped), what I suppose means stock isn't correct initialized.
What is exactly happening with stock initialization done at Warehouse constructor?
I understand that new keyword allocs the map and dereference its returned point would store the structure at stock Warehouse member attribute.
Am I misunderstand it?

WAREHOUSE is a static variable defined in a header. What this means is that every source file that includes that header gets its own copy of this variable, initialized to nullptr. Only one source file sets its own copy to a non-null value. Presumably, some other source file in the code not shown attempts to dereference its copy.
Make it
extern Warehouse* WAREHOUSE;

Related

C++ Removing Unused Class Attribute Causes std::logic_error

I have a class which has a few attributes like shown below, my problem is that when I remove or place the string s attribute before std::atomic<char*> atomic_input the program terminates with exception:
'std::logic_error'
what(): basic_string::_M_construct null not valid
Aborted (core dumped)
#include <string>
#include <atomic>
// In ui.cpp
class UI
{
private:
std::atomic<char*> atomic_input;
std::string s; /* this can be renamed, but removing or placing it
before the above field crashes the program */
};
// In main.cpp
#include "ui.cpp"
int main()
{
srand (time(NULL));
initscr(); /* start the curses mode */
UI* ui = new UI();
return 0;
}
The string attribute is not accessed within the program in any way, renaming it is possible. The reason why I have an atomic field is that the value is shared among several threads.
I have tried placing the string field in different lines within the class attributes, the program only crashes if the declaration is before the atomic_input.
What might be causing the problem? Is it something to do with how the classes in C++ should be defined?
Looks like I've found a solution.
std::atomic<char*> atomic_input not being initialized like seen below was causing the issue. I still don't know how the string variable was interfering with it.
My guess is that the compiler somehow interprets the string as a constructor for atomic_input. The error only occurs when atomic_input is accessed in runtime and not in the compilation.
#include <string>
#include <atomic>
// In ui.cpp
class UI
{
private:
std::atomic<char*> atomic_input{(char*)""};
// std::string s; /* Initializing the atomic char like above solved the problem */
};

Using private variable in class

I'm running into an irritating problem where my program keeps crashing if I try to reference a private variable that I have created in one of my classes. I can't figure out where I am going wrong. Here is the class that calls the class that crashes:
#include <stack>
#include <fstream>
#include <ostream>
#include <cstdlib>
#include <string>
#include <set>
#include "schemeList.cpp"
using namespace std;
class dataLog
{
public:
stack<string> commands;
set<string> domain;
processor tokens;
int nextToken;
schemeList * s;
dataLog(stack<string> s, ofstream * out, processor p, int location)
{
commands = s;
tokens = p;
nextToken = location;
commands.push("<Query List>");
commands.push(":");
commands.push("Queries");
commands.push("<Rule List>");
commands.push(":");
commands.push("Rules");
commands.push("<Fact List>");
commands.push(":");
commands.push("Facts");
commands.push("<Scheme List>");
commands.push(":");
commands.push("Schemes");
checkNext();
}
void checkNext()
{
for(int i = 0; i < tokens.tags.size(); i++)
{
if(commands.top().compare(tokens.tags[i].getName())!=0)
{
if(commands.top().find("<")==0)
{
if(commands.top().compare("<Scheme List>")==0)
{
int output = (*s).process(i, tokens, domain); string hi = (*s).toString();
}
}
}
commands.pop();
}
}
};
This class creates an object of my SchemeList class, which is written out as follows:
#include "schemes.cpp"
#include <cstdlib>
#include <string>
#include <set>
using namespace std;
class schemeList
{
private:
string success;
public:
int process(int number, processor p, set<string> domain)
{
success = "HELLO";
return 13;
}
string toString()
{
return success;
}
};
As soon as I get to line 15 success = "HELLO";, the program crashes with the message
Unhandled exception at 0x00E48B66 in lab2.exe: 0xC0000005: Access violation reading
location 0xCCCCCCE4.
I am using Microsoft Visual Studio Express 2012 for Windows Desktop.
First off, the variable schemeList * dataLog::s is never initialized, so accessing it is undefined behavior, which leads to the crash. Most likely calling process on a dangling pointer and attempting to write into some memory you don't own.
Second, don't #include "schemeList.cpp". You're not supposed to include cpp files. Rather, separate declarations & implementations and include a header.
You have not initialized dataLog::s. When you call (*s).process(i, tokens, domain), you get undefined behavior.
Firstly, you're apparently including source code files in headers. This will likely break the one definition rule and go horribly wrong.
Secondly, 's' is not a very good name for a class member. It makes it almost impossible to find uses of it.
Thirdly, I can see nowhere in your code that initialises s. I can see where it gets referenced OK, but as it hasn't been initialised, the effect of dereferencing is undefined, and with luck will merely crash your program, which looks like what is happening.

Explain the error: ISO C++ forbids declaration of `Personlist' with no type

I have a class which is going to handle an array of objects of another class I've created earlier (which works fine). The problem appears when I try to create an object of my List-class.
This is the header of the list-class:
#ifndef personlistH
#define personlistH
#include "Person.h"
#include <iomanip>
#include <iostream>
#define SIZE 10
namespace std {
class PersonList {
private:
Person persons[SIZE];
int arrnum;
string filename;
public:
Personlist();
};
}
#endif
This is the main function:
#include <iostream>
#include "PersonList.h"
using namespace std;
int main() {
PersonList personlist;
return 0;
}
The error my compiler is giving me is the following:
error: "27 \PersonList.h ISO C++ forbids declaration of `Personlist'
with no type"
I've searched for answers but as I'm quite new to C++ it's been a bit confusing and I haven't found any fitting yet. It would be great if you could explain this error for me.
You have the wrong capitalisation on your constructor declaration. You have Personlist(); but need PersonList();. Because what you have isn't equal to the class name it is considered a function rather than a constructor, and a function needs a return type.
Do not add your own types to the standard namespace(std), instead create your own namespace and define your class inside it.
//PersonList.h
namespace PersonNamespace
{
class PersonList
{
//members here
};
}
//Main.cpp
using namespace PersonNamespace;
The actual error is that you made a typo in Personlist instead of PersonList
The error is because you got the capitalisation wrong when you declared the constructor; it should be PersonList() not Personlist().
Also, you should never declare your own classes in the std namespace; that's reserved for the standard library. You shoud make up your own namespace name, and put your things in that.

Insert into multimap causes segfault

I am working on a project using multimaps inside of my own class, and I have run into a segfault. Here are the parts of my code relating to the issue. I would really appreciate some help. Thanks.
Here is database.h
#include <iostream>
#include <map>
using namespace std;
class database{
public:
database(); // start up the database
int update(string,int); // update it
bool is_word(string); //advises if the word is a word
double prox_mean(string); // finds the average prox
private:
multimap<string,int> *data; // must be pointer
protected:
};
Here is the database.cpp
#include <iostream>
#include <string>
#include <map>
#include <utility>
#include "database.h"
using namespace std;
// start with the constructor
database::database()
{
data = new multimap<string,int>; // allocates new space for the database
}
int database::update(string word,int prox)
{
// add another instance of the word to the database
cout << "test1"<<endl;
data->insert( pair<string,int>(word,prox));
cout << "test2" <<endl;
// need to be able to tell if it is a word
bool isWord = database::is_word(word);
// find the average proximity
double ave = database::prox_mean(word);
// tells the gui to updata
// gui::update(word,ave,isWord); // not finished yet
return 0;
}
Here is test.cpp
#include <iostream>
#include <string>
#include <map>
#include "database.h" //this is my file
using namespace std;
int main()
{
// first test the constructor
database * data;
data->update("trail",3);
data->update("mix",2);
data->update("nut",7);
data->update("and",8);
data->update("trail",8);
data->update("and",3);
data->update("candy",8);
// cout<< (int) data->size()<<endl;
return 0;
}
Thanks very much. It compiles and runs up to cout << "test1" << endl; but segfaults on the next line.
Rusty
You never actually created a database object, just a pointer to nowhere (perhaps you're used to another language).
Try creating one like this database data;
And then change your -> to . to access the members.
Consider obtaining one of the books at The Definitive C++ Book Guide and List as well.
You need to allocate your database before starting to insert data on it.
Change:
database *data;
to:
database *data = new database();
or:
database data;
in main().
EDIT: if you use the latter, change -> to . on the subsequent method calls. Otherwise, remember to delete your data object after using it.

Why can't initialize the static member in a class in the body or in the header file?

Could any body offer me any reason about that?
If we do it like that, what's the outcome? Compile error?
The problem is that static initialization isnt just initialization, it is also definition. Take for example:
hacks.h :
class Foo
{
public:
static std::string bar_;
};
std::string Foo::bar_ = "Hello";
std::string GimmeFoo();
main.cpp :
#include <string>
#include <sstream>
#include <iostream>
#include "hacks.h"
using std::string;
using std::ostringstream;
using std::cout;
int main()
{
string s = GimmeFoo();
return 0;
}
foo.cpp :
#include <string>
#include <sstream>
#include <iostream>
#include "hacks.h"
using std::string;
using std::ostringstream;
using std::cout;
string GimmeFoo()
{
Foo foo;
foo;
string s = foo.bar_;
return s;
}
In this case, you can't initialize Foo::bar_ in the header because it will be allocated in every file that #includes hacks.h. So there will be 2 instances of Foo.bar_ in memory - one in main.cpp, and one in foo.cpp.
The solution is to allocate & initialize in just one place:
foo.cpp :
...
std::string Foo::bar_ = "Hello";
...
It is just a limitation in the language it self. Hopefully, when C++0x becomes reality, this limitation would go away.
I think this page gives a somehow good reason:
One of the trickiest ramifications of
using a static data member in a class
is that it must be initialized, just
once, outside the class definition, in
the source file. This is due to the
fact a header file is typically seen
multiple times by the compiler. If the
compiler encountered the
initialization of a variable multiple
times it would be very difficult to
ensure that variables were properly
initialized. Hence, exactly one
initialization of a static is allowed
in the entire program.