Sizeof C 整数数组(在 Obj-C 中)

发布于 2024-10-14 11:42:52 字数 611 浏览 2 评论 0原文

myclass.h:

#define BUTTON_NAVI 41;
#define BUTTON_SETTINGS 42;
#define BUTTON_INFO 43;

myclass.m:

int btnNavi = BUTTON_NAVI;
int btnSettings = BUTTON_SETTINGS;
int btnArray[2] = {btnNavi, btnSettings};
NSLog(@"count = %i", sizeof(btnArray));
[self addToolbarButtons:btnArray];

->日志:计数 = 8

8?!我做错了什么?

在“addToolbarButtons”内部计数为 4...:-(

编辑:

- (void)addToolbarButtons:(int[])buttonIdArray {
    NSLog(@"count = %i", sizeof(buttonIdArray));
}

-> 日志:计数 = 4

myclass.h:

#define BUTTON_NAVI 41;
#define BUTTON_SETTINGS 42;
#define BUTTON_INFO 43;

myclass.m:

int btnNavi = BUTTON_NAVI;
int btnSettings = BUTTON_SETTINGS;
int btnArray[2] = {btnNavi, btnSettings};
NSLog(@"count = %i", sizeof(btnArray));
[self addToolbarButtons:btnArray];

-> Log: count = 8

8?! What did I do wrong?

And inside "addToolbarButtons" count is 4... :-(

EDIT:

- (void)addToolbarButtons:(int[])buttonIdArray {
    NSLog(@"count = %i", sizeof(buttonIdArray));
}

-> Log: count = 4

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

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

发布评论

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

评论(3

颜漓半夏 2024-10-21 11:42:52

sizeof 给出了以字节为单位的大小,8 个字节听起来适合 2 个整数(每个 32 位或 4 个字节)。

如果您想要的是数组的长度,您可以执行sizeof(arr) / sizeof(arr[0]),这将为您提供整个数组的大小除以每个元素的大小。在这种情况下,您将得到 8 / 4 == 2,这正是我所期望的。

编辑
为了回答你的第二个问题,当你将数组传递给方法时,你实际上是在传递一个指向数组的指针。因此,指针的大小也是 32 位或 4 字节。如果您希望函数知道数组的长度,则还需要将其长度与所述指针一起传递。

sizeof is giving you the size in bytes, 8 bytes sounds right for 2 integers (32-bit or 4 bytes each).

If what you want is the length of the array, you can do sizeof(arr) / sizeof(arr[0]) which will give you the size of the entire array divided by the size of each element. In this case you will get 8 / 4 == 2, which is what I take it you expect.

EDIT
To answer your second question, when you pass the array to the method, you're actually passing a pointer to the array. Hence, the size of the pointer is also 32-bits or 4 bytes. If you want the function to know the length of the array, you need to also pass its length along with said pointer.

好久不见√ 2024-10-21 11:42:52

sizeof 给出数组的大小(以字节为单位)。一个 int 是 4 个字节,因此 2 元素的 int 数组将是 8 个字节。

但是,sizeof 不会在您的方法中执行您想要的操作。当 C 数组传递到函数(或方法)时,它实际上是作为指针传递的,并且 sizeof 将返回指针的大小。您应该修改您的方法以采用长度参数:

- (void)addToolbarButtons:(int *)buttonIdArray length:(size_t)len
{
    NSLog(@"count = %d", len);
}

sizeof is giving the size of the array in bytes. An int is 4 bytes, so a 2-element array of ints will be 8 bytes.

However, sizeof won't do what you want in your method. When a C array is passed into a function (or method), it actually get passed as a pointer, and sizeof will return the size of a pointer. You should modify your method to take a length parameter:

- (void)addToolbarButtons:(int *)buttonIdArray length:(size_t)len
{
    NSLog(@"count = %d", len);
}
就是爱搞怪 2024-10-21 11:42:52

sizeof 运算符给出传递给它的内容的大小(以字节为单位)。如果一个 int 是 4 个字节,那么两个 int 的数组就是 8 个字节。

然而,sizeof 是对变量本身的编译时检查。在运行时,数组的边界是未知的——在定义变量的范围之外也不知道它们。当您声明参数 (int[])buttonIdArray 时,编译器会将其脱糖为 (int *)buttonArray — 它只是一个普通的 int 指针。因此,当您执行 sizeof(buttonArray) 时,它会告诉您指针的大小,4。

因为该语言不会为您跟踪它们的大小,所以使用 C 数组很痛苦。您必须将数组中的元素数量传递给对其进行操作的每个函数和方法。

The sizeof operator gives you the size in bytes of the thing you pass to it. If an int is 4 bytes, then an array of two ints is 8 bytes.

However, sizeof is a compile-time check on the variable itself. At runtime, the bounds of an array are not known — nor are they known outside the scope where the variable was defined. When you declare the argument (int[])buttonIdArray, the compiler desugars that to (int *)buttonArray — it's just a plain int pointer. So when you do sizeof(buttonArray), it tells you the size of a pointer, 4.

Because the language doesn't keep track of their size for you, using C arrays is a pain. You have to pass the number of elements in the array to every function and method that acts on it.

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