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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:38:45+00:00 2026-05-26T14:38:45+00:00

I have 3 C++ structs as well as two methods and I want to

  • 0

I have 3 C++ structs as well as two methods and I want to use them via C#. Here is my C# code as well as few comments on the C++ code.

[StructLayout(LayoutKind.Sequential)]
public unsafe struct smat
{
    public long rows;
    public long cols;
    public long vals;     
    public long* pointr;  
    public long* rowind;  
    public double* value; 
};

[StructLayout(LayoutKind.Sequential)]
public unsafe struct dmat {
    long rows;
    long cols;
    double **value;
};

[StructLayout(LayoutKind.Sequential)]
public unsafe struct svdrec {
    int d;      
    dmat Ut;    // it was dmat* in C++
    double *S;  
    dmat Vt;    // it was dmat* in C++
};

[DllImport(@"file.dll", EntryPoint = "svdNewSMat", CallingConvention = CallingConvention.Cdecl)]
public static extern smat svdNewSMat(int rows, int cols, int vals); // it was smat* in C++

[DllImport(@"file.dll", EntryPoint = "svdLAS2", CallingConvention = CallingConvention.Cdecl)]
    public static extern svdrec svdLAS2(smat a, long dimensions, long iterations, double[] las2end, double kappa); // it was smat* and svdrec* in C++

EDIT:
Here are the C++ headers

typedef struct smat *SMat;
typedef struct dmat *DMat;
typedef struct svdrec *SVDRec;

/* Harwell-Boeing sparse matrix. */
struct smat {
  long rows;
  long cols;
  long vals;     /* Total non-zero entries. */
  long *pointr;  /* For each col (plus 1), index of first non-zero entry. */
  long *rowind;  /* For each nz entry, the row index. */
  double *value; /* For each nz entry, the value. */
};

/* Row-major dense matrix.  Rows are consecutive vectors. */
struct dmat {
  long rows;
  long cols;
  double **value; /* Accessed by [row][col]. Free value[0] and value to free.*/
};

struct svdrec {
  int d;      /* Dimensionality (rank) */
  DMat Ut;    /* Transpose of left singular vectors. (d by m)
             The vectors are the rows of Ut. */
  double *S;  /* Array of singular values. (length d) */
  DMat Vt;    /* Transpose of right singular vectors. (d by n)
             The vectors are the rows of Vt. */
};

extern DMat svdNewDMat(int rows, int cols);
extern SVDRec svdLAS2(SMat A, long dimensions, long iterations, double end[2], 
                  double kappa);

I am not giving the full code because It includes a lot of libraries.
And here is what I excecute to test:

var a = svdNewSMat(3, 6, 3);
var m = new double[] {1, 2};
var r = svdLAS2(a, 1, 0, m, 1e-6); // I get the Exception here

The second line gives me Attempted to read or write protected memory error.
Any ideas? Thanks in advance.

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

    You cannot simply replace dmat* with dmat on the C# side. A pointer is not automatically marshalled as reference

    Please look at http://msdn.microsoft.com/en-us/library/0szztey7(v=vs.80).aspx on how to marshall embedded pointers correctly.

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

Sidebar

Related Questions

So, I have two structs: struct coordinate { float x; float y; } struct
I have a list of structs and I want to change one element. For
I have two structs: template <typename T> struct Odp { T m_t; T operator=(const
I have a class which I want to expose a list of structs (which
I have many concrete-structs and I want to designate fields as optional (present or
For an assignment at school, we have to use structs to make matrices that
I have two structs like so: public struct KeyLog { Keys key; DateTime time;
I have structs like below and when I do that initialization: ArrayList nodesMatrix =
I have these structs: typedef struct _Frag{ struct _Frag *next; char *seq; int x1;
I have many structs (classes) and standalone functions that I like to compile separately

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.