Access Violation when Initializing variables in a function
| leeand00 (13) | |||
| 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 | |||
| Jeff HS (71) | |||
| The same way you do in java. Use classes. | |||
| guestgulkan (231) | |||
| or use pointers to pointers instead . | |||
| Zaita (1138) | |||
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. | |||
| guestgulkan (231) | |||
Sorry Zaita - I can't let this one go (at least until proven wrong). I think it should look like this (I'm only interested in the char* pointers):
(This also assumes/hopes that the frmInterview is returning valid char pointers) | |||
| Zaita (1138) | |||
| 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. | |||
This topic is archived - New replies not allowed.
