Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Fortran MEX... can't allocate using gcc

Subject: Fortran MEX... can't allocate using gcc

From: Thomas Clark

Date: 4 Mar, 2009 13:18:01

Message: 1 of 1

Hi all,

I'm having problems porting a fortran MEX file to unix. This file works perfectly under windows (32 bit XP with Intel Visual Fortran 9.0 and Matlab R2007b). However, I'm getting segmentation violations when porting it to my linux box (Suse 10.3, kernel 2.6.x, 32 bit, Matlab R2008a, gcc 4.2.1).

I've provided a cut-down sample of the code at the end of this message - it's pretty standard, really. The SegV occurs when I try to allocate the 'pixels' array.

The only difference between the code I'm using in windows and the code in linux is that I had to convert to free-form (i.e. change line continuations from numbered in col 6 to use the & character). But the problem is repeatable even when I eliminate all line continuations - so I don't think it's a code form problem.

I've tried debugging with gdb, and there's nothing obviously wrong with my code - I think there must be a compiler dependency. In gdb, I get this error message:
___________________________
Program received signal SIGSEGV, Segmentation fault.
*_gfortran_allocate_array (mem=0x0, size=28, stat=0x0)
    at ../../../../../sources/gcc/libgfortran/runtime/memory.c:246
246 ../../../../../sources/gcc/libgfortran/runtime/memory.c: No such file or directory.
        in ../../../../../sources/gcc/libgfortran/runtime/memory.c
Current language: auto; currently c
____________________________
Although the debugger recognises that this is fortran source, it appears that a c library is used for the allocation of the memory. Is this normal, or could it be the source of the error?

Thanks for any help!

Kind regards,

Tom Clark



________ CODE SAMPLE ____________
#include "fintrf.h"


SUBROUTINE mexFunction(nlhs, plhs, nrhs, prhs)


IMPLICIT NONE

INTEGER*4 nlhs, nrhs


mwPointer plhs(*), prhs(*)


mwPointer mxGetPr


mwPointer xptrNPIX, xptrPIXELS



INTEGER*4 :: NPIX

REAL*4, DIMENSION(:), ALLOCATABLE :: PIXELS




! Get pointers to the input arguments out of the prhs array

xptrNPIX = mxGetPr(prhs(1))

xptrPIXELS = mxGetPr(prhs(2))



! Make local copies of the input arguments

CALL mxcopyPtrToInteger4(xptrNPIX, NPIX, 1)



! NB have to allocate the size of the local copy

ALLOCATE(PIXELS(NPIX))

CALL mxCopyPtrToReal4(xptrPIXELS, PIXELS, NPIX)

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us