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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:59:22+00:00 2026-06-12T10:59:22+00:00

I have a segmentation fault 11 error when I run the following code. The

  • 0

I have a “segmentation fault 11” error when I run the following code. The code actually compiles but I get the error at run time.

//** Terror.h **

#include <iostream>
#include <string>
#include <map>

using std::map;
using std::pair;
using std::string;

template<typename Tsize>
class Terror
{
    public:
    //Inserts a message in the map.
    static Tsize    insertMessage(const string& message)
    {
        mErrorMessages.insert( pair<Tsize, string>(mErrorMessages.size()+1, message) );
        return mErrorMessages.size();
    }

    private:
    static map<Tsize, string>       mErrorMessages;
};

template<typename Tsize>
map<Tsize,string> Terror<Tsize>::mErrorMessages;

//** error.h **

#include <iostream>
#include "Terror.h"

typedef unsigned short errorType;
typedef Terror<errorType>   error;
errorType memoryAllocationError=error::insertMessage("ERROR: out of memory.");

//** main.cpp **

#include <iostream>
#include "error.h"
using namespace std;

int main()
{   
    try
    {
        throw error(memoryAllocationError);
    }
    catch(error& err)
    {
    }
}

I have kind of debugging the code and the error happens when the message is being inserted in the static map member. An observation is that if I put the line:

errorType memoryAllocationError=error::insertMessage("ERROR: out of memory.");

inside the “main()” function instead of at global scope, then everything works fine. But I would like to extend the error messages at global scope, not at local scope. The map is defined static so that all instances of “error” share the same error codes and messages. Do you know how can I get this or something similar.

Thank you very 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-12T10:59:24+00:00Added an answer on June 12, 2026 at 10:59 am

    You need to ensure that the constructor for mErrorMessages runs before you use it by calling insertMessage. You can do this any way you want, but you must do it somehow.

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

Sidebar

Related Questions

I am getting a Segmentation fault (core dumped) run-time error with the following code:
I get the SIGSEGV - Segmentation Fault on the run-time for the code below
I have a segmentation fault in the code below, but after I changed it
My code gives me a segmentation fault when I run it with the rdebug
I am receiving a Segmentation Fault error when I run this program. To summarize,
I have segmentation fault problem with my multiple class files project. Without making empty
I am getting a segmentation fault and I cannot figure out why. I have
I have this output when trying to debug Program received signal SIGSEGV, Segmentation fault
I am getting a Segmentation Fault error when running my program in GCC. This
I have a software, code of which I have modified and run make again.

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.