How to convert char []/int into void *? newbie question

B

BNP

Hi All,


I have follwoing definitions in a file:

#define MAX_PARAMS 256
typedef struct
{
char funcName[128];
int numParams;
void *params[MAX_PARAMS];



}ML_Command;


int Execute (ML_Command cmd)
{
.....
.....
strcpy (cmd.funName, "SavePortInfo");
cmd.numParams = 5;
cmd.params [0] = (void *)sPortName; //char sPortName[]
cmd.params [1] = (void *)iBdRate; //int iBaudRate
......
......
errorCode= ML::Execute (cmd);//this a static function in ML class

return errorCode;
}

The question here is how do I convert sPortName and iBdRate in void *.
Are the cast I have written in code right? If it is wrong, please tell
me where I am making mistake? I am not getting any compilation errors,
but at runtime nothing is filled in
cmd.params [].

Please reply soon.
Thanks,

BNP
 
I

Ioannis Vranos

BNP said:
Hi All,


I have follwoing definitions in a file:

#define MAX_PARAMS 256
typedef struct
{
char funcName[128];
int numParams;
void *params[MAX_PARAMS];



}ML_Command;


int Execute (ML_Command cmd)
{
....
....
strcpy (cmd.funName, "SavePortInfo");
cmd.numParams = 5;
cmd.params [0] = (void *)sPortName; //char sPortName[]
cmd.params [1] = (void *)iBdRate; //int iBaudRate
.....
.....
errorCode= ML::Execute (cmd);//this a static function in ML class

return errorCode;
}

The question here is how do I convert sPortName and iBdRate in void *.


The casting is not needed. The conversion takes place implicitly.

Are the cast I have written in code right? If it is wrong, please tell
me where I am making mistake? I am not getting any compilation errors,
but at runtime nothing is filled in
cmd.params [].


What do you mean nothing is filled in cmd.params[]? What check did you use? What exactly
do you intend to do with params[] after the assignments?
 
M

Mark Stijnman

BNP said:
Hi All,


I have follwoing definitions in a file:

#define MAX_PARAMS 256
typedef struct
{
char funcName[128];
int numParams;
void *params[MAX_PARAMS];



}ML_Command;


int Execute (ML_Command cmd)
{
....
....
strcpy (cmd.funName, "SavePortInfo");
cmd.numParams = 5;
cmd.params [0] = (void *)sPortName; //char sPortName[]
cmd.params [1] = (void *)iBdRate; //int iBaudRate
.....
.....

I think you mean to do:

cmd.params[0] = (void *) &sPortName;
cmd.params [1] = (void *) &iBdRate;

actually storing the -address- of the variables sPortName and iBaudRate
in the cmd.params array. What your piece of code does, is trying to
convert the -value- of the variables to a pointer, which will give
undefined behaviour.
errorCode= ML::Execute (cmd);//this a static function in ML class

return errorCode;
}

The question here is how do I convert sPortName and iBdRate in void *.
Are the cast I have written in code right? If it is wrong, please tell
me where I am making mistake? I am not getting any compilation errors,
but at runtime nothing is filled in
cmd.params [].

Please reply soon.
Thanks,

BNP
 
B

BNP

the ML::Execute (cmd) converts the paramters from void * to appropriate
types depending on the cmd.funcName and calls the cmd.funcName function
from a DLL.

So I used watch window (vc++) to check the change of addresses & values
for cmd.params[] array. the ML::Execute (cmd) calls the right function,
but values filled in the parameters of the function are garbage values.


The project design is such that types of function parameters are not
known to the application. the ML class know the exact type information
for each parameter. So application just need to pass void * to the ML
class, and ML converts in to appropriate types.

Please help me solve this problem.
Thanks

BNP
 
I

Ioannis Vranos

BNP said:
the ML::Execute (cmd) converts the paramters from void * to appropriate
types depending on the cmd.funcName and calls the cmd.funcName function
from a DLL.


A first note here is that you are essentially programming in an antiquated C++ style
(C-style).


If you want to store strings, you should use std::string. If you want run-time type
identification (RTTI) then you should use the RTTI facilities of C++. If you want
compile-time generic programming you should use templates.

So I used watch window (vc++) to check the change of addresses & values
for cmd.params[] array. the ML::Execute (cmd) calls the right function,
but values filled in the parameters of the function are garbage values.



Casting to void * is not needed. Also you forgot & in this one:


cmd.params [1] = &iBdRate; //int iBaudRate


Apart from this, probably the original code is not doing precisely what you are mentioning
here.


For example there is no problem with this code:


#include <iostream>


int main()
{
using namespace std;

char s[10]= {'a'};

int x= 2;

void *varray[5]= {s, &x};

cout<< static_cast<char *>(varray[0])[0]<< "\n" << * static_cast<int *>(varray[1]);
}



Your code sample converted to modern C++:


namespace Something
{
using namespace std;

const int MAX_PARAMS= 256;


typedef struct
{
string funcName;
int numParams;

// This is not needed.
// Use compile-time templates (more usually) or RTTI instead.
vector<void *> params(MAX_PARAMS);
}ML_Command;

// ...
}


int Execute (ML_Command cmd)
{
//...
//...
cmd.funName= "SavePortInfo";
cmd.numParams = 5;

// Casting removed
cmd.params [0] = sPortName; //char sPortName[]
cmd.params [1] = &iBdRate; //int iBaudRate
//...
//...

// Better throw an exception in case of error
errorCode= ML::Execute (cmd);//this a static function in ML class

return errorCode;
}
 
H

hibiki

BNP a écrit :
the ML::Execute (cmd) converts the paramters from void * to appropriate
types depending on the cmd.funcName and calls the cmd.funcName function
from a DLL.

So I used watch window (vc++) to check the change of addresses & values
for cmd.params[] array. the ML::Execute (cmd) calls the right function,
but values filled in the parameters of the function are garbage values.


The project design is such that types of function parameters are not
known to the application. the ML class know the exact type information
for each parameter. So application just need to pass void * to the ML
class, and ML converts in to appropriate types.

Please help me solve this problem.
Thanks

BNP
I think you need a second struct like this

struct params {
char* port_name;
int baud_rate;
bool other_param;
short another_param_again;
}

struct func_name {
char func_name[128];
int num_params;
struct params param;
}

I also think the structure of your program is wrong, it is not necessary
to try that sort of cheat.
You're an asm programmer, am i wrong ?

--
Salutations,

Joachim Naulet

06 14 90 06 21
http://jnaulet.no-ip.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top