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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:00:05+00:00 2026-05-25T14:00:05+00:00

I have built a constructor for a matrix object. The data is being stored

  • 0

I have built a constructor for a matrix object. The data is being stored in an array of struct val, which in turn hold position (in the matrix) and value. This is the code:

SparseMatrix::SparseMatrix(const int numRow, const int numCol, vector<double> fill):
        Matrix(numRow,numCol)
{
    _matrix = new vector<val*>(fill.size());
    vector<double>::iterator itr = fill.begin();
    for(signed int i=0; i<numRow; i++)
    {
        for(signed int j=0; j<numCol; j++, itr++)
        {
            if (*itr == 0)
            {
                continue;
            }
            val *temp = new val;
            temp->value = *itr;
            temp->x = i;
            temp->y = j;
            _matrix->push_back(temp);
            cout << "Psition: " << ": " << _matrix->front()->x << //<--ERROR     
            cout << " " << _matrix->back()->y << endl;
        }
    }
}

You’ll notice that I’ve added cout just to verify that push_back does not really work for me. _matrix is on the heap and so does all the structs that are being held in it. All created using ‘new’. I fail to see why this doesn’t work. One line after I push a new struct pointer to the vector I can’t read it (segmentation fault as I said).

Any ideas? thanks!

EDIT:
Sorry, this is valgrind’s message:

==13334== Invalid read of size 4
==13334==    at 0x804AEAE: SparseMatrix::SparseMatrix(int, int, std::vector<double,     std::allocator<double> >) (in /home/yotamoo/workspace/ex3/main)
==13334==    by 0x8048C10: main (in /home/yotamoo/workspace/ex3/main)
==13334==  Address 0x8 is not stack'd, malloc'd or (recently) free'd
==13334== 
==13334== 
==13334== Process terminating with default action of signal 11 (SIGSEGV)
==13334==  Access not within mapped region at address 0x8
==13334==    at 0x804AEAE: SparseMatrix::SparseMatrix(int, int, std::vector<double,     std::allocator<double> >) (in /home/yotamoo/workspace/ex3/main)
==13334==    by 0x8048C10: main (in /home/yotamoo/workspace/ex3/main)
==13334==  If you believe this happened as a result of a stack
==13334==  overflow in your program's main thread (unlikely but
==13334==  possible), you can try to increase the size of the
==13334==  main thread stack using the --main-stacksize= flag.
==13334==  The main thread stack size used in this run was 8388608.

And – segmentation fault occurs during the first iteration!

  • 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-25T14:00:06+00:00Added an answer on May 25, 2026 at 2:00 pm

    This is the std::vector<val *> constructor that is being used to construct *_matrix:

    explicit vector ( size_type n, const val *& value = val *(), const std::allocator<val *>& = std::allocator<val *>() );
    

    The invocation of that constructor with n set to fill.size() creates a new std::vector<val *> having fill.size() default-constructed val * objects (all NULL). When you push back a new val pointer, *_matrix has fill.size() + 1 elements and _matrix->front() is still a NULL pointer. In effect, you are dereferencing NULL.

    You are probably looking for the reserve() method.

    EDIT: There are some other things that I noticed which can be improved:

    1. The SparseMatrix constructor is taking fill by value. This means that in order to construct a new SparseMatrix, a complete copy of the vector of double objects is made. You should pass fill by const-reference instead.
    2. With STL container iterators, unless you need the result of a post-increment, you should always use the pre-increment operator.
    3. If one of the allocations of a new val object throws a std::bad_alloc exception, then you will leak memory.
    4. Consider using a pool allocator to allocate the many val objects. Some C/C++ runtime library implementations (e.g. OpenBSD’s) randomize memory allocations. Allocating many, small objects on the heap can lead to severe heap fragmentation.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have built a web page which contains a Crystal Report built using the
I have a ViewModel class which i want to respond to the built in
Say I have an array of chars, which I have allocated on the heap,
I'd like to make a constructor for my object built at runtime that calls
I've built a class and would like to include a constructor which will construct
We have built an internal tool that generates the whole data access, each table
I have a question concerning subtypes of built-in types and their constructors. I want
I have built an MSI that I would like to deploy, and update frequently.
I have built a simple C#.Net app on a M/C with only .Net FX
I have built an application in C# that I would like to be optimized

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.