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

  • Home
  • SEARCH
  • 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 8723577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:39:41+00:00 2026-06-13T07:39:41+00:00

Possible Duplicate: Matrix Arithmetic using Vectors in C++ causing segmentation faults I created this

  • 0

Possible Duplicate:
Matrix Arithmetic using Vectors in C++ causing segmentation faults

I created this simple code in C++ to assign some values to a dynamic matrix:

unsigned N = 1000;
vector<vector<double> > Matrix;

for (unsigned i=0; i<(N-1); ++i) {
    for (unsigned j=0; j<(N-1); ++j) {

        if ((i>(N/4-1) && i<(3*N/4-1)) || (j>(N/4-1) && j<(3*N/4-1)))
            Matrix[i][j] = 1;

        else if (i==0 || i==(N-1) || j==0 || j==(N-1))
            Matrix[i][j] = 0;
}
}

The compiler does not return any problem, but when I try to run the program, it returns: Segmentation Fault. Where is my mistake?

Thank you for your attention.

  • 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-13T07:39:42+00:00Added an answer on June 13, 2026 at 7:39 am

    You are just creating two empty vectors, and using the operator[] is undefined behaviour. They are like arrays of size 0, if such a thing was possible.

    You have to create the vectors with a capacity, and each element will be default-initialised to 0:

    vector<vector<double>> Matrix(N, vector<double>(N));
    

    The first argument is the size of the outer vector, and the second is the value to copy into each element, which is itself a vector of N doubles.

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

Sidebar

Related Questions

Possible Duplicate: Returning a reference can work? Why this code works ( matrix is
Possible Duplicate: C programming, why does this large array declaration produce a segmentation fault?
Possible Duplicate: R gvisTable pre-selection using event trigger option and javascript code Hi I
Possible Duplicate: Floating point error in representation? So I am running this code to
Possible Duplicate: MATLAB: How To Efficiently Remove NaN Elements from Matrix I got code
Possible Duplicate: correlation matrix in r I have an excel sheet which has 700
Possible Duplicate: Algorithm to find the total number of connected sets in a matrix
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:

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.