Dynamic memory, allocs and pointers

From: Anthony (Anthony_at_work.nl)
Date: 06/29/04


Date: Tue, 29 Jun 2004 08:52:00 +0200

Hi,

In example the routine 'pointers' is used for wrapping
F90's ALLOCATE with some proper error handling included.
      file test.f:
      program test
      use pointers_mod
      real, allocatable, dimension(:) :: a ! is this the
right declaration ???
      n = 10
      call pointers(a,n)
      a = (/(i,i=1,n)/)
      write(*,*) ' Memory used so far ',memory_used
      end program test

     file pointers.f:
      module pointers_mod
         integer :: memory_used=0
      contains
      subroutine pointers(a,n)
      integer :: n, error
      real, dimension(:), allocatable :: a
      allocate(a(n), stat=error)
      memory_used= memory_used+ n
      if (error /=0) stop 'Memory problem'
      end subroutine pointers
      end module pointers_mod

In Visual Fortan (MS Dev Studio) this works nice, but I have
doubt about the declaration of array 'a' in the main
program.

Or must this better be replaced by declaration

       real, pointer, dimension(:) :: a

from the idea that routine 'pointers' returns a pointer to
the just allocated array. Something you have in C programs.
I know that pointers in F90 work more like aliases, so are
different the 'real pointers' in C.

At the other hand, the example above works for both
declarations, thus it gives the expected result.

Some help would be nice.

Thanks.
Anthony.



Relevant Pages

  • Re: strcat not working
    ... The elements of strare pointers to string literal values, ... The destination argument to strcat() must be a writable ... its implicit declaration as intis not correct, ... have an appropriate newsgroups line in your header for your mail to be seen, ...
    (comp.lang.c.moderated)
  • Re: Linked lists asignment help..
    ... Don't cast malloc(). ... proper declaration of mallocin scope; ... understand the basics of pointers, arrays, and the difference between ...
    (comp.lang.c)
  • Re: Consideration on pointer declarations
    ... to pointers like often found in function parameters. ... C's unique type declaration syntax doesn't really help in separating things ... had to fire up another machine with a known working cdecl, ...
    (comp.lang.c)
  • Re: Error when creating class reference to an instance of itself
    ... The forward declaration is completely unnecessary in this case, ... you still can declare pointers to that type without any problems. ... incomplete type 'BinaryTree::Node', ...
    (comp.lang.cpp)
  • Re: extern vars and linking
    ... > LOCAL int secondary_func{ ... > Compile and link libA.cpp as part of a library libA.a ... Including a declaration in any number of translation units is not going ... > C and CPP program any help or pointers would be much apprecaited. ...
    (comp.lang.cpp)