2

I have been using C++ for several years. Then I decided to use Fortran for better math performance. In C++ I have the following structure which I use everywhere:

structure     BitMap{
char*     rgba;         // pointer to the color array
int       w, h;};       // dimension of my bitmap

In Fortran, on the other hand:

Program Bitmap_p
implicit none
type BitMap
character :: rgba(*) ! like a pointer to bitmap array (colors and alpha)
integer:: w  ! bitmap width
integer:: h  ! bitmap height
endtype BitMap
endprogram Bitmap_p

However, when compiling this, the compilers states:

  1. f90 (4): error #6530: The array spec for this component must be of explicit shape and each bound must be an initialization expression. [RGBA]
kvantour
  • 25,269
  • 4
  • 47
  • 72
smval80
  • 21
  • 3
  • Sorry, please, for my English. I'm trying to avoid grammar mistakes. But it difficult for me yet. – smval80 Jul 09 '19 at 01:55
  • A pointer is something quite different from a string in both C++ and Fortran. You've translated a pointer in C++ into a string in Fortran. The two are not equivalent (In fact, pointers operate differently in C++ and Fortran, and strings also work differently between C++ and Fortran). – Peter Jul 09 '19 at 02:56
  • I'm sure you understand my goal. What dodge can I use for that? – smval80 Jul 09 '19 at 03:25
  • Of couse, I can't say that fortran has obviouse speed advantages. I only use simd parallelism "from the box", it's very comfortably for me. – smval80 Jul 09 '19 at 07:39
  • I mean: (x, y) = (x0, y0)+k×(dx, dy) through xmm registers. – smval80 Jul 09 '19 at 07:43
  • You probably want an allocatable object of some sort. But that's all I can really say - I don't understand what you are trying to do here. And please note the spelling of Fortran – Ian Bush Jul 09 '19 at 08:08
  • I want to send my bitmap structure from c++ main.object to fortran_lib.object as ONE argument. – smval80 Jul 09 '19 at 08:21
  • I can send my rgba× pointer (and other dynamic arrays)as separate arguments, but then my fortran function has about 10 parameters. It's not good, I think. When I send via structures my fortran function has only two parameters. – smval80 Jul 09 '19 at 08:36
  • What language will operate on the data (as opposed to "just passing it around")? In what language is the main program? What are your hard constraints (some existing lib in C++ or Fortran, etc)? – Pierre de Buyl Jul 09 '19 at 08:38
  • There is no problem to mix c++ and assembler. In this case I can create 1 structure (with lot of pointers to arrays)and send it to assembler as 1 argument. But assembler is too hard. I want to do that with fortran. – smval80 Jul 09 '19 at 08:49
  • Fortran doesn't allow me to create types whith free dimension arrays. That is my problem. – smval80 Jul 09 '19 at 09:05
  • Thank you all. I'll try explane when I am at home. – smval80 Jul 09 '19 at 10:52

1 Answers1

1

You should be able to use TYPE and POINTER in fortran to do what you can do with struct and * in C.

Program Bitmap_p
implicit none
type BitMap
character, pointer :: rgba(:) ! like a pointer to bitmap array (colors and alpha)
integer:: w  ! bitmap width
integer:: h  ! bitmap height
endtype BitMap

type(BitMap) :: bmap

bmap%w = 10
bmap%h = 10
allocate( bmap%rgba(4*bmap%w*bmap%h) )

endprogram Bitmap_p
L. Scott Johnson
  • 4,213
  • 2
  • 17
  • 28
  • 3
    Making the character array Allocatable rather than a pointer is most likely better style Fortran - pointers should be avoided wherever possible, allocatables avid a number of risks associated with them. – Ian Bush Jul 09 '19 at 13:13
  • When possible, yes (and the same can be said for C++ char pointers). I assume the OP knows how to handle them in C++, and the same should work here. – L. Scott Johnson Jul 09 '19 at 13:19
  • @smval80 You're welcome. If you're satisfied with the answer, please [accept] it -- that's the SO way of saying thanks. – L. Scott Johnson Jul 10 '19 at 13:58
  • Thank you very much. But I don't know what size my array will have. I need to pass a completed structure (the structure created and initialized in c++ class) to the FORTRAN subroutine for performing it. If I pass arguments into FORTRAN subroutine not by structure but one by one, it will be list of about 12-15 arguments. For example: extern "C" plot_(sbmp *bmp, smatrix *matr, spoints *points), each of thous structs has 2-3 pointers. What unit do I need to create in FORTRAN to operate with as argument to subroutine? – smval80 Jul 10 '19 at 14:09
  • Sorry. I haven't understood yet haw to make [accept]. But I shall try. – smval80 Jul 10 '19 at 14:15
  • Ah, the question indicates that you're re-writing all of the C code in Fortran. If you just want to call a Fortran function from C, see for example: https://stackoverflow.com/questions/34561835/pass-arrays-from-c-c-to-fortran-and-return-a-calculated-array – L. Scott Johnson Jul 10 '19 at 14:21
  • Of course, that example works. But there isn't any structs. I avoid large lists of arguments. – smval80 Jul 10 '19 at 14:34
  • In addition. I use c++ for GUI (QTCreator) and FORTRAN for all math. FORTRAN can do simd parallelization "from the box". – smval80 Jul 10 '19 at 14:46