Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1003111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:55:47+00:00 2026-05-16T07:55:47+00:00

Im developing a game engine for iphone in C++ but im getting a crash

  • 0

Im developing a game engine for iphone in C++ but im getting a crash with my Matrix class. I hope you can see whats wrong because i stare my self blind on the crash.

/*
 * Copyright (c) 2010 Johnny Mast
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:

 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include "Matrix.h"

Matrix::Matrix() {
 /*
 ** if this constuctor has been used you should call
 ** setWidth and setHeight before using this class.
 ** use of this constructor is not reconmended and it 
 ** will producse rended code.
 */
 matrixwidth  = 0;
 matrixheight = 0;
}

Matrix::Matrix(int width, int height) {
 matrixwidth  = width;
 matrixheight = height;
 setupMatrix();
}

void Matrix::setWidth(int width) {
 matrixwidth = width;
 setupMatrix();
}

void Matrix::setHeight(int height) {
 matrixheight = height;
 setupMatrix();
}

void Matrix::mark(int x, int y, int value) {
 NSLog(@"Marking");
 //matrix[x][y] = value;
 NSLog(@"Marked");
}

int Matrix::get(int x, int y) {
 return matrix[x][y]; 
}

#pragma mark -
#pragma mark Private functions from here.

void Matrix::setupMatrix() {
 if (matrixwidth > 0 && matrixheight > 0) {
  for (int w = 0, h = 0; w < matrixwidth, h < matrixheight; w++, h++) {
   matrix[w][h] = 0;
  } 
 }
}

The crash is in setupMatrix().

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:55:48+00:00Added an answer on May 16, 2026 at 7:55 am

    You have one visible problem and one invisible problem. First the invisible one.

    The fact that you’re crashing in setupMatrix is almost certainly because you haven’t allocated enough (or any) space for the matrix array. We’ll need to see where that’s done to be certain but it’s a fairly safe bet.

    One possibility, if you’re using a dynamic array, is to create it in the constructors thus (after setting up width and height members):

    matrix = 0; // for default constructor.
    setupMatrix();
    

    Then, in, setupMatrix, create the actual matrix, something like (untested):

    if (matrix != 0) delete[] matrix;                // to delete current.
    
    if (matrixwidth * matrixheight == 0)
        matrix = 0;                                  // none desired.
    else
        matrix = new int[matrixwidth][matrixheight]; // make new.
    
    // Initialise matrix (see corrected code below)
    

    keeping in mind that you may want to copy the old data in some cases.


    The other problem is with your for loop in that function. It only sets up the diagonals to 0, matrix[0][0], matrix[1][1] and so on. It should be:

    for (int w = 0; w < matrixwidth; w++) {
        for (int h = 0; h < matrixheight; h++) {
            matrix[w][h] = 0;
         }
     }
    

    The other problem with the loop you had will be if you have non-square matrices. Since the value of the expression a,b is b, your loop:

    for (int w = 0, h = 0; w < matrixwidth, h < matrixheight; w++, h++) {
    

    will only exit when h >= matrixheight. If your matrix is higher than it is wide, you may well be running off the end of your array, even if it is allocated correctly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone recommend a good Java game engine for developing simple tile-based games? I'm
i'm developing a android game which uses libgdx and bullet physics engine. I can
Background: I'm developing a game for the iPhone/iPad using cocos2d-iphone. Our engine provides the
Hi i am developing game level editor.Currently I am using win32 with directX.But with
I'm developing what it's turning into a cross-platform 2D Game Engine, my initial platform
Some time in the near future, I will begin developing a game engine. One
I am currently developing a C# .net xna game engine. I have been trying
I'm working on developing a fairly robust 2D game engine as a base that
I'm currently working on a small iPhone game, and am porting the 3d engine
This may be the wrong approach, but as I dig deeper into developing my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.