Results 1 to 4 of 4

Thread: C++ - Global variable, pass by address, or pass by reference?

  1. #1
    Join Date
    Sep 2005
    Beans
    1,619
    Distro
    Xubuntu 14.04 Trusty Tahr

    C++ - Global variable, pass by address, or pass by reference?

    I've found a few bugs in my game as I've been working on it, and something is hammering into my mind that it's my camera class causing the corruption (and it may not be in any shape or form).

    Here's what I'm doing:

    I have a bunch of classes and a few select ones depend on my camera interface for things such as:
    Player
    Collision
    Enemy
    Objects
    Weapons
    etc.

    Now here's what I'm doing:
    Each of those classes have a pointer called:
    Camera* cam;

    Each of those functions when initialized take a pointer to camera via:
    OnInit(Camera* camera)

    Each of those classes assign cam the following way:
    cam = camera;

    Each of those classes are invoked via:

    Player* player = new Player();
    player->OnInit(&cam);
    Weapon* weapon = new Weapon();
    weapon->OnInit(&cam);

    etc.

    Is that the best way? Should I make it a global variable? Could it be something else causing memory corruption? Now when my game first loads, the map is skewed. The last things I did were:
    Fixed bug where I was storing strings like so (from a file):
    Changed the following from:
    char tset[80];
    fscanf(map, "%s", &tset);

    To:
    char tset[80];
    fscanf(map, "%s", tset);

    I'm about to upload my entire project just to seek help finding these issues, but it's very very undocumented code.
    Dell XP 8700 - Windows 10 64-bit Fast Ring - i7 4790 8GB 1GB 250GB SSD 1TB HDD
    HP Stream 13 - elementary Loki 64-bit
    HP Envy M6 - Windows 10 64-bit

  2. #2
    Join Date
    Sep 2005
    Beans
    1,619
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: C++ - Global variable, pass by address, or pass by reference?

    Code:
    void Map::LoadMap(SDL_Renderer* ren, std::string mapName)
    {
    	std::cout << mapName << std::endl;
    	if(strlen(mapName.c_str()) < 3)
    	{
    		std::cout << "ERROR LOADING " << mapName;
    		return;
    	}
    	int mX = 0;
    	int mY = 0;
    	char tset[20];
    	std::string tSetStr;
    	tiles.clear();
    	warp.clear();
    	FILE* fMap = fopen(mapName.c_str(), "r");
    	if(fMap == NULL)
    	{
    		std::cout << "ERROR LOADING MAP " << mapName.c_str();
    	}
    	fscanf(fMap, "%s\n", tset);
    	fscanf(fMap, "%d:%d\n", &mX, &mY); 
    It keeps crashing on that line in debug mode:
    fscanf(fMap, "%d:%d\n", &mX, &mY);

    Why though?

    EDIT:
    Hmm... so I did:
    int mX = int(malloc(sizeof(int)));
    int mY = int(malloc(sizeof(int)));

    and it doesn't crash in debug now...

    EDIT2:
    Well it does not, an issue with one of my vectors... gah!!! Lol I'm pulling my hair ou.
    Last edited by slooksterpsv; February 27th, 2014 at 08:23 AM.
    Dell XP 8700 - Windows 10 64-bit Fast Ring - i7 4790 8GB 1GB 250GB SSD 1TB HDD
    HP Stream 13 - elementary Loki 64-bit
    HP Envy M6 - Windows 10 64-bit

  3. #3

    Re: C++ - Global variable, pass by address, or pass by reference?

    Just a guess, but using fscanf in that way is unsafe. Make sure you don't overwrite any buffers like that or by using gets(), carelessly with scanf() or any of the other functions that may walk off the end of a buffer.

    Code:
    fscanf(fMap, "%19s\n", tset)
    I won't offer help debugging because (as I mentioned in your other thread) I'm not familiar with C++, but if you can't nail down your mistake, you might want to post your entire program (perhaps using a pastebin-like site) and the misbehaving input files. The program is almost certainly corrupting memory somewhere, and that means the actual bug could be far, far away from the code that crashes.

    Also, I'm not sure what you were thinking this does --
    Code:
    int mX = int(malloc(sizeof(int)));
    -- but it probably doesn't do that. (In particular, it doesn't make mX dynamically allocated.)

  4. #4
    Join Date
    Sep 2005
    Beans
    1,619
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: C++ - Global variable, pass by address, or pass by reference?

    I changed the code instead of reading it in directly, using specific lengths and standards to copy it to variables.
    Dell XP 8700 - Windows 10 64-bit Fast Ring - i7 4790 8GB 1GB 250GB SSD 1TB HDD
    HP Stream 13 - elementary Loki 64-bit
    HP Envy M6 - Windows 10 64-bit

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •