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

  • Thread starter Thread starter Niles
  • Start date Start date
  • Tags Tags
    Fortran
AI Thread Summary
In FORTRAN, when a pointer is declared, it does not point to any memory location until memory is allocated. In the example provided, when the code "ALLOCATE(ptr)" is executed, "ptr" receives a new address pointing to the allocated memory for an integer. If the pointer is allocated an array, such as with "ALLOCATE(ptr(20))", it points to a new array of 20 integers. Importantly, if "ptr" already points to an existing array, that array will not be automatically deallocated upon the new allocation, which requires manual management of memory to avoid memory leaks.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top