C++

Error: Access Violation Writing Location C++

Whenever you are dealing with codes written in any programming language, there is a high chance that you encounter different types of errors. Similarly, while working with C++, you come across certain types of errors that seem quite challenging to resolve. However, when you look closely at your coding practices, you realize that these errors are nothing but a consequence of your careless mistakes.

One such error is the access violation writing location error in C++ and this article is dedicated to the discussion of this error. More precisely, we will discuss why this error occurs in the first place and then we will throw light on the different methods using which we can easily get rid of this error in C++ in Ubuntu 20.04.

What is Error Access Violation Writing Location in C++ in Ubuntu 20.04?

Before heading on to the occurrence of this error, we first required to recognize what this error really is. As its name says, this error occurs whenever you try to access a location that you are not allowed to access in the first place. In other words, whenever you will try to violate the norms of accessing a writing location set up by the C++ programming language, you will always come across this error. Now, the next question arises that which particular programming practice can give rise to this error.

Well, the simplest answer is that when you do not understand the actual usage of different entities of a programming language, then such errors are highly likely to occur. For example, you are unaware of the usage of pointers and objects of a class. For accessing the member functions of a class in C++, all you need is an object of that class. However, in some cases, you might require a pointer of that class. In that case, what you need to understand is that you have to initialize that pointer before trying to access anything with it. Failing to do so will result in the production of the error under discussion. However, apart from this, there can be some other situations too that can give rise to this error.

Example of the Occurrence of the Access Violation Writing Location Error in C++ in Ubuntu 20.04

For explaining to you the scenario that can possibly lead to the occurrence of the access violation writing location error in C++, we have coded the following example:

In this small sample C++ code, we have a class named “Test”. Within this class, we only have a single public member function named “myFunc()” whose return type is “void” i.e., this function will not return anything. Within this function, we have simply printed a message on the terminal. Then, we have our “main()” function in which we have first created a pointer of the class “Test”. After that, we have tried to access the “myFunc()” function of the “Test” class with the pointer of this class by using the “.” operator. Then, we have just used the “return 0” statement to close our code.

We used the command shown below to compile this code snippet:

$ g++ Error.cpp –o Error

As soon as we attempted to compile this C++ code, the error shown in the following image was produced on the terminal:

In other words, the error shown above is also known as the access violation writing location error in C++. It means that we have tried to access a location that we were not permitted to access. This error occurred in this case because we did not create any instance of the “Test” class with which we can access its member functions. Rather, we simply created a pointer of the “Test” type. This pointer was not pointing to any particular location because of which it contained a garbage address. That is why, when we tried to access the member function of the “Test” class while using this pointer, the error was produced on the terminal since this address did not contain the reference to a valid object of the “Test” class with which we can access its member functions.

How to Fix the Error Discussed Above?

There are two different ways of fixing the access violation writing location error in C++ that we have just discussed above. These two methods have been discussed in detail, affixed below:

Fix 1: By Dynamic Memory Allocation in C++
If you intend to create a pointer of the target class for accessing its member functions, then you can make use of this method. The fix for this particular method in the form of a C++ code is shown in the following image:

In this fix, the basic code for our sample class is the same, however, we have made a few changes in our “main()” function. First, we have created a pointer of the “Test” type while making use of the “new” keyword. Doing this, we are essentially initializing the “Test” type pointer through dynamic memory allocation i.e., we are allocating this memory on the heap. Then, with the help of this newly initialized pointer, we have tried to access the member function of the “Test” class while making use of the “->” operator in C++.

After making this change to our code, it got compiled successfully and when we executed this compiled code, we got the desired output on the terminal as shown in the image below:

Fix 2: By Creating a Valid Object of the Target Class in C++
Now, if you do not want to perform the dynamic memory allocation, or in other words, you do not want to deal with the pointers, then you can make use of this method. This method creates a valid object of the target class in C++ for accessing its member functions and it is considered a relatively easier method of achieving this goal. The fix for this particular method in the form of a C++ code is shown in the following image:

Again, in this fix, the basic code for our sample class is the same, however, we have made a few changes in our “main()” function. First, we have created an object or instance of the “Test” class. This instance or object is created on the stack and not on the heap. Then, with the help of this newly created object, we have tried to access the member function of the “Test” class while making use of the “.” operator in C++.

After making this change to our code, it got compiled successfully and when we executed this compiled code, we got the desired output on the terminal as shown in the image below:

Conclusion

This tutorial gave a nice explanation of the access violation writing location error in C++ in Ubuntu 20.04. For achieving this objective, we first explained to you the meaning of this error followed by the programming practices that can lead to this error. After that, we shared with you an example scenario that can give rise to this error along with the methods through which you can easily fix it. After going through this article, you will be capable to avoid this error while creating your programs in C++ in Ubuntu 20.04.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.