Access Violation Writing Location 0Xcccccccc

Access violation writing location 0xcccccccc

Either an uninitialized pointer, or a pointer stored in memory that's been freed. I think cccccccc is the first and cdcdcdcd is the second, but it varies with compiler/library implementation.

For your particular code, probably myMap hasn't been allocated yet, then myMap[0][0] would result in an access attempt to 0xcccccccc.


It can also happen that myMap is the beginning of your class, and the class pointer was uninitialized:

class mMap
{
Tile myMap[10][20];
public:
void f() { myMap[0][0] = 0; }
};

mMap* what;
what->f(); // what is an invalid pointer

This happens because the member function is not virtual, so the compiler knows what code to run and passes the object pointer as a hidden parameter. Eventually the compiler emits a calculation like:

this + offsetof(Whatever::myMap) + z * sizeof(myMap[0]) + i * sizeof(myMap[0][0])

this, being uninitialized, is 0xcccccccc. Evidently the offsetof part is zero, and i and z are both zero the first time through your loop, so you get 0xcccccccc + 0 + 0 + 0 as the memory address.


To debug this, use the call stack and find the function that called fillMap. Then check in that function where the pointers used for member access (->) came from.

Access violation writing location 0xCCCCCCCC when trying to add strings to an array

In C++ and pretty much all other programming languages I know of, arrays use this thing called 0 indexing. That means the index of the elements starts counting from 0. The first element is indexed 0, second 1, and so on. If you have an array of size n the last element is indexed n - 1.

In your code, the array test has a size of three. The elements have index 0, 1, and 2. Your loop goes from 1 to 3, so the last time in the loop you tried to access the element past the end of the array, causing an access violation. You should instead loop from 0 to 2 like this:

for(int i = 0; i < 3; i++)

By the way, there are a lot of other problems in your code:

You never set the seed for rand(), so every time you run the program it will output the same thing.

You used rand() % 4 to generate random numbers, which will give you numbers from 0 to 3, therefore, ignoring the last word.

The last if statement makes no sense at all since you are checking if guess is equal to 3, which it will never be (it must be larger than 3 for the loop to break), and then you tried to output the whole array words, which does not work. You should use a loop to output each element of test not guess.

Code with all the changes:

#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
// set the seed for rand() using the current time
std::srand(std::time(nullptr));
std::string words[5] = {"Me", "You", "Everyone", "Mom", "Dad"}, test[3];
for(int i = 0; i < 3; i++)
{
test[i] = words[std::rand() % 5];
}
for(int i = 0; i < 3; i++)
{
std::cout << test[i] << " ";
}
std::cout << '\n';
}

I would recommend getting a good book.

You can merge the two loops without even storing the words guessed:

#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
// set the seed for rand() using the current time
std::srand(std::time(nullptr));
std::string words[5] = {"Me", "You", "Everyone", "Mom", "Dad"};
for(int i = 0; i < 3; i++)
{
std::cout << words[std::rand() % 5] << ' ';
}
std::cout << '\n';
}

Note that in general, using the std::rand() function is not recommended since it tends to result in low quality random numbers. It is best to use the newer random number generators in the standard library instead.

Access violation writing location 0xCCCCCCCC

This page has a good description and background of the various "magic values" you might encounter when dealing with stack and heap.

From the page:

If you are seeing the 0xcccccccc bit pattern it means that you are reading memory that is on the current thread stack that has not been initialised.

Given the code snippet you've posted so far, and what you've described about "fixing" it with another variable declared in the base class, it sounds like the base and derived objects might not be in agreement as to their memory layout. Are they in the same library or executable? Check your compilation flags and make sure they match.

One strategy is to reduce your problem down to the minimal set of steps to reproduce the problem. You can make a copy of your project and start removing fields and methods until it works, and see if that helps you isolate it further.

Access violation writing location 0xCCCCCCCC creating 2d array

The [] operator takes precedence over *. cppreference

So the line

*piTable[ii] = new int[iSizeY];

is equivalent to

*(piTable[ii]) = new int[iSizeY];

what you wanted to write is:

(*piTable)[ii] = new int[iSizeY];

0xC0000005: Access violation writing location 0xCCCCCCCC caused by trying to make safe empty chars

Product::Product() {
sku_[0] = '\0';
name_[0] = '\0'; // <---- writing via unitialized pointer
price_ = 0;
quantity_ = 0;
qtyNeeded_ = 0;

}

There's one possible source of your problems. You have defined a char pointer (a dynamic array or a C-string, that is) name_ in your class but you never allocate any memory for it in your constructor, before attempting to record a value via the pointer. Naturally, you get a write access violation.

Before assigning the value to char at index [0] you need to first allocate space for at least one element in your string, e.g. by doing name_ = new char [1]. Alternatively, you may choose to initialize the pointer itself to NULL (or nullptr) and use that to indicate that name_ has not yet been set.

0xC0000005: Access violation writing location 0xcccccccc

Seems you are doing one level of indirection too much.

float planeCoefA, planeCoefB, planeCoefC, planeCoefD;

call

calculatePlaneEQ (<...>, &planeCoefA, &planeCoefB, &planeCoefC, &planeCoefD);

and

void calculatePlaneEQ (<...>, float * myXnorm, float * myYnorm, float * myZnorm, float* myD)
{
...
*myXnorm = 12.1f;
...


Related Topics



Leave a reply



Submit