Thread: Access violation when reading a string.

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    903

    Access violation when reading a string.

    Hey guys ! I just started a little project on the A* algorithm and got an access violation. I can't see what's wrong with my code =/

    Here's the code:

    Code:
    std::string Map[] = {  
    				 "cccccccccccccccc"
    				 "cooooooooooooooc",
    				 "cooooooocooooooc",
    				 "cooooooocooooooc",
    				 "coooosoocooooooc",
    				 "cooooooocooooooc",
    				 "cooooooooooceooc",
    				 "cooooooooooooooc",
    				 "cccccccccccccccc" };
    
    struct Move
    {
    	Move() : f(0), g(0), h(0), x(0), y(0), parent(0) { };
    	bool operator < (const Move& m) { return (f < m.f); }
    	bool operator == (const Move& m) { return (x == m.x) && (y == m.y); }
    
    	int f, g, h, x, y;
    	Move* parent;
    };
    
    std::vector<Move> Open, Closed;
    
    void FindPoint(Move* m, char c)
    {
    	for(int i = 1; i < Map[0].size() - 1; i++)
    	{
    		for(int j = 1; j < (9 - 1); j++)
    		{
    			if(Map[i][j] == c)
    			{
    				m->x = i;
    				m->y = j;
    				break;
    			}
    		}
    	}
    }
    Where:

    c = 's', i = 12, j = 1

    This piece of code basically searches for the 's' character and changes the struct if it is found. Does anyone have an idea why this access violation happens ?

    Thanks.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    And what is Move* m?

    Your loops are somewhat puzzling too.
    What is that meant to mean?
    Code:
    	for(int j = 1; j < (9 - 1); j++)
    By the way, std::string has a find method to look for a character.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I assume you're actually allocating space for 'm'?

    You should define a copy constructor/assignment operator for your struct if you want to use it in a standard container.

    Other than that, I don't see anything here that (by itself) would cause an access violation
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    Ahah, that "9 - 1" thing was like a note to myself. The map is 9 character high but the last line is inaccessible so instead of writing 8 I wrote 9 - 1 to make it obvious though I think I'd be better off just commenting it.

    m is a pointer to an empty object passed to the function. Nothing flashy here. I'll try to use the find function, I didn't think about it.

    Code:
    int main()
    {
    	Move start;
    	FindPoint(&start, 's');
    
    	// ...
    }
    Last edited by Desolation; 04-29-2007 at 06:07 PM. Reason: Added code.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    I added the copy constructor and assignment operator but the violation still won't go away =/ Meanwhile, I tried using std::find() but it returns an iterator to the element found and not an index to it. I'm not aware if substracting iterators is allowed though. It could lead to a trick like this:

    Code:
    std::string::iterator it = std::find(Map[i].begin(), Map[i].end(), c);
    int index = it - Map[i].begin();

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I take back what I said, you shouldn't really need to explicitly define an assignment operator or copy constructor here, sorry.

    But I see the problem:
    Code:
    std::string Map[] = {  
    				 "cccccccccccccccc"
    				 "cooooooooooooooc",
    //...
    You're missing a comma
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    Ah ah, thanks for that ! I get the same violation access error though. Same values for c, i and j as well =/

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    JaWiB, I don't think those commas belong there.

    I think it should be;

    Code:
     string blah="this and that"
                        "and this too"
                        "and then this";
    Above, looks like the OP's commas are trapped between quotes -

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by JaWiB View Post
    You're missing a comma
    Don't you just love preprocessor string concatenation?

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    903
    I got the correct answer on another forum. I got i and j backwards. It had to be Map[j][i].

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    903
    Still on the same project, I get another access violation (definitely not my day =P). I was just trying to remember something. I think I've read somewhere that pointing to an object into an STL container is 'evil' because those objects aren't guaranteed not to be moved therefore pointing to an object in an STL container could lead do 'crazy pointers' as I've heard them being called. Are there high chances that this could be my problem ? I don't really want to post code for that because it's a bit long and really messy but if I have to, I'll probably do so tomorrow in the evening.

    Thanks to all.

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Desolation View Post
    Still on the same project, I get another access violation (definitely not my day =P). I was just trying to remember something. I think I've read somewhere that pointing to an object into an STL container is 'evil' because those objects aren't guaranteed not to be moved therefore pointing to an object in an STL container could lead do 'crazy pointers' as I've heard them being called. Are there high chances that this could be my problem ? I don't really want to post code for that because it's a bit long and really messy but if I have to, I'll probably do so tomorrow in the evening.

    Thanks to all.
    You can point to an item within a container as long as you don't modify the contents of the container during the lifetime of that pointer. If you insert, delete, or change an element of the container, all pointers pointing to objects within the container become invalid.

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    903
    That would explain the problem. Do you know of an alternative ?

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It depends on the container. You should be able to use pointers if you are using a list (unless of course you erase the object pointed to). For a vector, saving the index is one option, but sometimes indexes change. For a map, you should be able to find your object by the key regardless.

    Another option is to store pointers in the container. This adds complexity because something has to manage the lifetime of the objects. For example, if the container you are talking about is vector<Move>, you could change it to boost::ptr_vector<Move> or vector<std::tr1::shared_ptr<Move> >. You could also use vector<Move*>, but manage the memory yourself. This is most easily done if the Move objects only exist inside the container. You would need to wrap the inserting and erasing of the objects so that you can new and delete them. You also would want to wrap the destruction of the vector (or clear()'ing of it) so you can properly delete its contents.

    Doing that will allow you to continue to store pointers because the pointers will not change if the data in the container changes.

  15. #15
    Registered User
    Join Date
    May 2006
    Posts
    903
    You know, I'd really love to install boost::ptr_vector<> thing but I'm not nerd enough. I can't understand a damn thing of command compilers and it seems wayyy too complicated and long to do.

    http://www.boost.org/more/getting_started.html

    Here's what I'm talking about.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. IRC, reading the stream
    By Iyouboushi in forum C# Programming
    Replies: 6
    Last Post: 08-03-2006, 05:34 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. reading file weird access violation
    By p3p in forum C++ Programming
    Replies: 6
    Last Post: 09-03-2005, 08:06 AM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM