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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:24:03+00:00 2026-05-13T23:24:03+00:00

I’m having a weird problem with memset, that was something to do with a

  • 0

I’m having a weird problem with memset, that was something to do with a class I’m creating before it and a file I’m opening in the constructor. The class I’m working with normally reads in an array and transforms it into another array, but that’s not important. The class I’m working with is:

#include <vector>
#include <algorithm>
using namespace std;
class PreProcess
{
 public:
  PreProcess(char* fileName,char* outFileName);
  void SortedOrder();
 private:
  vector< vector<double > > matrix;
  void SortRow(vector<double> &row);
  char* newFileName;
  vector< pair<double,int> > rowSorted;
};

The other functions aren’t important, because I’ve stopped calling them and the problem persists. Essentially I’ve narrowed it down to my constructor:

PreProcess::PreProcess(char* fileName,char* outFileName):newFileName(outFileName){
  ifstream input(fileName);
  input.close(); //this statement is inconsequential
}

I also read in the file in my constructor, but I’ve found that the problem persists if I don’t read in the matrix and just open the file. Essentially I’ve narrowed it down to if I comment out those two lines the memset works properly, otherwise it doesn’t.

Now to the context of the problem I’m having with it: I wrote my own simple wrapper class for matrices. It doesn’t have much functionality, I just need 2D arrays in the next part of my project and having a class handle everything makes more sense to me.

The header file:

#include <iostream>
using namespace std;
class Matrix{
 public:
  Matrix(int r,int c);
  int &operator()(int i,int j)
  {//I know I should check my bounds here
    return matrix[i*columns+j];
  }
  ~Matrix();
  const void Display();
 private:
  int *matrix;
  const int rows;
  const int columns;
};

Driver:

#include "Matrix.h"
#include <string>
using namespace std;
Matrix::Matrix(int r,int c):rows(r),columns(c)
{
  matrix=new int[rows*columns];
  memset(matrix,0,sizeof(matrix));
  }
const void Matrix::Display(){
  for(int i=0;i<rows;i++){
    for(int j=0;j<columns;j++)
      cout << (*this)(i,j) << " ";
    cout << endl;
  }
}
Matrix::~Matrix()
{
  delete matrix;
}

My main program runs:

PreProcess test1(argv[1],argv[2]);
//test1.SortedOrder();
Matrix test(10,10);
test.Display();

And when I run this with the input line uncommented I get:

0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 -1371727776 32698 -1 0 
0 0 0 0 6332656 0 -1 -1 0 0 
6332672 0 0 0 0 0 0 0 0 0 
0 0 0 0 -1371732704 32698 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 

I really don’t have a clue what’s going on in memory to cause this, on a side note if I replace memset with:

for(int i=0;i<rows*columns;i++)
   *(matrix+i) &= 0x0;

Then it works perfectly, it also works if I don’t open the file. If it helps I’m running GCC 64-bit version 4.2.4 on Ubuntu.I assume there’s some functionality of memset that I’m not properly understanding.

  • 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-13T23:24:04+00:00Added an answer on May 13, 2026 at 11:24 pm

    You use memset() like this:

    memset(matrix,0,sizeof(matrix));
    

    Here matrix is a pointer, so sizeof(matrix) gives the size of a pointer, not the size of the array. To fill the whole array, use columns * rows * sizeof(int) instead.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,

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.