• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Getting element of array returns a pointer?

Guest
Apr 11, 2014 Apr 11, 2014

Copy link to clipboard

Copied

Hi to All,

I have a code:

/tt [[0 1 2] [10 11 12] [20 21 22]] def

tt 0 get    /ttr exch def          % ttr = [0 1 2]

ttr 2 777 put                      % ttr = [0 1 777]

tt pstack                          %  tt = [[0 1 777] [10 11 12] [20 21 22]]

I thought tt should not be changed. But it is. Why is this? How may I avoid this?

Thank you in advance,

JackK

TOPICS
Programming

Views

643

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Contributor , Apr 12, 2014 Apr 12, 2014

From the PLRM, 3rd edition:

As discussed earlier, an array is a composite object. When an array object is copied,

the value is not copied. Instead, the old and new objects share the same value.

Additionally, there is an operator (getinterval) that creates a new array object

whose value is a subinterval of an existing array; the old and new objects share

the array elements in that subinterval.

Helge

Votes

Translate

Translate
Contributor ,
Apr 12, 2014 Apr 12, 2014

Copy link to clipboard

Copied

From the PLRM, 3rd edition:

As discussed earlier, an array is a composite object. When an array object is copied,

the value is not copied. Instead, the old and new objects share the same value.

Additionally, there is an operator (getinterval) that creates a new array object

whose value is a subinterval of an existing array; the old and new objects share

the array elements in that subinterval.

Helge

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 12, 2014 Apr 12, 2014

Copy link to clipboard

Copied

LATEST

In addition, try this. Composite objects (arrays, dictionaries, strings) are allocated from a potentially limited pool of memory so the programmer has full control of the memory allocation process. Only specific operators such as array, string, dict will allocate memory (and the parser as it sees literals of these types). So, if another operator returns a composite object, it follows that no memory was allocated, and it was not copied. It is therefore a reference to the original (a pointer, if you prefer, though PostScript avoids the term).

If you're a C programmer it can help to think of PostScript objects as 8 byte structures. A structure could (but would not exactly) contain the object type and simple value. For an array, the structure would contain the type and a pointer to the array memory, length and offset. The array memory is a list of 8 byte structures.

getinterval makes a new object (8 byte) containing a pointer to the same array memory, but a different length and offset.

get returns a copy of one of the 8 byte structures from the array memory. For a simple type (e.g. real) this copy is complete and separate. If the element is an array the 8 bytes are copied, but the pointer to the array memory, length and offset are unaltered. Essentially it is the same array.

Indeed, if you translated this example to C it would be the same array.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines