Giter Site home page Giter Site logo

Comments (2)

radfordneal avatar radfordneal commented on July 23, 2024

This is closely related to issue #42, which involved this code:

a <- structure ( c , b = c )
attributes ( a )

so I'll address them both here.

from pqr.

radfordneal avatar radfordneal commented on July 23, 2024

There are two issues here. One is what sort of object these "structure" calls create. The other is what happens when you try to print them.

Regarding the first issue, note that "structure" isn't a primitive, so I'll look at the equivalent with "attrib" just to avoid any confusion from the details of "structure" (but I think there's no real difference). It seems that the same sort of object gets created by pqR-2020-07-23 and R-4.0.2. One thing to note is that primitive functions in both pqR-2020-07-23 and R-4.0.2 cannot be copied, so the attribute gets put on all instances of them (which are really the same instance).

Here's an example an with ordinary object (same happens with an ordinary function created with "function"):

> a<-1; attr(a,"fred")<-a; attributes(a)
$fred
[1] 1

The 1 gets duplicated before being attached as an attribute, so the structure is not recursive.

Here's an example with a primitive function (same result in pqR-2020-07-23 and R-4.0.2):

> x <- c; attr(x,"fred") <- 1
> attributes(x)
$fred
[1] 1

> attributes(c)
$fred
[1] 1

Attaching an attribute to x attaches the attribute to c as well, because the primitive function c can't be duplicated.

When you attach c as an attribute to itself, a recursive structure is created, because it can't be copied before being attached. For example:

> attr(c,"fred") <- c

The print representation of this is infinite.

One could argue that it should be possible to duplicate primitive functions, or that attaching attributes to them should not be allowed (as it is not allowed for NULL, which also can't be duplicated). But that's not the way it is in both pqR-2020-07-23 and R-4.0.2. I'd advise against writing code that relies on this behaviour, since it seems possible that it may change (as it did for NULL not too long ago).

So the second question is what happens when you try to print one of these recursive objects with an infinite print representation.

One difference is that pqR doesn't always print the attribute, while R-4.0.2 does. So in pqR-2020-07-23 you get the following:

> attr(c,"fred") <- c
> c
function (..., recursive = FALSE)  .Primitive("c")

pqR didn't attempt to print the attributes on the primitive function c, so the problem doesn't arise. But R-4.0.2 does print the attributes, and starts printing an infinite representation, until it gets a buffer overlow (but doesn't crash).

However, if you explicitly ask for the attributes, pqR will try to print them:

> attr(c,"fred") <- c
>attributes(c)
$fred
function (..., recursive = FALSE)  .Primitive("c")
attr(,"fred")
function (..., recursive = FALSE)  .Primitive("c")
attr(,"fred")attr(,"fred")
function (..., recursive = FALSE)  .Primitive("c")
[ and so forth ]

which goes on until pqR gets buffer overflow (and crashes).

So the only remaining question is why pqR crashes rather than just printing a buffer overflow message and stopping with an error. That's a bug, which I'll try to track down. It may also arise in other contexts where for some reason a really long print representation gets created.

from pqr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.