Object-oriented programming and shared memory

I am developing a object-oriented application and use programming C++ in
qnx4.24.

I need to develop an class where its methods are different processes,
that they need have to access the same attribute (variable) of the class. I
decided to use the feature of shared memory, however I have not had success.
I find that the reason is the mode as the attributes of the class are
called. I declared the attribute as " volatile float attribute".

I made some attempts without success. One that me seemed interesting to
illustrate the problem was the following one:

I did not use object-oriented programming C++. I created a program in
standard language C and the previous methods had been transformed into
functions. I used memory shared and in the end the great surprise: when I
compiled the program with compiler C, the program functioned. Already when I
compiled with the compiler C++, I got same the previous errors when it was
using object-oriented class.

Please, somebody already had some similar experience? Somebody can help
me? I do not know more what to make…

Gustavo.

It sounds like you have an error in your program that does not show up when
you use the C compiler.
Example: You use a variable uninitalized. Now, by accident, this variable
has the value that you want (because by accident the memory at that address
contains that value), but with the different compiler, it is not.
I don’t think it’s a problem with the compiler. I’m using shared memory from
C++ all the time. You probably didn’t initalize some structure or sth. like
that.

Maybe you wanna describe your error in more detail.

Markus

“Gustavo André” <gandre@usp.br> wrote in message
news:9frc4t$6g6$1@inn.qnx.com

I am developing a object-oriented application and use programming C++
in
qnx4.24.

I need to develop an class where its methods are different processes,
that they need have to access the same attribute (variable) of the class.
I
decided to use the feature of shared memory, however I have not had
success.
I find that the reason is the mode as the attributes of the class are
called. I declared the attribute as " volatile float attribute".

I made some attempts without success. One that me seemed interesting
to
illustrate the problem was the following one:

I did not use object-oriented programming C++. I created a program in
standard language C and the previous methods had been transformed into
functions. I used memory shared and in the end the great surprise: when I
compiled the program with compiler C, the program functioned. Already when
I
compiled with the compiler C++, I got same the previous errors when it was
using object-oriented class.

Please, somebody already had some similar experience? Somebody can
help
me? I do not know more what to make…

Gustavo.

\

You should first ensure you are checking all possible return
codes from the shmem calls. I am assuming you have a semaphore
to keep mutual exclusion over the shared memory region.

“Markus Loffler” <loffler@ces.clemson.edu> wrote in message
news:9frct5$721$1@inn.qnx.com

It sounds like you have an error in your program that does not show up
when
you use the C compiler.
Example: You use a variable uninitalized. Now, by accident, this variable
has the value that you want (because by accident the memory at that
address
contains that value), but with the different compiler, it is not.
I don’t think it’s a problem with the compiler. I’m using shared memory
from
C++ all the time. You probably didn’t initalize some structure or sth.
like
that.

Maybe you wanna describe your error in more detail.

Markus

“Gustavo André” <> gandre@usp.br> > wrote in message
news:9frc4t$6g6$> 1@inn.qnx.com> …
I am developing a object-oriented application and use programming
C++
in
qnx4.24.

I need to develop an class where its methods are different
processes,
that they need have to access the same attribute (variable) of the
class.
I
decided to use the feature of shared memory, however I have not had
success.
I find that the reason is the mode as the attributes of the class are
called. I declared the attribute as " volatile float attribute".

I made some attempts without success. One that me seemed interesting
to
illustrate the problem was the following one:

I did not use object-oriented programming C++. I created a program
in
standard language C and the previous methods had been transformed into
functions. I used memory shared and in the end the great surprise: when
I
compiled the program with compiler C, the program functioned. Already
when
I
compiled with the compiler C++, I got same the previous errors when it
was
using object-oriented class.

Please, somebody already had some similar experience? Somebody can
help
me? I do not know more what to make…

Gustavo.



\

Markus,

I am thankful for the so fast reply!
To summarize the explanation of my problem, I placed below the code
source of my implementation of the class. I only left what I found necessary
to understand itself.

//
//

#ifndef CLASSNAME_H
#define CLASSNAME_H


#include
#include

#define OFLAGS O_CREAT|O_RDWR
#define SIZE 4096
#define PROT PROT_READ|PROT_WRITE
#define MFLAGS MAP_SHARED

class NameClass
{
private:
pid_t pid_Proxy, pid_Control;
timer_t pid_Timer;
struct itimerspec timer;
struct timespec start;
struct sigevent event;
int ENTRADA,SAIDA, mem;
volatile float *TENSAO;

public:
NameClass(int Entrada, int Saida, int Amostragem);
~NameClass();
void Controle_Velocidade();
void Variar_Ref(float Tensao);
};


NameClass::NameClass(int Entrada, int Saida, int Amostragem)
{

mem = shm_open(“mem_test”,OFLAGS,0777);
if( mem != -1 )
{
if( ltrunc(mem,SIZE,SEEK_SET) != -1 )
{
TENSAO = mmap(0,SIZE,PROT,MFLAGS,mem,0);

}
}
};

NameClass::~NameClass()
{

close(mem);
shm_unlink(“mem_test”);

};

void NameClass::Controle_Velocidade()
{
int mem;
volatile float *TENSAO;

if(pid_Control = fork())
{
mem = shm_open(“mem_test”,O_RDWR,0777);
if( mem != -1 )
{
TENSAO = mmap(0,SIZE,PROT,MFLAGS,mem,0);

}

close(mem);
}
};

void NameClass::Variar_Ref(float Tensao)
{

*TENSAO = Tensao;

};

#endif

//
//

When compiling a program (using the compiler C++) that it uses the
implementation of class above, the compiler presents the following error in
relation to the code

TENSAO = mmap(0,SIZE,PROT,MFLAGS,mem,0);


nameclass.h(119): Error! E166: (col 32) cannot convert right pointer to type
of left operand
nameclass.h(119): Note! N393: (col 32) included from main.cpp(4)
nameclass.h(119): Note! N630: (col 32) source conversion type is “void *”
nameclass.h(119): Note! N631: (col 32) target conversion type is “float
volatile *”

beyond other errors the same type.
I am thankful since already for the aid!

Gustavo.

“Gustavo André” <gandre@usp.br> wrote in message
news:9frfsp$8kg$1@inn.qnx.com

Markus,

I am thankful for the so fast reply!
To summarize the explanation of my problem, I placed below the code
source of my implementation of the class. I only left what I found
necessary
to understand itself.

//
//

#ifndef CLASSNAME_H
#define CLASSNAME_H


#include
#include

#define OFLAGS O_CREAT|O_RDWR
#define SIZE 4096
#define PROT PROT_READ|PROT_WRITE
#define MFLAGS MAP_SHARED

class NameClass
{
private:
pid_t pid_Proxy, pid_Control;
timer_t pid_Timer;
struct itimerspec timer;
struct timespec start;
struct sigevent event;
int ENTRADA,SAIDA, mem;
volatile float *TENSAO;

public:
NameClass(int Entrada, int Saida, int Amostragem);
~NameClass();
void Controle_Velocidade();
void Variar_Ref(float Tensao);
};


NameClass::NameClass(int Entrada, int Saida, int Amostragem)
{

mem = shm_open(“mem_test”,OFLAGS,0777);
if( mem != -1 )
{
if( ltrunc(mem,SIZE,SEEK_SET) != -1 )
{
TENSAO = mmap(0,SIZE,PROT,MFLAGS,mem,0);

}
}
};

NameClass::~NameClass()
{

close(mem);
shm_unlink(“mem_test”);

};

void NameClass::Controle_Velocidade()
{
int mem;
volatile float *TENSAO;

That variable already exists in the class???


if(pid_Control = fork())
{
mem = shm_open(“mem_test”,O_RDWR,0777);
if( mem != -1 )
{
TENSAO = mmap(0,SIZE,PROT,MFLAGS,mem,0);

}

close(mem);
}
};

void NameClass::Variar_Ref(float Tensao)
{

*TENSAO = Tensao;

};

#endif

//
//

When compiling a program (using the compiler C++) that it uses the
implementation of class above, the compiler presents the following error
in
relation to the code

TENSAO = mmap(0,SIZE,PROT,MFLAGS,mem,0);



nameclass.h(119): Error! E166: (col 32) cannot convert right pointer to
type
of left operand
nameclass.h(119): Note! N393: (col 32) included from main.cpp(4)
nameclass.h(119): Note! N630: (col 32) source conversion type is “void *”
nameclass.h(119): Note! N631: (col 32) target conversion type is “float
volatile *”

Try:

TENSAO = (volatile float *) mmap ( …)

C++ is more strict about casting.


beyond other errors the same type.
I am thankful since already for the aid!

Gustavo.

Thanks, Mario!

Its suggestion functioned perfectly. My problem of some days without
solution finishes to be solved.
Thanks, again.

Gustavo.

“Mario Charest” <mcharest@deletezinformatic.com> escreveu na mensagem
news:9g0qo2$ch5$1@inn.qnx.com

Try:

TENSAO = (volatile float *) mmap ( …)

C++ is more strict about casting.

Hi Gustavo,

“Gustavo André” wrote:

Markus,

I am thankful for the so fast reply!
To summarize the explanation of my problem, I placed below the code
source of my implementation of the class. I only left what I found necessary
to understand itself.

//
//

#ifndef CLASSNAME_H
#define CLASSNAME_H


#include
#include

#define OFLAGS O_CREAT|O_RDWR
#define SIZE 4096
#define PROT PROT_READ|PROT_WRITE
#define MFLAGS MAP_SHARED

class NameClass
{
private:
pid_t pid_Proxy, pid_Control;
timer_t pid_Timer;
struct itimerspec timer;
struct timespec start;
struct sigevent event;
int ENTRADA,SAIDA, mem;
volatile float *TENSAO;

public:
NameClass(int Entrada, int Saida, int Amostragem);
~NameClass();
void Controle_Velocidade();
void Variar_Ref(float Tensao);
};

NameClass::NameClass(int Entrada, int Saida, int Amostragem)
{

mem = shm_open(“mem_test”,OFLAGS,0777);
if( mem != -1 )
{
if( ltrunc(mem,SIZE,SEEK_SET) != -1 )
^^^^^^

→ that’s the problem … you have to use
ftruncate().

I don’t know why ltrunc() leads to an invalid pointer and a memory
object with a size of zero :frowning:

Armin