question

kavehrahimi-5744 avatar image
0 Votes"
kavehrahimi-5744 asked WayneAKing-0228 answered

Read access violation

Hi , I run my cpp code and receive run-time error:
'read access violation'
I've a variable oByte with the type PUCHAR I want assign this variable to another called TEMP that its type is UCHAR .
The oByte is the pointer which its value is the address of TEMP.
I have assigned oByte to TEMP by :
TEMP=*oByte;
I don't know this way of assigment is correct or not?
Please help.
Thanks

c++
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I noticed you've been most active question placer with returning no feedback past few days at least.
IIRC, you mentioned earlier you're noob with c++ so my suggestion for you is to start from basics by following guides like https://www.dummies.com/programming/cpp/

What comes to your earlier questions regarding link errors etc. ... understanding the basics might help you learn something.

0 Votes 0 ·

Well, if you were working with properly initialized variables then dereferencing oByte and assigning the result to TEMP wouldn't cause a read access violation.

Post the code so we can see what is actually happening.

0 Votes 0 ·

int main() {
PUCHAR TEMP;
PUCHAR curr;
mPCH341_NOTIFY_ROUTINE(iEventStatus);
ULONG iIndex = 0;
PCHAR *iDeviceID = 0;
UCHAR iDevice;
UCHAR iAddr;
UCHAR iByte;
PUCHAR *oByte=0;
PUCHAR *oByte1=0;
mPCH341_NOTIFY_ROUTINE iNotifyRoutine = 0;
CH341SetDeviceNotify(iIndex, *iDeviceID, iNotifyRoutine);
if (CH341SetDeviceNotify(iIndex, *iDeviceID, iNotifyRoutine) != 0) {
std::cout << "USB PORT CONNECTED";
}
else {
CH341OpenDevice(iIndex);
iDevice = 0x48;
iAddr = 0x90;
iByte = 0x02;
CH341WriteI2C(iIndex, iDevice, iAddr, iByte);
iByte = 0x90;
CH341WriteI2C(iIndex, iDevice, iAddr, iByte);
iByte = 0x04;
CH341WriteI2C(iIndex, iDevice, iAddr, iByte);
iAddr = 0x91;
CH341ReadI2C(iIndex, iDevice, iAddr, *oByte);
TEMP = *oByte;
CH341ReadI2C(iIndex, iDevice, iAddr, *oByte1);
curr = *oByte1;
}
return 0;
}

0 Votes 0 ·
WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered

I've a variable oByte with the type PUCHAR I want assign
this variable to another called TEMP that its type is UCHAR .
The oByte is the pointer which its value is the address of TEMP.
I have assigned oByte to TEMP by :
TEMP=*oByte;

Not related to any access violation, if your code does
exactly what you have described above then it serves no
useful purpose.

You say that "oByte is the pointer which its value is the
address of TEMP."
You then dereference that pointer and
assign the value to TEMP. It should be obvious that what
this does is simply assign TEMP to TEMP.

Example:

 UCHAR TEMP;
    
 PUCHAR oByte = &TEMP;
    
 TEMP = *oByte; // assigns TEMP to TEMP. Possible use
                 // of uninitialized variable TEMP
  • Wayne

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered RLWA32-6355 commented

The oByte pointer is initialized with 0 so it is a null pointer. At no time in your posted code is a valid address ever stored in this pointer. Therefore, dereferencing it will cause an access violation.

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Except 0 which initial value can I assign to oByte to fix my exception?

0 Votes 0 ·
RLWA32-6355 avatar image RLWA32-6355 kavehrahimi-5744 ·

Isn't it clear by now that oByte must contain the address of a UCHAR variable?

0 Votes 0 ·

Yes of course you are right.But the value of the address for me is unknown,and the format of it too.

0 Votes 0 ·
Show more comments
WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered

You seem to be struggling with the concept of pointers.

In your original post you said that TEMP was of type UCHAR.
But in the latest post you have TEMP as a PUCHAR which is a
pointer to UCHAR.

 PUCHAR TEMP; // same as unsigned char *TEMP;

You also said that "oByte is the pointer which its value is the
address of TEMP." But in your latest post you have

 PUCHAR *oByte = 0;

Which means that oByte is a pointer to a PUCHAR. So you now
have double indirection, where oByte is a pointer to a pointer
to an unsigned char.

To use code like that would require storing and dereferencing
two pointers. For example:

 UCHAR iByte = 'Z';
    
 PUCHAR p1 = &iByte;    
 PUCHAR *oByteX = &p1;
 unsigned char **oByteY = &p1; // equivalent
    
 printf("%c\n", *p1);
 printf("%c\n%c\n", **oByteX, **oByteY);

Output:

Z
Z
Z

You should brush up on pointers before continuing.

A TUTORIAL ON POINTERS AND ARRAYS IN C
by Ted Jensen
Version 1.2 (PDF Version)
https://pdos.csail.mit.edu/6.828/2014/readings/pointers.pdf

There are many, many other sources on the WEB. Review several
of them if you intend to continue using pointers in C (or C++
where their use is deprecated).

  • Wayne

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.