Where does the pointer point?

somlioy

Member
Join Date
Jan 2011
Location
Localhost
Posts
27
If one were to bubble sort an array (data from AD) using pointers, how would one keep track of which pointer points to which AD after sorting?
or: Is there any way to extract the index of the pointer to the array?

(Codesys)
 
Last edited:
No,no,no,no,no,no,no,no..!!!!!!!!

Don't return a pointer to a temporary variable!!!!!!
There are on the stack and can be written over.
Pointers are dangerous unless you know how to use them.
I need to look at the IEC specification again. I know pointers were not included in the first version of ST.
 
Hi Peter,

Good advice.

Please note the key points.

1. I am returning a value by dereferencing the pointer - ^ is the syntax for dereferencing.

2. So, by using ADR and then ^ I am essentially looking at the value of i. One will realize that in the code could just look at i instead of ADR and ^ - that's correct! The code was a reference only for depiction of usage of ADR and ^

3. As far as stack - that's typical of local variables in functions, it's normal for them to get destroyed when the function exits.

4. The value return of the function (regardless of ^ pointer) still needs to be stored in a global variable outside of the function.

5. The fear of returning pointers is when memory is moved around middle of execution - like an online change of the PLC.

6. Pointers can be used as an extension of the IEC 61131-3 standard.


Thanks.
 
You should look into linked lists if you're going to be sorting with pointers.

I'd like to point out that bubble sorts can be optimized easily by reducing how far you go in the array by one every time to get to the end of the array. Each completion of a run through the array ensures that the last position you checked now contains the largest number left, so you never have to recheck that element or the ones after it again.

https://en.wikipedia.org/wiki/Bubble_sort#/media/File:Bubble_sort_animation.gif
 

Similar Topics

Hi there, I need to work on a GE series 5 PLC which need DOS based Logic Master 5, it seems like nowhere I can purchase this software anymore...
Replies
2
Views
945
Yesterday I had to power down a machine to work on a mechanical problem. When I was done and powered back up I got an E05 on one of the Kinetix...
Replies
2
Views
2,295
I need a explanation of how data is translated between different devices. Where is this data being stored? What different languages of information...
Replies
5
Views
1,802
This is logic for a stand alone machine, no network, just this one machine. It has one single SLC-503. I'm working on getting individaul...
Replies
2
Views
1,214
We are fitting a profibus repeater to a network and I wondered if it matters where it goes. Does it have to be at the farthest point of the...
Replies
3
Views
2,192
Back
Top Bottom