看看 C++新的[] cookie。这段代码的可移植性如何?

发布于 2024-08-31 16:37:48 字数 580 浏览 5 评论 0原文

我想出了这个作为调试问题的快速解决方案 - 我有指针变量及其类型,我知道它指向堆上分配的对象数组,但我不知道有多少个。所以我写了这个函数来查看在堆上分配内存时存储字节数的cookie。

template< typename T >
int num_allocated_items( T *p )
{
return *((int*)p-4)/sizeof(T);
}

//test
#include <iostream>
int main( int argc, char *argv[] )
{
    using std::cout; using std::endl;
    typedef long double testtype;
    testtype *p = new testtype[ 45 ];

    //prints 45
    std::cout<<"num allocated = "<<num_allocated_items<testtype>(p)<<std::endl;
    delete[] p;
    return 0;
}

我想知道这段代码的可移植性如何。

I came up with this as a quick solution to a debugging problem - I have the pointer variable and its type, I know it points to an array of objects allocated on the heap, but I don't know how many. So I wrote this function to look at the cookie that stores the number of bytes when memory is allocated on the heap.

template< typename T >
int num_allocated_items( T *p )
{
return *((int*)p-4)/sizeof(T);
}

//test
#include <iostream>
int main( int argc, char *argv[] )
{
    using std::cout; using std::endl;
    typedef long double testtype;
    testtype *p = new testtype[ 45 ];

    //prints 45
    std::cout<<"num allocated = "<<num_allocated_items<testtype>(p)<<std::endl;
    delete[] p;
    return 0;
}

I'd like to know just how portable this code is.

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

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

发布评论

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

评论(3

那小子欠揍 2024-09-07 16:37:48

它甚至不能远程携带。

实现可以按照需要执行堆簿记,并且绝对没有办法可移植地获取堆分配的大小,除非您自己跟踪它(这是您应该做的)。

It is not even remotely portable.

An implementation can perform heap bookkeeping however it wants and there is absolutely no way to portably get the size of a heap allocation unless you keep track of it yourself (which is what you should be doing).

假装不在乎 2024-09-07 16:37:48

您可以全局重载数组上的 new/delete 运算符,并将大小放入内存区域。您将获得一个便携式解决方案。

下面的代码显示了如何:

void * operator new [] (size_t size)
{
    void* p = malloc(size+sizeof(int));
    *(int*)p = size;
    return (void*)((int*)p+1);
}

void operator delete [] (void * p)
{
    p = (void*)((int*)p-1);
    free(p);
}

template<typename T>
int get_array_size(T* p)
{
    return *((int*)p-1)/sizeof(T);
}


int main(int argc, char* argv[])
{
    int* a = new int[200];
    printf("size of a is %d.\n", get_array_size(a));
    delete[] a;
    return 0;
}

结果:

   size of a is 200.

You can globally overload new/delete operators on array, and put the size into the memory area. You get a portable solution.

The below code shows how:

void * operator new [] (size_t size)
{
    void* p = malloc(size+sizeof(int));
    *(int*)p = size;
    return (void*)((int*)p+1);
}

void operator delete [] (void * p)
{
    p = (void*)((int*)p-1);
    free(p);
}

template<typename T>
int get_array_size(T* p)
{
    return *((int*)p-1)/sizeof(T);
}


int main(int argc, char* argv[])
{
    int* a = new int[200];
    printf("size of a is %d.\n", get_array_size(a));
    delete[] a;
    return 0;
}

Result:

   size of a is 200.
Spring初心 2024-09-07 16:37:48

不便携。但为什么不使用 std::vector 呢?然后你可以直接询问它包含多少个元素,而无需担心内存管理和异常安全。

Not portable. But why not use std::vector? Then you can ask it directly how many elements it contains, and you won't need to worry about memory management and exception safety.

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