首页 文章
  • 582 votes
     answers
     views

    为什么structof sizeof不等于每个成员的sizeof总和?

    为什么 sizeof 运算符返回的结构大小大于结构成员的总大小?
  • 478 votes
     answers
     views

    为什么sizeof(x)不会增加x?

    这是在dev c windows中编译的代码: #include <stdio.h> int main() { int x = 5; printf("%d and ", sizeof(x++)); // note 1 printf("%d\n", x); // note 2 return 0; } 执行 not...
  • 0 votes
     answers
     views

    sizeof运算符在C中的工作原理?

    在下面的代码中: #include<stdio.h> int main(void) { printf("%d",sizeof(int)); return 0; } 在gcc(Ubuntu 4.8.4-2ubuntu1~14.04.3)4.8.4编译器上编译时,它会发出警告: format'%d'需要类型为'int'的参数,但参数2的类型为'lon...
  • 122 votes
     answers
     views

    为什么sizeof(my_arr)[0]编译并且sizeof(my_arr [0])?

    为什么这段代码会编译? _Static uint32_t my_arr[2]; _Static_assert(sizeof(my_arr) == 8, ""); _Static_assert(sizeof(my_arr[0]) == 4, ""); _Static_assert(sizeof(my_arr)[0] == 4, ""); ...
  • 12 votes
     answers
     views

    sizeof结构成员[重复]

    这个问题在这里已有答案: sizeof single struct member in C 9个答案 如何在C中获取结构中成员的大小? struct A { char arr[64]; }; 我需要这样的东西: 的sizeof(A :: ARR) 谢谢
  • 11 votes
     answers
     views

    为什么结构的sizeof是不安全的

    MSDN明确指出 对于所有其他类型(包括结构),sizeof运算符只能用于不安全的代码块 . C# Language Specification更精确: 未指定成员打包到结构中的顺序 . 出于对齐目的,在结构的开头,结构内和结构的末尾可能存在未命名的填充 . 用作填充的位的内容是不确定的 . 当应用于具有结构类型的操作数时,结果是该类型变量中的总字节数,包括任何填充 . 但...
  • 37 votes
     answers
     views

    ISO / IEC在sizeof(char)中混淆不同的字符集编码,如UTF-16

    假设程序在具有UTF-16编码字符集的系统上运行 . 所以根据C编程语言 - 第4页,第150页: char可以保存机器字符集的字符 . →我认为char变量的大小是2字节 . 但根据ISO / IEC 14882:2014: sizeof(char),sizeof(signed char)和sizeof(unsigned char)是1“ . 或C语言程序设计 - 第4页,第149页: ...
  • 472 votes
     answers
     views

    如何在Python中确定对象的大小?

    在C中,我们可以找到 int , char 等的大小 . 我想知道如何在Python中获取对象的大小,如字符串,整数等 . 相关问题:How many bytes per element are there in a Python list (tuple)? 我使用的XML文件包含指定值大小的大小字段 . 我必须解析这个XML并进行编码 . 当我想更改特定字段的值时,我将检查该值的大小字段 . 在...
  • 21 votes
     answers
     views

    使用带有数组和指针的sizeof的实验

    对于该计划: #include<stdio.h> int main(void) { int (*a)[2]; int b[5]; printf("sizeof(int) : %zu\n", sizeof(int)); printf("sizeof(int*) : %zu\n", sizeof(int*)); ...
  • 582 votes
     answers
     views

    为什么structof sizeof不等于每个成员的sizeof总和?

    为什么 sizeof 运算符返回的结构大小大于结构成员的总大小?
  • 129 votes
     answers
     views

    为什么“sizeof(a?true:false)”给出四个字节的输出?

    我有一小段关于带有三元运算符的 sizeof 运算符的代码: #include <stdio.h> #include <stdbool.h> int main() { bool a = true; printf("%zu\n", sizeof(bool)); // Ok printf("%zu\n", si...
  • 0 votes
     answers
     views

    分配适当的内存大小

    我在程序中分配正确大小的内存时遇到问题 . 我做以下事情: void * ptr = sbrk(sizeof(void *)+sizeof(unsigned int)); 当我这样做时,我认为它为堆添加了太多内存,因为它以void *而不是bytes为单位进行分配 . 我如何告诉它我想要sizeof(无论如何)意味着什么字节而不是其他任何单位? 编辑: 我已经看到其他人将事物转换为char,以便...
  • 0 votes
     answers
     views

    链表中Next指针的大小?

    Hello All给出了一个链表节点, struct node { int data; struct node* next; }; 考虑使用4个字节的int . 指针 next 的大小是多少? 我也有以下, void * ptr; printf("%d",sizeof(ptr)); 指针的大小是8个字节 . 我得到sizeof(struct node)为12,给定struc...
  • 8 votes
     answers
     views

    初始化后你可以调整C数组的大小吗? [重复]

    这个问题在这里已有答案: How to resize array in C++? 5个答案 我正在学习编程,C是我的第一语言 . 不要打扰使用指针来展示我 - 我还不了解它们,并且在我有更多的空闲时间专注于此之前不会打扰 . int mergeSort() { const int n = 9; int originalarray[n] = {1, 3, 5, 7, 9, 2, ...
  • 19 votes
     answers
     views

    如何获得vector :: value_type的sizeof?

    我想得到一个包含在向量中的 sizeof 类型 . 这是我尝试过的: #include <iostream> #include <vector> int main() { std::vector<uint> vecs; std::cout << sizeof(vecs.value_type) << std::endl; ...
  • 5 votes
     answers
     views

    使用sizeof在argv中查找字符串的大小

    在这一点上,这更像是一个概念性的问题,而不是一个实际问题,但它确实困扰着我 . 让我们说我有一个名为“test.c”的c程序,我想找到数组中的空格数,用户输入的单词作为参数 . 例如,“ . / test.c test_run”应该打印9,因为有8个字符,然后一个用于空终止字符 . 当我尝试在argv上使用sizeof时虽然我遇到了麻烦 . int main(int argc, char *ar...
  • 1 votes
     answers
     views

    类型和内存分配的大小[重复]

    这个问题在这里已有答案: Why isn't sizeof for a struct equal to the sum of sizeof of each member? 11个答案 我在动态内存分配方面遇到了麻烦 . 不知何故,我的struct的实际大小(所有部分的总和)小于类型本身的大小 . 这是代码: #include <stdio.h> #include <mallo...
  • 88 votes
     answers
     views

    有机器,sizeof(char)!= 1,或者至少CHAR_BIT> 8?

    是否有机器(或编译器), sizeof(char) != 1 ? C99 standard 是否认为标准合规实施的 sizeof(char) 必须正好为1?如果有,请给我部分编号和引文 . 更新:如果我有一台机器(CPU),它不能解决字节(最小读取是4个字节,对齐),但只有4-s的字节( uint32_t ),这台机器的编译器可以将sizeof(char)定义为4 ? sizeof(char) ...
  • 4 votes
     answers
     views

    C指定Struct的实际大小

    我遇到了C#和C之间的互操作问题,我通过本机代码和托管代码中定义的结构在我的应用程序的两个“端”之间共享内存 . 本机端的结构定义如下: #pragma pack(push, 1) struct RayTestCollisionDesc { btVector3 hitPosition; btRigidBody* hit...
  • 2 votes
     answers
     views

    K&R哈希函数

    以下代码的一部分是K&R的简单哈希查找算法的实现 . lookup在表中搜索s,并返回指向找到它的位置的指针,如果不存在,则返回NULL: struct hashElement *lookup(char *name){ struct hashElement *currentStruct; int cmp; for (currentStruct = hashTable[ca...

热门问题