10

For the past 2 days I've been stuck on a violation which I can't seem to get to go away. I've used break points and located where the error is, but I'm just hoping one of you will know what the issue is without me having to copy+paste all my code -.-

I'm getting

First-chance exception at 0x1027cb1a (msvcr100d.dll) in Escape.exe: 0xC0000005: Access violation writing location 0xcccccccc. Unhandled exception at 0x1027cb1a (msvcr100d.dll) in Escape.exe: 0xC0000005: Access violation writing location 0xcccccccc.

Now, a quick google search makes me think there's something peculiar going on. All the search results talk about pointers not actually pointing anywhere (0xccccccccc is a low memory address?).

I'm yet to use pointers in my code but either way I'll paste the function and point out the line the exception gets thrown (in bold):

void mMap::fillMap(){
    for(int i = 0; i <= 9; i++){
        for(int z = 0; z <= 19; z++){
            Tile t1;    // default Tile Type = "NULLTILE"
            myMap[i][z] = t1;
        }
    }
}

Now myMap is a 2d array of type Tile. I had this working a couple of days ago until I added some other classes and it all stopped working!

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Linky
  • 113
  • 1
  • 1
  • 4

4 Answers4

11

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.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • myMap has been declared as so in the mMap.h file: Tile myMap [10][20]; – Linky May 22 '11 at 03:26
  • @Linky: See my edit. This address is almost always caused by a pointer not being set, in your case `myMap` isn't a pointer, so it must be the pointer to the class containing `myMap` that's bad. – Ben Voigt May 22 '11 at 03:33
  • @Ben Voigt, thanks for your help. Although the array myMap of type Tile (Tile class) is declared within the mMap.h file. – Linky May 22 '11 at 03:40
  • @Linky: I understand that. That's the case the second and third parts of my answer discuss. – Ben Voigt May 22 '11 at 03:41
  • I'm testing the classes with a driver class. If I put: mMap mappy; By calling the constructor of the mMap class, it calls fillMap() and that's where the error occurs. The myMap array is local, which is why a pointer-related error being thrown confuses me. – Linky May 22 '11 at 03:45
  • @Linky: Is the constructor the only function that calls `fillMap()`? Use the call stack window to find out for sure what function called `fillMap()`. I'm positive you have some function calling `p->fillMap()` where `p` has never been set. The only way that `Tile myMap[10][20];` can be improperly allocated is if the entire `mMap` object containing it hasn't been allocated. – Ben Voigt May 22 '11 at 03:49
  • @Ben: this is what I don't understand, the code is so basic, just the building blocks. I'm yet to use a pointer. My driver class is doing VERY little and I assure you it's the constructor class calling fillMap() which somehow causes this error. – Linky May 22 '11 at 03:52
  • 0xcdcdcdcd is uninitialized memory when compiling debug. 0xcccccccc is uninitialized memory when /GZ is used. http://stackoverflow.com/questions/370195/when-and-why-will-an-os-initialise-memory-to-0xcd-0xdd-etc-on-malloc-free-new – sean e May 22 '11 at 04:14
  • So I'm assuming it's trying to tell me my myMap[10][20] array is uninitialized. But I declared it in my header file, DO NOT UNDERSTAND!! Thanks all for the help btw :) – Linky May 22 '11 at 04:17
  • @Linky: Being declared and being initialized are 2 different things. – sean e May 22 '11 at 04:20
  • @Linky: `myMap[10][20]` is inside another object (`class mMap`). If the `mMap` instance is invalid, so is the `myMap` inside. You have `mMap* ptr;` somewhere which is pointing to `0xcccccccc`, because you forgot to point it to a valid address. – Ben Voigt May 22 '11 at 04:25
  • @Ben: hrmm... so it could be in my driver class? The mMap object, in which myMap resides is being declared like this: mMap mappy; Therefore pointing to the default constructor which then, in turn calls the fillMap() method. I dunno, I'm lost. I can't get it to work... there's no pointers, nothing seems wrong. Ghost in the shell. – Linky May 22 '11 at 05:12
4

On MSVC++ and in debug mode, the debugging memory allocator sets all returned memory to 0xcccccccc, as a way to find cases of undefined behavior. In all likelihood, you never initialized myMap , or some of the pointers inside of myMap. Check your initialization code for bugs.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
  • Tile myMap [10][20]; show me how to properly initialise please. – Linky May 22 '11 at 03:29
  • 2
    0xcccccccc happens when using /GZ. http://stackoverflow.com/questions/370195/when-and-why-will-an-os-initialise-memory-to-0xcd-0xdd-etc-on-malloc-free-new – sean e May 22 '11 at 04:14
1

Had similar error when I tried to fill string value in table element of my own-class type with for-loop. I declared 1000 elements in that table, so I've put something like that:

    for (int i = 0; i <= 1000; i++)
    {
        TAble[i].name = "Some Guy";
        TAble[i].age = 4;
    }

Unfortunately as with string it occurred that I'm perhaps insisting on fillinf element that doesn't exist witch is element number 1000 in table. I managed to solve this by changing loop header, deleting equal sign before 1000.

Try to see whether you're not trying to call something that doesnt exist.

Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
Konard
  • 11
  • 1
1

For all of the answers and comments happening in this question, here are good references about memory fills in Visual C++:

Community
  • 1
  • 1
sean e
  • 11,792
  • 3
  • 44
  • 56