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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:47:26+00:00 2026-06-13T19:47:26+00:00

I need to implement a matrix class in C++ and one of the operations

  • 0

I need to implement a matrix class in C++ and one of the operations must be matrix multiplication via dgemm. My professor had done an example in class in C, but for some reason I can’t get it to work in C++. This is my header file matrix.h:

#include <iostream>
#include <stdlib.h>

extern "C" {
#include "blas.h"
}

[blah blah blah, matrix class here; overloaded * operator will do the matrix      
multiplication]

matrix operator* (const matrix &other){

matrix AxB(Nrows, other.Ncolumns, "(" + name + "*" + "other.name" + ")");

char TRANSA = 'N';
char TRANSB = 'N';
int M = Nrows;
int N = other.Ncolumns;
int K = Ncolumns;
double alpha = 1.;
double beta = 0.;

dgemm_ (&TRANSA,
        &TRANSB,
        &M,
        &N,
        &K,
        &alpha,
        data,
        &M,
        other.data,
        &K,
        &beta,
        AxB.data,
        &M);

return AxB;

[blah blah blah, overloaded = operator here; i'm positive this is not the problem 
 since it works for matrix addition]

Main function:

#include "matrix.h"

main(){

// entries of A and B are randomized between 0 and 1
matrix A(5,5);
matrix B(5,5);

matrix C = A*B;

}

Now the blas.h header file:

void dgemm_ (char *TRANSA,
             char *TRANSB,
             int *M,
             int *N,
             int *K,
             double *ALPHA,
             double **A,
             int *LDA,
             double **B,
             int *LDB,
             double *BETA,
             double **C,
             int *LDC);

I’m to use the subroutine outlined here: http://www.netlib.org/blas/dgemm.f

Basically, the way we did it in class for the C implementation built a matrix as one long array using (double*) calloc(rows*columns, sizeof(double)).

My C++ implemenation does:

double **data;

data = new double[rows];

for(int i=1; i<=rows; ++i){
    data[i-1] = new double[columns];
}

and then I can index using data[i][j]. But since the dgemm subroutine is supposed to take in double *A, double *B, etc. but my matrices are double **A and so on, how do I fix this?

This is the error message I get from valgrind:

==18845== Invalid write of size 8
==18845==    at 0x4165C1C: ATL_dgezero (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x4149DA7: ATL_dNCmmJIK (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x415BE8E: ATL_dgemm (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x4182F54: ATL_dptgemm_nt (in /usr/lib/atlas-base/atlas   
/libblas.so.3gf.0)
==18845==    by 0x4183140: ATL_dptgemm (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x40818F6: atl_f77wrap_dgemm_ (in /usr/lib/atlas-base/atlas 
/libblas.so.3gf.0)
==18845==    by 0x4E4E0004: ???
==18845==  Address 0x478d680 is 8 bytes after a block of size 16 alloc'd
==18845==    at 0x402B454: operator new[](unsigned int) (in /usr/lib/valgrind   
/vgpreload_memcheck-x86-linux.so)
==18845==    by 0x8049045: dmatrix::dmatrix(int, int, std::string) (dmatrix.hpp:77)
==18845==    by 0x8049532: dmatrix::operator*(dmatrix const&) (dmatrix.hpp:218)
==18845==    by 0x8048DCB: main (main.cpp:17)
==18845== 
==18845== Invalid write of size 8
==18845==    at 0x4165C1F: ATL_dgezero (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x4149DA7: ATL_dNCmmJIK (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x415BE8E: ATL_dgemm (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x4182F54: ATL_dptgemm_nt (in /usr/lib/atlas-base/atlas 
/libblas.so.3gf.0)
==18845==    by 0x4183140: ATL_dptgemm (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x40818F6: atl_f77wrap_dgemm_ (in /usr/lib/atlas-base/atlas   
/libblas.so.3gf.0)
==18845==    by 0x4E4E0004: ???
==18845==  Address 0x478d678 is 0 bytes after a block of size 16 alloc'd
==18845==    at 0x402B454: operator new[](unsigned int) (in /usr/lib/valgrind 
/vgpreload_memcheck-x86-linux.so)
==18845==    by 0x8049045: dmatrix::dmatrix(int, int, std::string) (dmatrix.hpp:77)
==18845==    by 0x8049532: dmatrix::operator*(dmatrix const&) (dmatrix.hpp:218)
==18845==    by 0x8048DCB: main (main.cpp:17)
==18845== 
==18845== Invalid write of size 8
==18845==    at 0x4165C22: ATL_dgezero (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x4149DA7: ATL_dNCmmJIK (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x415BE8E: ATL_dgemm (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x4182F54: ATL_dptgemm_nt (in /usr/lib/atlas-base/atlas    
/libblas.so.3gf.0)
==18845==    by 0x4183140: ATL_dptgemm (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x40818F6: atl_f77wrap_dgemm_ (in /usr/lib/atlas-base/atlas 
/libblas.so.3gf.0)
==18845==    by 0x4E4E0004: ???
==18845==  Address 0x478d690 is not stack'd, malloc'd or (recently) free'd
==18845== 
==18845== Invalid write of size 8 
==18845==    at 0x4165C25: ATL_dgezero (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x4149DA7: ATL_dNCmmJIK (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x415BE8E: ATL_dgemm (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x4182F54: ATL_dptgemm_nt (in /usr/lib/atlas-base/atlas 
/libblas.so.3gf.0)
==18845==    by 0x4183140: ATL_dptgemm (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x40818F6: atl_f77wrap_dgemm_ (in /usr/lib/atlas-base/atlas 
/libblas.so.3gf.0)
==18845==    by 0x4E4E0004: ???
==18845==  Address 0x478d688 is not stack'd, malloc'd or (recently) free'd
==18845== 
==18845== Invalid read of size 8
==18845==    at 0x4143D60: ATL_dJIK0x0x0NN0x0x0_aX_bX (in /usr/lib/atlas-base/atlas
/libblas.so.3gf.0)
==18845==    by 0x4149A9D: ATL_dNCmmJIK (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x415BE8E: ATL_dgemm (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x4182F54: ATL_dptgemm_nt (in /usr/lib/atlas-base/atlas 
/libblas.so.3gf.0)
==18845==    by 0x4183140: ATL_dptgemm (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x40818F6: atl_f77wrap_dgemm_ (in /usr/lib/atlas-base/atlas 
/libblas.so.3gf.0)
==18845==    by 0x4E4E0004: ???
==18845==  Address 0x478cf80 is not stack'd, malloc'd or (recently) free'd
==18845== 
==18845== Invalid read of size 8
==18845==    at 0x4143D64: ATL_dJIK0x0x0NN0x0x0_aX_bX (in /usr/lib/atlas-base/atlas 
/libblas.so.3gf.0)
==18845==    by 0x4149A9D: ATL_dNCmmJIK (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x415BE8E: ATL_dgemm (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x4182F54: ATL_dptgemm_nt (in /usr/lib/atlas-base/atlas 
/libblas.so.3gf.0)
==18845==    by 0x4183140: ATL_dptgemm (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x40818F6: atl_f77wrap_dgemm_ (in /usr/lib/atlas-base/atlas    
/libblas.so.3gf.0)
==18845==    by 0x4E4E0004: ???
==18845==  Address 0x478d130 is 0 bytes after a block of size 16 alloc'd
==18845==    at 0x402B454: operator new[](unsigned int) (in /usr/lib/valgrind 
/vgpreload_memcheck-x86-linux.so)
==18845==    by 0x8049045: dmatrix::dmatrix(int, int, std::string) (dmatrix.hpp:77)
==18845==    by 0x8048D49: main (main.cpp:6)
==18845== 
==18845== Invalid read of size 8 
==18845==    at 0x4143D4C: ATL_dJIK0x0x0NN0x0x0_aX_bX (in /usr/lib/atlas-base/atlas 
/libblas.so.3gf.0)
==18845==    by 0x4149A9D: ATL_dNCmmJIK (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x415BE8E: ATL_dgemm (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x4182F54: ATL_dptgemm_nt (in /usr/lib/atlas-base/atlas 
/libblas.so.3gf.0)
==18845==    by 0x4183140: ATL_dptgemm (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x40818F6: atl_f77wrap_dgemm_ (in /usr/lib/atlas-base/atlas 
/libblas.so.3gf.0)
==18845==    by 0x4E4E0004: ???
==18845==  Address 0x478d678 is 0 bytes after a block of size 16 alloc'd
==18845==    at 0x402B454: operator new[](unsigned int) (in /usr/lib/valgrind 
/vgpreload_memcheck-x86-linux.so)
==18845==    by 0x8049045: dmatrix::dmatrix(int, int, std::string) (dmatrix.hpp:77)
==18845==    by 0x8049532: dmatrix::operator*(dmatrix const&) (dmatrix.hpp:218)
==18845==    by 0x8048DCB: main (main.cpp:17)
==18845== 
==18845== Invalid write of size 8
==18845==    at 0x4143D93: ATL_dJIK0x0x0NN0x0x0_aX_bX (in /usr/lib/atlas-base/atlas 
/libblas.so.3gf.0)
==18845==    by 0x4149A9D: ATL_dNCmmJIK (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x415BE8E: ATL_dgemm (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x4182F54: ATL_dptgemm_nt (in /usr/lib/atlas-base/atlas 
/libblas.so.3gf.0)
==18845==    by 0x4183140: ATL_dptgemm (in /usr/lib/atlas-base/atlas/libblas.so.3gf.0)
==18845==    by 0x40818F6: atl_f77wrap_dgemm_ (in /usr/lib/atlas-base/atlas   
/libblas.so.3gf.0)
==18845==    by 0x4E4E0004: ???
==18845==  Address 0x478d678 is 0 bytes after a block of size 16 alloc'd
==18845==    at 0x402B454: operator new[](unsigned int) (in /usr/lib/valgrind 
/vgpreload_memcheck-x86-linux.so)
==18845==    by 0x8049045: dmatrix::dmatrix(int, int, std::string) (dmatrix.hpp:77)
==18845==    by 0x8049532: dmatrix::operator*(dmatrix const&) (dmatrix.hpp:218)
==18845==    by 0x8048DCB: main (main.cpp:17)
==18845== 
==18845== Invalid read of size 8
==18845==    at 0x8048DEA: main (main.cpp:19)
==18845==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==18845== 
==18845== 
==18845== Process terminating with default action of signal 11 (SIGSEGV)
==18845==  Access not within mapped region at address 0x0
==18845==    at 0x8048DEA: main (main.cpp:19)
==18845==  If you believe this happened as a result of a stack
==18845==  overflow in your program's main thread (unlikely but
==18845==  possible), you can try to increase the size of the
==18845==  main thread stack using the --main-stacksize= flag.
==18845==  The main thread stack size used in this run was 8388608.
==18845== 
==18845== HEAP SUMMARY:
==18845==     in use at exit: 3,581 bytes in 39 blocks
==18845==   total heap usage: 49 allocs, 10 frees, 7,760 bytes allocated
==18845== 
==18845== LEAK SUMMARY:
==18845==    definitely lost: 128 bytes in 4 blocks
==18845==    indirectly lost: 0 bytes in 0 blocks
==18845==      possibly lost: 88 bytes in 4 blocks
==18845==    still reachable: 3,365 bytes in 31 blocks
==18845==         suppressed: 0 bytes in 0 blocks
==18845== Rerun with --leak-check=full to see details of leaked memory
==18845== 
==18845== For counts of detected and suppressed errors, rerun with: -v
==18845== ERROR SUMMARY: 111 errors from 9 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)

up until I try to implement the dgemm matrix multiplication, I get no errors and no leaks, so I’m pretty sure all my troubles lie with the dgemm implementation

  • 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-13T19:47:28+00:00Added an answer on June 13, 2026 at 7:47 pm

    The memory layout of the C version is different from the memory layout in your C++ version. As BLAS expects the kind of layout used by the C version, your C++ version won’t work.

    Thus, you need to allocate a big 1-D array in your C++ version as well; you could overload operator() in order to get indexing for 2D arrays. Or for production code, use a library such as Eigen.

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

Sidebar

Related Questions

I need to implement operations with matrices and size of matrix has to be
I need to implement concurrent matrix multiplication in C using multiple processes. I understand
I have a matrix class very tailored for the algorithm I need to implement.
I need to implement portable code, but I do not know how to deal
I need to implement a one-to-one videoconferencing solution server-based, runnable by browser, free (or
I need to implement a very large matrix, say NxN in Standard C. The
I'm implementing a maxmin function, it works like matrix multiplication but instead of summing
I have some problem with matrix multiplication: I want to multiplicate for example a
I am writing a sparse matrix class. I need to have a node class,
Not too practical maybe, but still interesting. Having some abstract question on matrix multiplication

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.