Problems when comparing mwSize and mwIndex variables

2 views (last 30 days)
Hello
I am slowly changing my old fortran-c codes to the new matlab standard. When changing one of the gateway funtions, I encounter the following
....
mwIndex nyt, nsuby;
mwSize YDA_COLS;
....
then nyt and YDA_COLS = nsuby = mxGetN(YDA_IN) take value 1, that is nyt=1 and YDA_COLS=1. When I issue the following comand
if (YDA_COLS < nyt)
{
mexErrMsgTxt("Number of Columns of Y is incompatible. ");
}
The number os Columns of Y is incompatible! I have no idea of what is wrong. If I change to mwSize nyt, nsuby, the same thing happens. It only works if I change to int nyt, nsuby.
Many thanks
Ed
  10 Comments
Ed Mendes
Ed Mendes on 30 Apr 2019
I have changed nyt in the fortran code from integer to integer*8 just to see what happens. The output was
>> [x,m,ve]=olsml65(model,u,y,10,'c');
YDA_COLS = 1 , 8 bytes , is signed? 0
nyt = 1 , 8 bytes , is signed? 1
Everything OK
for "mwIndex nyt"
James Tursa
James Tursa on 30 Apr 2019
Can you clarify? Are you linking compiled C-mex code to compiled Fortran code? How does changing integer to integer*8 on the Fortran side affect the "signed" output on the C side? I am confused about what you are actually doing.

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 30 Apr 2019
Edited: James Tursa on 30 Apr 2019
A couple of observations:
>> typecast(uint64(1),'uint8')
ans =
1 0 0 0 0 0 0 0
>> typecast(uint64(4294967297),'uint8')
ans =
1 0 0 0 1 0 0 0
So now I am thinking it may be a function signature problem. If a function is returning a value of 1 as an int (4-bytes), but you told the compiler it is returning an 8-byte integer, then an extra garbage 4-bytes may be picked up by the compiler and stuffed into nyt, accounting for that extra 1 in the bit pattern. This is just speculation, but I have seen this behavior before when prototypes/signatures are not correct.
I next would like to see the exact code where nyt is being set. And if your functions are involved, the prototypes/signatures of those functions.
Are you getting any compiler warnings about mismatched pointer types?
Do you have compiled C mex code calling compiled Fortran code? If so, then I would need to see the Fortran function/subroutine signature also. E.g., if it is the Fortran signature you show above, then I would expect all of the integers on the Fortran side to be 4-bytes. And if you are passing in pointers to 8-byte integers from the C-mex side then yes that would be a problem.
  4 Comments
Ed Mendes
Ed Mendes on 3 May 2019
Many thanks. I will certainly check all of the arguments painstakingly.
If I keep the default integer on the fortran sise, I will not make use of array length beyond 32-bit, will I? If that is the case, would you advise me what I need to pay attention to? I need to change all C-gateway and fortran-77 sources to make full use of 64-bit and to make them easily compiled in future versions of Matlab.
Ed Mendes
Ed Mendes on 3 May 2019
I decided to shorten the fortran code to see if I could convert it to the new standard. Unfortunately I run into trouble. Attached are the sources files for the C-gateway and Fotran-77 with integer*8.
The fortran source was compiled with "gfortran -c calnpr.f" and then I used mex command to do the rest.
It has been a long time that I try to modify and compile my old source files and it is very likely that I am missing something or doing something stupid.
Many thanks

Sign in to comment.

More Answers (0)

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!