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

  • Fortran
  • Thread starter Niles
  • Start date
  • Tags
    Fortran
When you allocate the pointer, the memory location is assigned.In summary, the conversation discusses the behavior of a pointer in FORTRAN when memory is allocated to it. It is clarified that when a pointer is declared, it does not have a memory location until it is allocated. When an integer is allocated to the pointer, it receives a new address and when an array is allocated to the pointer, it points to the new array while retaining its previous address if it already pointed to an array.
  • #1
Niles
1,866
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
  • #2
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.
 
  • #3
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.
 

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

What is FORTRAN?

FORTRAN stands for "Formula Translation" and is a programming language used for scientific and engineering applications. It was one of the first high-level programming languages developed and is still widely used today.

What does it mean to allocate a pointer?

Allocating a pointer in FORTRAN refers to the process of setting aside a section of memory to store a variable or data. This allows the programmer to access and modify the data stored in that memory location.

How do you allocate a pointer in FORTRAN?

To allocate a pointer in FORTRAN, the programmer must first declare the pointer variable using the "ALLOCATABLE" keyword. Then, they can use the "ALLOCATE" statement to assign memory to the pointer variable.

What are the advantages of using pointers in FORTRAN?

Using pointers in FORTRAN can allow for more efficient memory usage, as it allows the programmer to allocate memory as needed and modify its contents. Pointers can also be used to create dynamic data structures, such as linked lists and trees.

Are there any potential drawbacks to using pointers in FORTRAN?

One potential drawback of using pointers in FORTRAN is the risk of memory leaks or errors if the programmer does not properly deallocate the memory allocated to the pointer. Additionally, using pointers can make the code more complex and difficult to debug.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
4
Replies
118
Views
7K
  • Programming and Computer Science
Replies
19
Views
3K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
7
Views
5K
  • Programming and Computer Science
Replies
3
Views
858
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
7
Views
3K
Back
Top