cplusplus.com cplusplus.com
cplusplus.com   C++ : Forums : General C++ Programming : Access Violation when Initializing varia...
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Forums
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Articles
Lounge
Jobs

-

post  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):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void somefunction(void * Data)
{	
	short DateStart, DateEnd;	
	char * SalesRep, * Customer, * Master;	
	int Detail;	
	BOOL Group;	
	OrdjourParamInit(........, &SalesRep, &Customer, &Master,.....);

	memcpy(Socket->sData+ip,SalesRep,strlen(SalesRep)+1);         


void OrdjourParamInit(...... char **SalesRep, char **Customer, char **Master,.....)
{
	//.... other code 
	
	*SalesRep=frmInterview->GetPrompt(ORDJOUR_REP_CODE)->Data;
	*Customer=frmInterview->GetPrompt(ORDJOUR_CUSTOMER_CODE)->Data;
	*Master=frmInterview->GetPrompt(ORDJOUR_MASTER_CUSTOMER_CODE)->Data;

	//... other code
}


(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.

| Last edited on

This topic is archived - New replies not allowed.
Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us