1,105,578 Community Members

Memory Violation Error

Member Avatar
Doogledude123
Junior Poster
160 posts since Jun 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 0 [?]
 
0
 

Im reading a text file and storing the characters in an array. I get a "Unhandled exception at 0x0109985e in Executable.exe: 0xC0000005: Access violation writing location 0xcccccccc."

void drawMap(string MapLoc) {
    int i = 0;
    int x = 0;
    int y = 0;
    char* arrayMap[5225];
    ifstream mapReader;
    mapReader.open(MapLoc);

    for (i = 0; i <= 5225; i++) 
    {
        while (mapReader.good())
        {
*           mapReader >> arrayMap[i];
        }
    }
    mapReader.close();

    for (i = 0; i = 5225; i--)
    {
        if ((i = 96) || (i = 191) || (i = 286) || (i = 381) || (i = 476) || (i = 571) || (i = 666) || (i = 761) || (i = 856) || (i = 951) || (i = 1046) || (i = 1141)
            || (i = 1236) || (i = 1331) || (i = 1426) || (i = 1521) || (i = 1616) || (i = 1711) || (i = 1806) || (i = 1901) || (i = 1996) || (i = 2091) || (i = 2186) || (i = 2281)
            || (i = 2376) || (i = 2471) || (i = 2566) || (i = 2661) || (i = 2756) || (i = 2851) || (i = 2946) || (i = 3041) || (i = 3136) || (i = 3231) || (i = 3326) || (i = 3421)
            || (i = 3516) || (i = 3611) || (i = 3706) || (i = 3801) || (i = 3896) || (i = 3991) || (i = 4086) || (i = 4181) || (i = 4276) || (i = 4371) || (i = 4466) || (i = 4561)
            || (i = 4656) || (i = 4751) || (i = 4846) || (i = 4941) || (i = 5036) || (i = 5131))
        {
            y++;
        }
        if (arrayMap[i] == "X")
                {
                    //Draw Wall
                    glColor4ub(255,255,255,255);
                    glBegin(GL_QUADS);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glEnd();
                }
                else if (arrayMap[i] == ".")
                {
                    //Draw Space
                    glColor4ub(0,0,0,255);
                    glBegin(GL_QUADS);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glEnd();
                }
                else if (arrayMap[i] == "C")
                {
                    //Draw Char
                }
                else if (arrayMap[i] == "O")
                {
                    //Draw Opponent
                }
    }
}

Happens when executing the code at the *

mapReader >> arrayMap[i];
Member Avatar
CGSMCMLXXV
Junior Poster in Training
54 posts since Jan 2013
Reputation Points: 5 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
 
0
 

Except for the typo or whatever

for (i = 0; i = 5225; i--) // ???

try initializing the array with less than 1024 and see if you get the same problem.

(edit) At the second thought (I missed that part when I posted the previous message), you have loop in loop:

    for (i = 0; i <= 5225; i++)
    {
        while (mapReader.good())
        {
            mapReader >> arrayMap[i];
        }
    }

which should be one of:

    for (i = 0; i <= 5225; i++)
    {
        if (mapReader.good())
        {
            mapReader >> arrayMap[i];
        }
    }

or

    int i = 0;
    while (mapReader.good())
    {
        mapReader >> arrayMap[i];
        i++;
    }

That's because while loop traverses the file.

Member Avatar
Doogledude123
Junior Poster
160 posts since Jun 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 0 [?]
 
0
 

Even with the loop replaced and the array value lowered, it still gives an access violation error.

    *_Str++ = _Traits::to_char_type(_Meta); // add it to string

Thats where it breaks at in istream.

Member Avatar
Suzie999
Posting Pro in Training
464 posts since Jul 2010
Reputation Points: 78 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 3 [?]
 
0
 
char* arrayMap[5225];

So the max number you want to test or alter is 5224, arrays start at 0.

for (i = 0; i < 5225; i++)  // <===================
    {
        if (mapReader.good())
        {
            mapReader >> arrayMap[i];
        }
    }

You need to make sure your incrementer does not exceed that number.
Also, not knowing what mapReader does, it couls be that you are writing incorrect data to arrayMap, which is char** (a pointer to a pointer).

Member Avatar
Doogledude123
Junior Poster
160 posts since Jun 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 0 [?]
 
0
 

Im reading in characters from a file, which is read through from MapLoc. Only specific characters, and I know the file doesnt contain a value not known. (X, ., C, and O are the characters)

Then when theyre being read, the characters will individually go into an array 'arrayMap' where they are then read and processed accordingly here:

if (arrayMap[i] == "X")
                {
                    //Draw Wall
                    glColor4ub(255,255,255,255);
                    glBegin(GL_QUADS);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glEnd();
                }
                else if (arrayMap[i] == ".")
                {
                    //Draw Space
                    glColor4ub(0,0,0,255);
                    glBegin(GL_QUADS);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glEnd();
                }
                else if (arrayMap[i] == "C")
                {
                    //Draw Char
                }
                else if (arrayMap[i] == "O")
                {
                    //Draw Opponent
                }

The error happens when reading from the file, after opening.

Member Avatar
Suzie999
Posting Pro in Training
464 posts since Jul 2010
Reputation Points: 78 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 3 [?]
 
0
 

arrayMap[i] will never == "X" because arrayMap[i] is a pointer to char*

try declaring it like this char arrayMap[5225];

Member Avatar
Doogledude123
Junior Poster
160 posts since Jun 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 0 [?]
 
0
 

operand types are incompatible ("char" and "const char *")

error given at

if (arrayMap[i] == "X") // <---
                {
                    //Draw Wall
                    glColor4ub(255,255,255,255);
                    glBegin(GL_QUADS);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glEnd();
                }
                else if (arrayMap[i] == ".") // <---
                {
                    //Draw Space
                    glColor4ub(0,0,0,255);
                    glBegin(GL_QUADS);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glVertex2f(10*x,10*y);
                    glEnd();
                }
                else if (arrayMap[i] == "C") // <---
                {
                    //Draw Char
                }
                else if (arrayMap[i] == "O") // <---
                {
                    //Draw Opponent
                }
Member Avatar
deceptikon
Eternally Awesome
4,700 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 690 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

operand types are incompatible ("char" and "const char *")

You should use the standard C++ string class. C-style strings are awkward, low level, and easy to get wrong (as evidenced by this thread). However, let's look at the problems in your original code, and ignore all of the wrong advice you've been given up to this point.

This is wrong:

char* arrayMap[5225];
...
mapReader >> arrayMap[i];

Why is it wrong? Because arrayMap[i] doesn't point to memory that you own; you're extremely likely to get a memory access violation at runtime. Memory must be allocated first, but doing that would be awkward, low level, and easy to get wrong (as evidenced by this thread). Yes, I'm intentionally repeating myself.

This is wrong too:

if (arrayMap[i] == "X")

The equality operator doesn't work the way you think it does at a lower level. arrayMap[i] is a pointer, and "X" evaluates to a pointer. Unless the planets align and you get a perfect storm of shared string literals in the compiler implementation and arrayMap[i] was conveniently initialized to "X", this test will always fail.

To compare C-style strings you must compare all characters up to a null character ('\0'). The simplest way to do that is by calling the std::strcmp() function:

if (std::strcmp(arrayMap[i], "X") == 0)

Or, since these are single character tests, you can just look at the first character:

if (arrayMap[i][0] == 'X')

But you can greatly simplify things by using C++ strings instead. One simple change will fix all of your string handling problems in that function:

string arrayMap[5225];

Now that that's taken care of, you really need to remember the difference between the = operator and the == operator. The former is assignment and the latter is comparison. You're mixing and matching them, and it's going to bork up your code big time.

I'd probably make a few assumptions and fix your code like so:

void drawMap(string MapLoc)
{
    string arrayMap[5225];
    int x = 0, y = 0;
    int n = 0;

    {
        ifstream mapReader(MapLoc);

        while (n < 5225)
        {
            if (!(mapReader >> arrayMap[n++]))
                break;
        }
    }

    for (int i = 0; i < n; i++)
    {
        if ((i == 96) || (i == 191) || (i == 286) || (i == 381) || (i == 476) || (i == 571) || (i == 666) || (i == 761) || 
            (i == 856) || (i == 951) || (i == 1046) || (i == 1141) ||(i == 1236) || (i == 1331) || (i == 1426) || (i == 1521) || 
            (i == 1616) || (i == 1711) || (i == 1806) || (i == 1901) || (i == 1996) || (i == 2091) || (i == 2186) || (i == 2281) ||
            (i == 2376) || (i == 2471) || (i == 2566) || (i == 2661) || (i == 2756) || (i == 2851) || (i == 2946) || (i == 3041) || 
            (i == 3136) || (i == 3231) || (i == 3326) || (i == 3421) || (i == 3516) || (i == 3611) || (i == 3706) || (i == 3801) || 
            (i == 3896) || (i == 3991) || (i == 4086) || (i == 4181) || (i == 4276) || (i == 4371) || (i == 4466) || (i == 4561) ||
            (i == 4656) || (i == 4751) || (i == 4846) || (i == 4941) || (i == 5036) || (i == 5131))
        {
            y++;
        }

        if (arrayMap[i] == "X")
        {
            // Draw Wall
            glColor4ub(255, 255, 255, 255);
            glBegin(GL_QUADS);
            glVertex2f(10 * x, 10 * y);
            glVertex2f(10 * x, 10 * y);
            glVertex2f(10 * x, 10 * y);
            glVertex2f(10 * x, 10 * y);
            glEnd();
        }
        else if (arrayMap[i] == ".")
        {
            // Draw Space
            glColor4ub(0, 0, 0, 255);
            glBegin(GL_QUADS);
            glVertex2f(10 * x, 10 * y);
            glVertex2f(10 * x, 10 * y);
            glVertex2f(10 * x, 10 * y);
            glVertex2f(10 * x, 10 * y);
            glEnd();
        }
        else if (arrayMap[i] == "C")
        {
            // Draw Char
        }
        else if (arrayMap[i] == "O")
        {
            // Draw Opponent
        }
    }
}

One of the assumptions is that the algorithm in general is correct and what you want.

Member Avatar
Suzie999
Posting Pro in Training
464 posts since Jul 2010
Reputation Points: 78 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 3 [?]
 
0
 

operand types are incompatible ("char" and "const char *")

To test a char you need single quotes

if (arrayMap[i] == 'X')

Also, all the advice in this thread is not wrong, and there is nothing wrong with using c style strings if you wish to.

Member Avatar
deceptikon
Eternally Awesome
4,700 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 690 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

Also, all the advice in this thread is not wrong

Your most recent post is factually correct (but potentially problematic depending on the format of the file). My previous post is factually correct. Everything prior to that is either flat out wrong or incomplete to the point of being wrong. You can choose which one applies to each post.

If you can't see the wrongness, please consider that you might not be in the best position to help others, and don't be afraid to defer to those who know what they're talking about.

and there is nothing wrong with using c style strings if you wish to.

I never said it was wrong. I said it was awkward, low level, and easy to mess up. If you wish to make things harder on yourself by using a more advanced approach when you lack the experience to do it right, that's your own business. But it would be negligent of me to not mention clearly superior alternatives.

Member Avatar
Suzie999
Posting Pro in Training
464 posts since Jul 2010
Reputation Points: 78 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 3 [?]
 
0
 

I do not mean to offend you, perhaps you could just point out to me what is wrong with anything in any post I made.

Member Avatar
NathanOliver
Posting Virtuoso
1,658 posts since Apr 2009
Reputation Points: 284 [?]
Q&As Helped to Solve: 310 [?]
Skill Endorsements: 4 [?]
 
1
 

Hate to but in but to answer your question Suzie999 your first post neglected the fact that mapReader is a stream object and he is trying to put information from the stream into an uninitialized pointer array. Your second post wouldn’t work either since he is trying to read in an array of strings. Your suggestion is to put the file into one string which could cause problems and is not what the OP is trying to accomplish. I wouldn’t say that your advice was wrong but IMHO it wasn’t correct.

Member Avatar
Suzie999
Posting Pro in Training
464 posts since Jul 2010
Reputation Points: 78 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 3 [?]
 
0
 

Fair enough.
Thanks.

Member Avatar
Doogledude123
Junior Poster
160 posts since Jun 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 0 [?]
 
0
 

Im not sure, but I dont believe deceptikon's code is correct, I need to check if the current array index value is equal to one of numbers listed so that i can make it start drawing on the next line.

Aswell its not drawing, which means either its not reading the file correct, or its not reading array correct.

By the way, the array is 95x55=5225, im basically making an grid.

Member Avatar
Suzie999
Posting Pro in Training
464 posts since Jul 2010
Reputation Points: 78 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 3 [?]
 
0
 

So you want a two dimentional array of char, or are you using strings?

If you want an array of char like a grid, you should have it like so.

arrayMap[95][55];

here is example

char arrayMap[3][3];

    for (int y = 0; y < 3; y++){

        for (int x = 0; x < 3; x++){

            arrayMap[x][y] = 'z';
        }

    }

In your minds eye, you might view that array as so...

zzz
zzz
zzz

3 rows and 3 columns.

Member Avatar
Doogledude123
Junior Poster
160 posts since Jun 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 0 [?]
 
0
 

I know, I tried that at first although it didnt work.

void drawMap(std::string MapLoc) {
    char* gridMap[95] [55];
    ifstream Map(MapLoc, ifstream::in, ifstream::binary);
    while (Map.good())
    {
    for(int i = 0; i < 95; i++)
        {
        for(int j = 0; j < 55; j++)
            {
                if (gridMap[i] [j] == "X")
                {
                    //Draw Wall
                    glColor4ub(255,255,255,255);
                    glBegin(GL_QUADS);
                    glVertex2f(10*i,10*j);
                    glVertex2f(10*i,10*j);
                    glVertex2f(10*i,10*j);
                    glVertex2f(10*i,10*j);
                    glEnd();
                }
                else if (gridMap[i] [j] == ".")
                {
                    //Draw Space
                    glColor4ub(0,0,0,255);
                    glBegin(GL_QUADS);
                    glVertex2f(10*i,10*j);
                    glVertex2f(10*i,10*j);
                    glVertex2f(10*i,10*j);
                    glVertex2f(10*i,10*j);
                    glEnd();
                }
                else if (gridMap[i] [j] == "C")
                {
                    //Draw Char
                }
                else if (gridMap[i] [j] == "O")
                {
                    //Draw Opponent
                }
            }
        }
    }
    Map.close();
}

Thats basically what I had.

Member Avatar
Suzie999
Posting Pro in Training
464 posts since Jul 2010
Reputation Points: 78 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 3 [?]
 
0
 

Did you try gridMap[i] [j] == 'X' (single quotes) ?

Member Avatar
Doogledude123
Junior Poster
160 posts since Jun 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 0 [?]
 
0
 

I honestly just think were backtracking now. I just think we should stick with the code given and go from there. Unless you figure out all the code to get it working, then I dont believe we should be using another whole new set of code.

Member Avatar
Suzie999
Posting Pro in Training
464 posts since Jul 2010
Reputation Points: 78 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 3 [?]
 
0
 

I think we're not backtracking, it is a very simple question which you seem to not want to answer, "X" is not the same as 'X'.

Anyway, I'm out, good luck.

Member Avatar
Doogledude123
Junior Poster
160 posts since Jun 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 0 [?]
 
0
 

Sorry if it seems that way, I lost my code snippet from that, that contains the actuall reading of the file, and I cant seem to figure out how I did it before. Aswell, I believe it would be easier the way Im currently doing it. Please tell me if I am wrong.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: