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 7804325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:52:40+00:00 2026-06-02T01:52:40+00:00

I have a C++ class Matrix22 with an array and a default constructor: class

  • 0

I have a C++ class Matrix22 with an array and a default constructor:

class Matrix22{
  /* something more */
  double mat[2][2];
  Matrix22(){
    for(int i=0; i<2; i++)
      for(int j=0; j<2; j++)
        mat[i][j] = i==j ? 1.0 : 0.0;
  }
};

I used it in my program and got a segmentation fault. As the rest was quite difficult and complicated I wrote a simple test routine, that just calls Matrix22(). No more seg fault.

I then ran gdb to debug the problem. If I call the constructor from the separate test routine, gcc reserves some memory for the member mat. I can navigate through the stack and see the return address some bytes after the array.

In the main program the compiler does not reserve enough space. The first element (mat[0][0]) gets written but any futher write just overwrites the next stack frame. I can also verify that as before the constructor the command btreturns a correct backtrace, where after the critical assignment the backtrace is corrupted.

So my question is: Why does in one case the compiler (or the linker?) reserve not enough space for the array, while in the other case that is not happening?

PS: Both “test cases” are compiled with the same compiler and flags and alsolinked against the same object files.

edit:

Here is the “simple” test case that works without seg fault:

void test_Matrix22()
{
  Framework::Math::Matrix22 matrix;
}

The code with creates a seg fault is in the class ModuleShaddower (intermixed header and implementation):

class ModuleShaddower{
    public:
        ModuleShaddower(PVModule& module, const EnvironmentalSetup& setup, const Position& position);
    private:
        Matrix22 rotMatrix90;
};

ModuleShaddower::ModuleShaddower(PVModule& module, const EnvironmentalSetup& setup, const Position& position)
  : module (module), position(position), setup(setup), logger(LoggerFactory::getLoggerInstance())
{
    double mat[][2] = {{0, -1},{1, 0}}; // This line will never be reached
    rotMatrix90 = Matrix22(mat);
}

As you see, it is quite from within the rest. I will maybe try to extract the problematic code but I think this won’t help much.

  • 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-06-02T01:52:42+00:00Added an answer on June 2, 2026 at 1:52 am

    The problem was due to the fact that two object files in different locations had the same name. In the resulting static library, that was created from that object code, sometimes the wrong file gets replaced (both were called Shaddower.o). As I renamed one of the files all went well and no more errors.

    I do not know the exact origin of this problem but it is solvable like that.

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

Sidebar

Related Questions

I have: class first{ private: int *array; public: first(int x){ array = new int[x][10];
I have class Fred { public: void inspect() const {}; void modify(){}; }; int
I have: class Foo { int a; int b; std::string s; char d; };
I have class User that contains protected constructor and class Account that has access
i have class point: public class Point { private double _x; private double _y;
I have class SpeexRunner as follows, The constructor of takes two arguments a boolean
I have class that holds Tree Items for a Tree. Say the constructor looks
I have class that is a DataContract and the member values are holding default
Let's say I have interface IMatrix { double this[int r, int c] { get;
I have class with a member function that takes a default argument. struct Class

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.