Jul 3, 2008 at 6:47pm UTC
Hi,
I'm rather new to Cpp and I can't understand why I keep getting an Access Violation, when I initialize something in another function (in Java we'd do this sort of thing all the time)
void somefunction(void * Data)
{
short DateStart, DateEnd;
char * SalesRep, * Customer, * Master;
int Detail;
BOOL Group;
OrdjourParamInit(Data, DateStart, DateEnd, SalesRep, Customer, Master, Detail, Group);
// ...
// And then SalesRep causes an Access Violation (wtf)?
memcpy(Socket->sData+ip,SalesRep,strlen(SalesRep)+1);
// ...
}
void OrdjourParamInit(void * Data, short &DateStart,
short &DateEnd, char * SalesRep,
char * Customer, char * Master, int &Detail,
BOOL &Group)
{
DateStart=*(short *)frmInterview->GetPrompt(ORDJOUR_FROM)->Data;
DateEnd=*(short *)frmInterview->GetPrompt(ORDJOUR_TO)->Data;
SalesRep=frmInterview->GetPrompt(ORDJOUR_REP_CODE)->Data;
Customer=frmInterview->GetPrompt(ORDJOUR_CUSTOMER_CODE)->Data;
Master=frmInterview->GetPrompt(ORDJOUR_MASTER_CUSTOMER_CODE)->Data;
Detail=frmInterview->GetCombo(ORDJOUR_DETAIL)->GetSelection();
Group=frmInterview->GetCheckBox(ORDJOUR_GROUP_REP)->GetValue();
}
Is there any way to do this other than declaring everything as global variables?
Thanks you,
leeand00
Jul 3, 2008 at 9:23pm UTC
The same way you do in java. Use classes.
Jul 3, 2008 at 10:26pm UTC
or use pointers to pointers instead .
Jul 3, 2008 at 11:09pm UTC
What your doing looks fine to me.
SalesRep=frmInterview->GetPrompt(ORDJOUR_REP_CODE)->Data;
You have to make sure this is actually returning a pointer to data that isn't going to be cleared elsewhere.
memcpy(Socket->sData+ip,SalesRep,strlen(SalesRep)+1);
You shouldn't be using the +1, because there isn't a +1 character in SalesRep, not unless you put it there urself. Does sData+ip have the correct amount of memory allocated etc.
Jul 6, 2008 at 6:43pm UTC
You are indeed correct gulkan. I didn't think this one through properly :D
The pointer is passed by value, so a copy is made =\ Hence the pointer to pointer.
Last edited on Jul 6, 2008 at 7:52pm UTC