您是否遇到过三级间接的任何原因?

发布于 2024-07-10 07:12:08 字数 220 浏览 3 评论 0原文

翻阅我最喜欢的一本书(Ellen Ullman 的《The Bug》),有一小段内容,一个程序员在三个间接级别上与另一个程序员对峙:

***object_array = ***winarray;

我得到了双重间接的想法 - 一种将指针传递到函数的方法,并且允许它指向函数内创建的对象。

但是您是否遇到过使用三层(或更多)间接层的任何理由?

Just flicking through one of my favourite books (Ellen Ullman's The Bug) and there is a small bit where one programmer confronts another over three levels of indirection:

***object_array = ***winarray;

I get the idea of double indirection - a way of passing a pointer into a function, and allowing it to point to an object created within the function.

But have you come across any reason to use three (or more) levels of indirection?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冷月断魂刀 2024-07-17 07:12:08

当然
4 维数组。
对于这样的数组的应用程序来说也不需要太多。 说某种查找表。 我有 8 个或更多维度的查找表。

Sure
4 dimensional arrays.
It doesn't take too much for an application for such an array either. Say some sort of lookup table. I've had lookup tables of 8 and more dimensions.

谁与争疯 2024-07-17 07:12:08

正如 David Wheele 所说:“计算机科学中的任何问题都可以通过另一层间接解决。” 几乎可以肯定,您已经使用了这样的一行的三层间接:

int x = 3;

毕竟,芯片是通过两层 L1 和 L2 缓存来间接访问内存的。 操作系统通过虚拟内存页间接访问内存。 您的 C# 编译器通过虚拟机中的对象间接访问内存。 当然,它没有一长串星号,但那是因为所有这些间接都是用机器、操作系统或编译器等事物抽象出来的。

As David Wheele said: "Any problem in computer science can be solved with another layer of indirection." You have almost certainly used three layers of indirection with a line like this:

int x = 3;

After all, the chip is indirecting memory access through two layers of L1 and L2 cache. And the OS is indirecting the memory access via virtual memory pages. And your C# compiler is indirecting the memory access via objects in a virtual machine. Sure, it doesn't come with a long line of asterixes, but that's because all of these indirections are abstracted with things liks a machine, an OS or a compiler.

清泪尽 2024-07-17 07:12:08

您现在可能正在运行 3 个或更多级别。 可能是在 Mac(或 VM)上的 Windows 中运行的浏览器中的 javascript 上的 jQuery,通过在...中运行的远程访问。

或者,从更接近您的问题上下文的另一个角度来看,3 个级别是我们最常见的工件。

什么是指向窗口容器中控件的指针?

You're possibly running at 3 or more levels right now. Could be jQuery on javascript in a browser running in Windows on a Mac (or in a VM) via Remote Access running in ....

Or, from another perspective closer to your question's context, 3 levels is the most common artifact we have.

What's a pointer to a control in a container in a Window?

过去的过去 2024-07-17 07:12:08

不,我从未真正见过或使用过它(据我所知,至少在没有合理的 typedef 来使其不那么令人困惑的情况下),但我可以设计一个可能是[可疑]有效使用的示例:

struct Foo{
    struct greater{
        bool operator()(Foo const *a, Foo const *b) const{
            return a->place > b->place ||
                   a->place == b->place && a->holder > b->holder;
        }
    };

    int place;
    int holder;
};

template<typename T, typename Comparer>
void Sort(T const *unorderedList, int count, T const ***orderedList, Comparer &cmp);

void UseOrderedList(Foo const **orderedList, int count);

int main(){
    Foo list[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
    Foo const **orderedList;

    Sort(list, sizeof list / sizeof *list, &orderedList, Foo::greater());
    UseOrderedList(orderedList, sizeof list / sizeof *list);
    delete[] orderedList;
    return 0;
}

void UseOrderedList(Foo const **orderedList, int count){/*...*/}

template<typename T, typename Comparer>
void Sort(T const *unorderedList, int count, T const ***orderedList, Comparer &cmp){
    /*
     * The result array stores pointers to the items in the original array.
     * This way, the original array is unmodified, and the result array
     * doesn't create duplicate items.  This makes sense if the objects
     * are large and copying them would be slow (another argument against
     * in-place sorting), or if duplicating them violates some design
     * principle.
     */
    *orderedList = new const T*[count];

    for(int i = 0; i < count; i++)
        (*orderedList)[i] = unorderedList + i;

    std::sort(*orderedList, &(*orderedList)[count], cmp);
}

我实际上不会做我在这里所做的事情。 这只是一个如何最终获得三个指针级别的示例。 不过,我无法想象您经常遇到这种情况。

No, I've never actually seen or used it (as far as I can recall, and at least not without sensible typedefs to make it less fubar), but I can contrive an example of what might be a [questionably] valid use:

struct Foo{
    struct greater{
        bool operator()(Foo const *a, Foo const *b) const{
            return a->place > b->place ||
                   a->place == b->place && a->holder > b->holder;
        }
    };

    int place;
    int holder;
};

template<typename T, typename Comparer>
void Sort(T const *unorderedList, int count, T const ***orderedList, Comparer &cmp);

void UseOrderedList(Foo const **orderedList, int count);

int main(){
    Foo list[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
    Foo const **orderedList;

    Sort(list, sizeof list / sizeof *list, &orderedList, Foo::greater());
    UseOrderedList(orderedList, sizeof list / sizeof *list);
    delete[] orderedList;
    return 0;
}

void UseOrderedList(Foo const **orderedList, int count){/*...*/}

template<typename T, typename Comparer>
void Sort(T const *unorderedList, int count, T const ***orderedList, Comparer &cmp){
    /*
     * The result array stores pointers to the items in the original array.
     * This way, the original array is unmodified, and the result array
     * doesn't create duplicate items.  This makes sense if the objects
     * are large and copying them would be slow (another argument against
     * in-place sorting), or if duplicating them violates some design
     * principle.
     */
    *orderedList = new const T*[count];

    for(int i = 0; i < count; i++)
        (*orderedList)[i] = unorderedList + i;

    std::sort(*orderedList, &(*orderedList)[count], cmp);
}

I wouldn't actually do what I've done here. It's just an example of how you could end up with three pointer levels. I can't imagine you running into this kind of scenario very often, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文