1

Here are a few lines of C++ code I recently saw.

double a[p][p];
for(i=0;i<p;i++)
{
    for(j=0;j<p;j++)
    {
        a[i][j] = m[i][j]; //m is sth we are given.
    }
}
dgeev_(&jobvl,&jobvr,&p,(double*)a,&lda,(double*)wr,(double*)wi,(double*)vl,
      &ldvl,(double*)vr,&ldvr,(double*)work,&lwork,&info);
// sth else..

dgeev_ is a function and you can find its documentation here: http://www.netlib.org/clapack/old/double/dgeev.c

dgeev_ is declared in header in the following way, so we can use C function in C++ codes.

extern "C" {
void dgeev_(char*, char*, int*, double*, int*, double*, double*, double*,
 int*, double*, int*, double*, int*, int*);
}

As we can see, a is static array, and dgeev_ need a variable of type doublereal * for the 4th parameter.

What does (double*)a do here?

14
  • 2
    It's casting. You should have a good C or C++ reference on hand if this is baffling. It's an unusually long method signature, but it's not too crazy in terms of structure. Commented May 2, 2017 at 20:22
  • 2
    This is not C++ code. It's C. Both in style and in reality. Look at the file extension. Commented May 2, 2017 at 20:25
  • 1
    @WeatherVane: No, because a is two-dimensional. The cast is not only "necessary" to obtain a build; it is wrong. Commented May 2, 2017 at 20:29
  • 1
    @W.Yang: Then why is the file called dgeev.c? C++ code is not written in files named .c. Furthermore, the project's own FAQ says that it is C code. So I'm not sure why you're claiming that it is C++. Commented May 2, 2017 at 20:55
  • 1
    @BoundaryImposition You are right that dgeev.c is C. But the snippets of codes I provided is C++. By declaring the C function extern "C" (in the C++ code), so we can use C directly in C++. I think this may be important for this question. Commented May 2, 2017 at 20:59

1 Answer 1

2

It's either a mistake or the author is playing a trick on you.

He's used a C-style cast to "hack" the value a into the double* type, so that a two-dimensional array may be treated as a single-dimensional array. (doublereal is just an alias for double.)

Problem is, a isn't a double*, nor is it actually convertible to a double*. I don't know what it is convertible to, since VLAs are only available in C++ as a GCC extension; there is no standard wording to consult. I'm going to just conclude that this program has undefined behaviour (though it probably appears to work, due to how these things are practically laid out in memory).

If the author wants to be able to safely read this array using single-dimension indices, he should have declared it that way to begin with (optionally providing the illusion of 2D indexes on top of the data store).

Sign up to request clarification or add additional context in comments.

1 Comment

Comments are not for extended discussion; this conversation has been moved to chat.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.