如何从在C中的指针获取数组的大小? [英] How can I get the size of an array from a pointer in C?

查看:208
本文介绍了如何从在C中的指针获取数组的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经分配的的数组 MYSTRUCT 尺寸 N 是这样的:

I've allocated an "array" of mystruct of size n like this:

if (NULL == (p = calloc(sizeof(struct mystruct) * n,1))) {
 /* handle error */
}

后来,我只能访问 P ,再也不用 N 。有没有一种方法来确定只给定指针数组的长度 P

Later on, I only have access to p, and no longer have n. Is there a way to determine the length of the array given just the pointer p?

我想它的必须的可能,因为免费(P)就是这样做的。我知道的malloc()跟踪它多少内存分配,这就是为什么它知道的长度;也许有一种方法来查询这些信息?喜欢的东西...

I figure it must be possible, since free(p) does just that. I know malloc() keeps track of how much memory it has allocated, and that's why it knows the length; perhaps there is a way to query for this information? Something like...

int length = askMallocLibraryHowMuchMemoryWasAlloced(p) / sizeof(mystruct)

我知道我应该只是返修code,让我知道 N ,但我宁愿不如果可能的话。任何想法?

I know I should just rework the code so that I know n, but I'd rather not if possible. Any ideas?

推荐答案

没有,有没有办法得到这个信息,而在的malloc 实施细则强烈依赖。尤其是,的malloc 比您请求(例如,对于效率在一个特定的内存架构)可以分配更多的字节。这将是更好的重新设计code,让您跟踪 n个明确。另一种方法是至少的尽可能多的重新设计和更为危险的方法(因为它的不规范,滥用指针的语义,将是一个维护的噩梦对于那些后你来):店长度 N 在malloc分配的地址,其次是数组。然后是分配将是:

No, there is no way to get this information without depending strongly on the implementation details of malloc. In particular, malloc may allocate more bytes than you request (e.g. for efficiency in a particular memory architecture). It would be much better to redesign your code so that you keep track of n explicitly. The alternative is at least as much redesign and a much more dangerous approach (given that it's non-standard, abuses the semantics of pointers, and will be a maintenance nightmare for those that come after you): store the lengthn at the malloc'd address, followed by the array. Allocation would then be:

void *p = calloc(sizeof(struct mystruct) * n + sizeof(unsigned long int),1));
*((unsigned long int*)p) = n;

N 现在存储在 *((unsigned long int类型*)P)和阵列的开始现在

n is now stored at *((unsigned long int*)p) and the start of your array is now

void *arr = p+sizeof(unsigned long int);

编辑:只是起到唱反调......我知道这些的解决方案都需要重新设计,但让我们发挥出来。
当然,上述psented解决$ P $只是一个哈克实施(以及包装)结构的。你还不如定义:

Just to play devil's advocate... I know that these "solutions" all require redesigns, but let's play it out. Of course, the solution presented above is just a hacky implementation of a (well-packed) struct. You might as well define:

typedef struct { 
  unsigned int n;
  void *arr;
} arrInfo;

和绕过 arrInfo 取值,而不是原始指针。

and pass around arrInfos rather than raw pointers.

现在我们正在做饭。但只要你重新设计了,为什么停在这里?你真正想要的是一个抽象数据类型(ADT)。一个算法和数据结构类的任何介绍文字会做到这一点。一个ADT定义数据类型的公共接口,但隐藏的数据类型的实现。因此,对于公开数组中的ADT可能看起来像

Now we're cooking. But as long as you're redesigning, why stop here? What you really want is an abstract data type (ADT). Any introductory text for an algorithms and data structures class would do it. An ADT defines the public interface of a data type but hides the implementation of that data type. Thus, publicly an ADT for an array might look like

typedef void* arrayInfo;
(arrayInfo)newArrayInfo(unsignd int n, unsigned int itemSize);
(void)deleteArrayInfo(arrayInfo);
(unsigned int)arrayLength(arrayInfo);
(void*)arrayPtr(arrayInfo);
...

在换句话说,ADT是数据和行为封装的一种形式......换句话说,这是关于尽可能靠近你可以到面向对象的使用直C.编程除非你被困在一个平台上的没有一个C ++编译器,你还不如去养猪整体,只是使用STL 的std ::矢量

In other words, an ADT is a form of data and behavior encapsulation... in other words, it's about as close as you can get to Object-Oriented Programming using straight C. Unless you're stuck on a platform that doesn't have a C++ compiler, you might as well go whole hog and just use an STL std::vector.

有,我们已经采取了关于C简单的问题,并结束了在C ++。上帝帮助我们。

There, we've taken a simple question about C and ended up at C++. God help us all.

这篇关于如何从在C中的指针获取数组的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆