Skip to content

matrix4x4 operator[] is broken #685

Closed

Description

@mintar

The operator[] of matrix4x4 gives me nonsense values, probably from uninitialized memory. According to the comment in matrix4x4.inl:243, that's a known problem.

Code snippet that reproduces the problem:

aiMatrix4x4 transform = node->mTransformation;

std::cout <<  "  BROKEN: " << std::endl
          << std::fixed
          << "[ " << *transform[0]  << " \t" << *transform[1]  << " \t" << *transform[2]  << " \t" << *transform[3]  << std::endl
          << "  " << *transform[4]  << " \t" << *transform[5]  << " \t" << *transform[6]  << " \t" << *transform[7]  << std::endl
          << "  " << *transform[8]  << " \t" << *transform[9]  << " \t" << *transform[10] << " \t" << *transform[11] << std::endl
          << "  " << *transform[12] << " \t" << *transform[13] << " \t" << *transform[14] << " \t" << *transform[15] << " ]" << std::endl<std::endl;

std::cout << "  WORKING:" << std::endl
          << std::fixed
          << "[ " << transform.a1 << " \t" << transform.a2 << " \t" << transform.a3 << " \t" << transform.a4 << std::endl
          << "  " << transform.b1 << " \t" << transform.b2 << " \t" << transform.b3 << " \t" << transform.b4 << std::endl
          << "  " << transform.c1 << " \t" << transform.c2 << " \t" << transform.c3 << " \t" << transform.c4 << std::endl
          << "  " << transform.d1 << " \t" << transform.d2 << " \t" << transform.d3 << " \t" << transform.d4 << " ]" << std::endl;

Output:

  BROKEN:
[ 1.000000  0.000000    0.000000    0.000000
  -6383549033750467933401407029248.000000   -6383573212266860225984901152768.000000     -322887.250000  -322887.281250
  -322889.093750    -6444387016696754531989320630272.000000     -322887.250000  0.000000
  0.000000  0.000000    0.000000    0.000000 ]

  WORKING:
[ 1.000000  0.000000    0.000000    0.000000
  0.000000  1.000000    0.000000    0.000000
  0.000000  0.000000    1.000000    0.000000
  0.000000  0.000000    0.000000    1.000000 ]

Activity

  1. mintar commented on Nov 6, 2015

    @mintar
    Author

    Some system information:

    distribution:     Ubuntu 14.04.3 LTS
    assimp version:   3.0~dfsg-2 (from official Ubuntu repos)
    gcc:              4.8.4
    CMAKE_BUILD_TYPE: Debug
    kernel:           3.16.0-52-generic (x86_64)
    
  2. kimkulling commented on Nov 6, 2015

    @kimkulling
    Member

    Thanks a lot for the report, I will fix that issue!

  3. added
    Bug
    on Nov 6, 2015
  4. kimkulling commented on Nov 7, 2015

    @kimkulling
    Member

    Checked this: the []-operator will return a pointer showing to the first entry of the row of the matrix. I added a test to show this.
    You can use it as following:

    TEST_F( utMatrix4x4Test, indexOperatorTest ) {
        aiMatrix4x4 m;
        float *a0 = m[ 0 ];
        EXPECT_FLOAT_EQ( 1.0f, *a0 );
        float *a1 = a0+1;
        EXPECT_FLOAT_EQ( 0.0f, *a1 );
        float *a2 = a0 + 2;
        EXPECT_FLOAT_EQ( 0.0f, *a2 );
        float *a3 = a0 + 3;
        EXPECT_FLOAT_EQ( 0.0f, *a3 );
    
        float *a4 = m[ 1 ];
        EXPECT_FLOAT_EQ( 0.0f, *a4 );
        float *a5 = a4 + 1;
        EXPECT_FLOAT_EQ( 1.0f, *a5 );
        float *a6 = a4 + 2;
        EXPECT_FLOAT_EQ( 0.0f, *a6 );
        float *a7 = a4 + 3;
        EXPECT_FLOAT_EQ( 0.0f, *a7 );
    
        float *a8 = m[ 2 ];
        EXPECT_FLOAT_EQ( 0.0f, *a8 );
        float *a9 = a8 + 1;
        EXPECT_FLOAT_EQ( 0.0f, *a9 );
        float *a10 = a8 + 2;
        EXPECT_FLOAT_EQ( 1.0f, *a10 );
        float *a11 = a8 + 3;
        EXPECT_FLOAT_EQ( 0.0f, *a11 );
    
        float *a12 = m[ 3 ];
        EXPECT_FLOAT_EQ( 0.0f, *a12 );
        float *a13 = a12 + 1;
        EXPECT_FLOAT_EQ( 0.0f, *a13 );
        float *a14 = a12 + 2;
        EXPECT_FLOAT_EQ( 0.0f, *a14 );
        float *a15 = a12 + 3;
        EXPECT_FLOAT_EQ( 1.0f, *a15 );
    }
    
  5. added a commit that references this issue on Nov 7, 2015
  6. added a commit that references this issue on Nov 9, 2015
  7. mintar commented on Nov 10, 2015

    @mintar
    Author

    Ah, so the bug was on my side. Thanks for checking!

  8. kimkulling commented on Nov 10, 2015

    @kimkulling
    Member

    You will get a NULL in case of a misuse.

  9. added a commit that references this issue on Dec 11, 2023
  10. added a commit that references this issue on Feb 5, 2024
  11. added a commit that references this issue on Jul 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions