Arrays, Pointers, and a new Class.

This is a discussion on Arrays, Pointers, and a new Class. within the C++ Programming forums, part of the General Programming Boards category; Mmmm, strange going-ons with my game. First of all, I have to make an array of a custom class called ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    37

    Arrays, Pointers, and a new Class.

    Mmmm, strange going-ons with my game.

    First of all, I have to make an array of a custom class called "Room" of a size that's inputted from a file. So, what I did, is I made a pointer of type Room, and then I later made an array of Rooms, and linked the pointer to the array (sorry, don't know the proper terminology).

    Problem is, the program compiles, but anytime I try and do anything with the array, like get a string from a spot, I get either 3 million+ for an integer, or gibberish text for a string, which ends with Windows2k saying my program's crashed and an error log has been created (wierd pointer-related problem).

    So, here's my code--if anyone knows the solution, please let me know.

    Code:
    :::Driver.cpp:::
    Room* roomListPtr;
    // rest is omitted
    
    ::loadMap()::
    int numRooms;
    //some omissions
    ifstream mapData("D:\\ashiq\\C++\\game\\data\\testMap.txt");
    mapData >> numRooms;
    Room roomList[numRooms+1];
    roomListPtr = roomList;
    // Data loading omitted
    
    ::movePlayer()::
    //omitted; but this line is there
                    player.setRoom(roomListPtr[curRoom].getNorth());
    I checked all my array sizes, verified my streams were created, did everything I could think of...it compiles....but outputting anything accessed from the roomListPtr causes crashness instantly.

    --Ashiq

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Post the real code. The stuff you posted obviously won't compile (You are trying to initialize an array with a non-constant value).

    You might also want to use std::vector instead of arrays and dynamic memory, it's easier to do.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Also, an array name acts like a pointer, so you don't have to create a pointer and assign it the address of the array--just use the array name, unless of course you need a base class pointer for polymorphic behavior.
    Last edited by 7stud; 05-23-2003 at 12:07 AM.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    37
    Ok, here's the real stuff.

    Pardon the files...it's 3 files...I was doing a lot of java, so I'm following that style.

    Cheers!

    --Ashiq
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    37
    Er.

    As for polymorphism and stuff, I'm sad to say I'm a newbie, so all that went over my head.

    Basically the problem is creating an array based on input from a file I get later...so, like, I can't dimension it, so I have to make a pointer and then later point it to an object. Maybe there are simpler ways?

    --Ashiq

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    polymorphism is basically mutliple inheritance correct?

    I cant remember what it was anymore -_-
    MSVC++~

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "As for polymorphism and stuff, I'm sad to say I'm a newbie, so all that went over my head."

    Then just cross that part of the sentence out. Here is what you are doing:

    int ints[3] = {1, 2, 3};
    int* pints = ints;

    cout<<pints[1];

    Well, why wouldn't you just do this:

    int ints[3]={1, 2, 3};
    cout<<ints[1];

    There doesn't appear to be a reason to declare the pointer to Room in your code. Just use the array name roomlist:

    player.setRoom(roomlist[curRoom].getNorth());

    polymorphism is basically mutliple inheritance correct?
    Polymorphism involves declaring a base class pointer, which you can assign any derived class object's address to. The base class pointer operates like it has an internal switch statement that checks to see what type of object it's pointing to determine which member function to execute(each object's member function has the same name).
    Last edited by 7stud; 05-23-2003 at 12:56 AM.

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    37
    Erm. I can't use RoomList. You see, RoomList is only declared in my loadMap function. The reason for this is that the size of the array depends on a number I extract from a file.

    I don't know how to use vectors. So what I did is I declared the pointer, then once I knew the size of the array I created it and pointed to it. Since the pointer is globally accessible, I can access the list right?

    Maybe the problem is that I pointed the pointer at a variable that does not exist out of the scope of that function...is that why it crashes, possibly?

    And, can someone please provide a link to some vector tutorials please? Thanks.

    --Ashiq

  9. #9
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    First of all, I am genuinely suprised this compiled.

    Room roomList[numRooms+1];

    Your problem is that you have a dangling pointer. After the loadMap function finishes, the space allocated on the stack for roomList is gone. The roomListPtr still points to the location on the stack where roomList was, however, and that is your problem. When you are trying to access roomList from outside of loadMap, you are getting into memory that shouldn't be allocated there anymore.

    This is decent information on vector, maybe enough to get you started using it.

    http://www.msoe.edu/eecs/ce/courseinfo/stl/vector.htm

    One possible way to fix this is to use a global std::vector<Room> that you add things to in loadMap. I'd recommend trying to limit the scope of the roomlist however. Creating it in main and passing it around to different function is a cleaner solution.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "You see, RoomList is only declared in my loadMap function."

    Then it ceases to exist when your loadMap function ends, and any pointer pointing to the array fails. Try this code:
    Code:
    #include <iostream>
    using namespace std;
    
    void func(int* pint)
    {
    	int ints[3]={1, 2, 3};
    	pint = ints;
    	
    	for(int i=0; i<3; i++)
    		cout<<pint[i];
    	cout<<endl;
    }
    
    int main () 
    {
    	int* pint=0;
    	func(pint);
    	
    	cout<<pint[0]<<endl;
    		
    	return 0;
    }
    Get ahead of the curve and use deques instead of vectors:

    http://www.gotw.ca/publications/mill10.htm
    Last edited by 7stud; 05-23-2003 at 01:40 AM.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21