Does Allocating a Pointer in FORTRAN Give it a New Address?

  • Context: Fortran 
  • Thread starter Thread starter Niles
  • Start date Start date
  • Tags Tags
    Fortran
Click For Summary
SUMMARY

In FORTRAN, when a pointer is allocated using the ALLOCATE statement, it receives a new address pointing to the allocated memory. For example, the code snippet "integer, pointer :: ptr; ALLOCATE(ptr)" results in "ptr" pointing to a new integer. If the pointer previously pointed to an array, such as "ptr(:)", and a new allocation occurs, the original array remains allocated unless explicitly deallocated. This behavior emphasizes the importance of managing memory allocations in FORTRAN to prevent memory leaks.

PREREQUISITES
  • Understanding of FORTRAN pointer syntax
  • Knowledge of memory allocation concepts in programming
  • Familiarity with the ALLOCATE statement in FORTRAN
  • Basic understanding of array handling in FORTRAN
NEXT STEPS
  • Research memory management techniques in FORTRAN
  • Learn about the DEALLOCATE statement in FORTRAN
  • Explore best practices for using pointers in FORTRAN
  • Study the differences between static and dynamic memory allocation in FORTRAN
USEFUL FOR

FORTRAN developers, programmers working with scientific computing, and anyone involved in memory management within FORTRAN applications.

Niles
Messages
1,834
Reaction score
0
Hi all.

When I have the following code in FORTRAN:

Code:
integer, pointer :: ptr
ALLOCATE(ptr)

then does "ptr" receive a new address (i.e. do we have "ptr => <new integer>") or does the pointer retain its address (i.e. we don't have "ptr => ...")?
 
Technology news on Phys.org
That is a bit weird just allocating a single integer. If you did this:

integer, pointer :: ptr(:)
allocate(ptr(20))

Then an array of 20 integers would be allocated and ptr would be set to point to the new array. If ptr already points to an array then that array will not automatically be deallocated.
 
Niles said:
Hi all.

When I have the following code in FORTRAN:

Code:
integer, pointer :: ptr
ALLOCATE(ptr)

then does "ptr" receive a new address (i.e. do we have "ptr => <new integer>") or does the pointer retain its address (i.e. we don't have "ptr => ...")?
What do you mean a "new" address? When you declare the pointer, there is NO memory location assigned to it until memory is allocated to it.
 

Similar threads

  • · Replies 118 ·
4
Replies
118
Views
10K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 19 ·
Replies
19
Views
6K
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
7
Views
5K
Replies
3
Views
2K
  • · Replies 19 ·
Replies
19
Views
4K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K