boost interprocess managed shared memory raw pointer as a class member - c++

What I want is to access the data info of an managed shared memory object using a class named ShmObj with the raw pointer to the shared objects as private member, as code blocks below.
My problem is the main program segmentation fault. I guess the absolute raw pointer causes the problem. I tried to change the raw pointer to bi::offset_ptr but doesn't help. Any help is appreciated.
ShmObj.h
#include <string>
#include <boost/interprocess/managed_shared_memory.hpp>
namespace bi = boost::interprocess;
class ShmObj {
public:
ShmObj() {
bi::managed_shared_memory segment(bi::open_only, "shm");
pNum = segment.find<int>("Number").first;
}
int getNumber() {return *pNum;}
virtual ~ShmObj() {}
private:
int* pNum;
};
main.cpp
#include "ShmObj.h"
#include <iostream>
int main() {
ShmObj X;
std::cout << X.getNumber() << std::endl;
}

Your shared memory segment is destructed at the end of the constructor... Move it to a field to extend its lifetime:
#include <string>
#include <boost/interprocess/managed_shared_memory.hpp>
namespace bi = boost::interprocess;
class ShmObj {
public:
ShmObj()
: segment(bi::open_or_create, "shm", 32ul*1024),
pNum(0)
{
pNum = segment.find_or_construct<int>("Number")(0);
}
int getNumber() {
assert(pNum);
return *pNum;
}
virtual ~ShmObj() {}
private:
bi::managed_shared_memory segment;
int* pNum;
};
#include <iostream>
int main() {
ShmObj X;
std::cout << X.getNumber() << std::endl;
}

Related

How to change objects inside of vectors globally c++

When an object is placed inside a vector, the vector makes a copy of the object, this means that any changes to the object does not change the object inside the vector. How do i make so any changes to the object also change the object inside the vector.
#include <iostream>
#include <vector>
using namespace std;
class objectClass {
public:
int x = 0;
};
vector<int> my_vector;
int main()
{
objectClass testObject;
vector<objectClass> my_vector = {testObject};
testObject.x = 5;
cout << my_vector[0].x;
}
//outputs 0
You can do this in two way
change the object from the vector like :
#include <iostream>
#include <vector>
using namespace std;
class objectClass {
public:
int x = 0;
};
vector<int> my_vector;
int main()
{
objectClass testObject;
vector<objectClass> my_vector = {testObject};
my_vector[0].x = 5;
cout << my_vector[0].x;
}
or you can do this by using pointers or reference, use a vector of pointers, where the vector will have the collections of objects references and thus changing the object from outside the vector also will change from inside the vector.
#include <iostream>
#include <vector>
using namespace std;
class objectClass {
public:
int x = 0;
};
int main()
{
objectClass testObject;
vector<objectClass*> my_vector = {&testObject};
testObject.x = 5;
cout << my_vector[0]->x;
}
One way of doing it would be having a vector of pointers instead, where each pointer points to some instance of objectClass:
#include <iostream>
#include <vector>
using namespace std;
class objectClass {
public:
int x = 0;
};
int main()
{
objectClass testObject;
vector<objectClass*> my_vector = {&testObject};
testObject.x = 5;
cout << my_vector[0]->x;
}

How to acess elements of structs inside of a list created with <list> library?

I'm new to C++, coming from C. How do I access each element of each struct in a std::list created with the <list> library?
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <list>
#include <funcoes.h>
using namespace std;
typedef struct candidato{
int inscricao;
int idade;
int cod;
int nota;
}candidato_c;
int main(){
list<candidato_c> l;
startlist(l);
}
funcoes.h
void startlist (list<candidato_c>& lista1){
//How to access each element of each index?
}
This is how to to access each element
struct candidato {
int inscricao;
int idade;
int cod;
int nota;
};
int main(){
list<candidato> l;
startlist(l);
}
static void startlist(const std::list<candidato>& lista1) {
for (const candidato& c : lista1)
{
std::cout << c.cod << std::endl;
}
}

setter and getter /classes

I have a class Project and each Project can have different tasks.
Project.h:
#pragma once
#include "Task.h"
#include <vector>
using namespace std;
class Project
{
private:
vector<Task> Tasks;
public:
Project::Project(int inleesgetal);//constructor
vector<Task> GetTasks();
};
Project.cpp:
#include "Project.h"
#include <string>
#include <vector>
Project::Project(int inleesgetal)
{
//constructor
Tasks.resize(Numbertasks);
}
vector<Task> Project::GetTasks()
{
return Tasks;
}
Task.h:
#pragma once
#include <vector>
using namespace std;
class Task
{
private:
//Info:
int StartTime_Solo;
public:
Task(); //constructor
void SetStartTime_Solo(int st_s);
int GetStartTime_Solo();
};
Task.cpp:
#include "Task.h"
#include <string>
#include <vector>
#include <iostream>
using namespace std;
Task::Task()
{
//constructor
StartTime_Solo = 0;
}
int Task::GetStartTime_Solo()
{
return StartTime_Solo;
}
void Task::SetStartTime_Solo(int st_s)
{
StartTime_Solo = st_s;
}
main:
#include <iostream>
#include <vector>
#include "Task.h"
#include "Project.h"
using namespace std;
int main()
{
Project Project1(6);
Project1.GetTasks()[2].SetStartTime_Solo(55);
cout << "test:" << Project1.GetTasks()[2].GetStartTime_Solo();
return 0;
}
Now when I try to set the 3rd task of Project1 to a starttime of 55 and then print the start time out it still gives me 0 as a result.
Why is this? And how can I change my code so it actually sets the starttime to 55?
vector<Task> GetTasks();
should be
const vector<Task>& GetTasks() const;
vector<Task>& GetTasks();
And so with definitions:
vector<Task> Project::GetTasks()
{
return Tasks;
}
should be:
const vector<Task>& Project::GetTasks() const { return Tasks; }
vector<Task>& Project::GetTasks() { return Tasks; }
The problem is that you are returning a copy of the vector<Task> from the GetTasks function. You then modify this copy and throw it away right afterwards. The internal member of Project is not changed.
If you return by reference like this:
vector<Task>& GetTasks();
Then you are basically returning something that points to the internal vector, and so when you modify it, you actually modify the member data of your class.

how to use pointer to ostream in class

I need ostream pointer in class which will be created at the time of construction of class.
My code is :
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <cstring>
#include <climits>
#include <cstdio>
#include <fstream>
using namespace std;
class test2_t {
public:
test2_t ()
{
std::filebuf fb;
fb.open ("dump.txt",std::ios::out);
ostream *output_ = new std::ostream(&fb);
}
virtual ~test2_t ()
{}
ostream *output_;
void printing()
{
print(output_);
}
void print(ostream *out)
{
*out<<"dump data"<<"\n";
}
private:
/* data */
};
int main( )
{
test2_t obj;
obj.printing();
}
But is getting Segmentation fault I don't know why. Please help me out.
You made the following mistake in your code:
You "redeclared" your "Output"-variable in the constructor - so the pointer ios only stored in a local variable within the constructor-scope.
change this line: ostream *output_ = new std::ostream(&fb);
into: *output_ = new std::ostream(&fb);
In this way, the member-variable of your class is filled with the correkt pointer.
You can change your construtor function like follows to get it working:
test2_t () : output_(new std::ofstream("dump.txt")) {
}
Don't forget to release the resource in the destructor:
virtual ~test2_t () {
delete output_;
}

Invalid use of non static data member C++

Here is my code
main.cpp
#include <iostream>
#include "header.h"
#include "source.cpp"
using namespace std;
int main()
{
cout << "Hello world!" << endl;
int testarray[]={1,3,5,7};
mymatrix* first=new mymatrix(testarray,2,2);
return 0;
}
and header.h
using namespace std;
#include <iostream>
#include <string>
class mymatrix{
public:
int i;
int j;
int marray[];
mymatrix(int m[],int rows,int cols ) : marray(m),i(rows),j(cols)
{
cout<<"this is for testings ";
}
mymatrix()
{};
~mymatrix(){
// delete[] marray;
};
};
I get this error :Invalid use of non static data member mymatrix::i
what I wanna do is make a object of my
matrix class and pass an array
Convert it from
int marray[];
to
int *marray;
In addition, either use C paradigm or use C++ one but not the mixture.
Instead of
mymatrix* first=new mymatrix(testarray,2,2);
use
mymatrix first(testarray,2,2);
Let the compiler allocate and release the memory instead of you.
If you have no restriction about the C++ libraries that you use, consider std::vector library to manage your dynamic arrays.
Instad of managing memory out of the object, manage it inside the object specially inside constructor and destructor.