Read access violation

kaveh rahimi 61 Reputation points
2021-09-25T14:25:59.523+00:00

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++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,626 questions
{count} votes

3 answers

Sort by: Most helpful
  1. WayneAKing 4,921 Reputation points
    2021-09-25T19:49:44.813+00:00

    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
    0 comments No comments

  2. RLWA32 42,876 Reputation points
    2021-09-26T16:28:42.347+00:00

    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.


  3. WayneAKing 4,921 Reputation points
    2021-09-26T23:51:43.797+00:00

    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
    0 comments No comments