大家好,
我想知道下面结构的大小是多少.根据我的计算它应该是20.还有一件事是这种结构有什么方法考虑它的变量t的大小?
请忽略任何语法错误,我在32位机器上
struct structc
{
char c;
double d;
int s;
} t;
main()
{
printf("sizeof(structc_t) = %d\n", sizeof(t));
}
Run Code Online (Sandbox Code Playgroud) 为什么输出8?
不是sizeof(int)+sizeof(char) = 5吗?
class CBase
{
int a;
char p;
};
int main() {
cout<<"sizeof(CBase)="<<sizeof(CBase)<<endl;
getchar();
} ///:~
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
#include <string.h>
#include <malloc.h>
struct Student {
char name[100];
int id;
char major[50];
char address[200];
};
int main()
{
struct Student st;
strcpy(st.name, "Chuck");
st.id = 20001;
strcpy(st.major, "Computer Science");
strcpy(st.address, "1st Street");
printf("%d %d %d %d %d\n", sizeof(st), sizeof(st.name),sizeof(st.id), sizeof(st.major), sizeof(st.address));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出为356 100 4 50 200.为什么sizeof(st)356,而不是354?我尝试了这个没有int,输出是350 100 50 200,所以我假设问题在整数.
在下面的代码中我期待另一个输出!:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _cust {
char customerId[10];
char customerPhone[10];
char customerDep[4];
} cust;
int main(int argc, char **argv) {
cust *newCust;
const char testChar[] = "11W35A5CT-012345678-CORP";
newCust = (cust *)malloc(sizeof(struct _cust));
newCust = (cust *)testChar;
printf("CustomerId = %s\n", newCust->customerId);
printf("CustomerPhone = %s\n", newCust->customerPhone);
printf("CustomerDep = %s\n", newCust->customerDep);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
CustomerId = 11W35A5CT-012345678-CORP
CustomerPhone = 012345678-CORP
CustomerDep = CORP
Run Code Online (Sandbox Code Playgroud)
我期待这个输出:
CustomerId = 11W35A5CT-
CustomerPhone = 012345678-
CustomerDep = CORP
Run Code Online (Sandbox Code Playgroud)
有人能解释一下为什么这样吗?谢谢.
编辑:
为了避免混淆我的帖子,我在调试这个程序时在这里添加了gdb跟踪:
(gdb) …Run Code Online (Sandbox Code Playgroud) 在C中查找结构的大小
struct student
{
char name;
int age;
float weight;
};
main ()
{
int i,j,k,l;
struct student s1;
i=sizeof(s1.name);
j=sizeof(s1.age);
k=sizeof(s1.weight);
l=sizeof(s1);
printf ("\n size of name %d",i);
printf ("\n size of age %d",j);
printf ("\n size of weight %d",k);
printf ("\n size of s1 %d",l);
printf("\n");
}
Run Code Online (Sandbox Code Playgroud)
我的输出是:
size of name 1
size of age 4
size of weight 4
size of s1 12
但结构大小应该是其成员大小的总和.为什么我得到12而不是9作为结构变量s1的大小.有人可以解释什么是错的.
我们在课堂上给出了一个任务,对结构数组进行排序.在分配完成后,我们讨论了使用数组指针进行排序,因为它比大多数人的方式更有效.
我决定尝试这样做,但是我遇到了一些我无法解决的问题.
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
typedef struct stage2{//standard dec of the struct field
char need;
double ring;
char fight[8];
int32_t uncle;
char game;
double war;
int8_t train;
uint32_t beds;
float crook;
int32_t feast;
int32_t rabbits;
int32_t chin;
int8_t ground;
char veil;
uint32_t flowers;
int8_t adjustment;
int16_t pets;
} stage2;
void usage(){//usage method to handle areas
fprintf(stderr,"File not found\n");//prints to stderr
exit(1);//exits the program
}
int needS(const void *v1, const void *v2)
{
const stage2 …Run Code Online (Sandbox Code Playgroud) 当我用c ++做sizeof时,我一定会得到"整个对象"吗?我问,因为我要使用memcpy将对象复制到其他内存区域(从一开始可能是一个愚蠢的想法,对吧?).
我担心的是,我可能无法获得整个对象,但只有属于该类的部分现在才能获得.它有意义还是我感到困惑?
编辑示例
class A{ public: int a = 123; };
class B: public A{ public: int b = 321; };
class C : public B{ public: int c = 333; };
C c_ = C();
B b_ = C();
A a_ = C();
std::cout << sizeof(a_) << " , " << sizeof(b_) << " , " << sizeof(c_) << std::endl;
Run Code Online (Sandbox Code Playgroud)
似乎给了我4,8,12.
我想我需要做动态转换来弄清楚如何获得我在每种情况下构建为"C"类的"整体"对象?
我使用一个缓冲区来传递我的 C++ 结构
struct Node {
Node(int size, glm::ivec3 position);
bool isEmpty();
int getSubIndex(const glm::ivec3& vec);
void divide(std::vector<Node> &nodes);
void setColor(glm::vec4 color);
int getSubNodeIndex(const glm::ivec3& vec);
int getSubNodeIndex(int subIndex);
glm::ivec4 position;
glm::vec4 color;
int halfSize;
int sub;
int leaf;
};
Run Code Online (Sandbox Code Playgroud)
在着色器中看起来像这样
struct Node {
vec4 position;
vec4 color;
int data[3];
};
layout(std430, binding=4) readonly buffer Octree_data {
Node nodes[];
};
Run Code Online (Sandbox Code Playgroud)
在计算过程中,我发现数组的所有元素(除了第一个元素)都有不正确的数据(很可能是移位的),我会犯什么错误?
我对《计算机系统:程序员的视角》 (第 3 版(2015 年 10 月 6 日))一书的问题 3.44 有关数据对齐的问题有疑问。
问题:
对于以下每个结构声明,确定每个字段的偏移量,即结构的总大小,以进行 8 位对齐:
struct P1 {short i; int c; int *j; short *d}
...
struct P4 {char w[16]; char *c[2]}
struct P5 {struct P4 a[2]; struct P1 t}
Run Code Online (Sandbox Code Playgroud)
书中给出的答案:
| 我 | C | j | d | 全部的 |
|---|---|---|---|---|
| 0 | 2 | 6 | 14 | 16 |
| w | C | 全部的 |
|---|---|---|
| 0 | 16 | 32 |
| A | t | 全部的 |
|---|---|---|
| 0 | 24 | 40 |
我不明白的是为什么 did struct P4 a[2]inP5只需要 24 个字节?
既然P5.a是一个大小为2的数组P4 …
我不承担下面显示的输出.
我知道无论何时存在虚函数,它都会创建一个vptr但仍然打印的尺寸超出我的预期:
#include<iostream>
using namespace std;
class Base
{
int x;
int y;
int z;
public:
virtual void fun(){}
virtual void fun2(){}
};
class Derived:public Base
{
public:
void fun() override {}
};
int main(int argc, char const *argv[])
{
cout<<sizeof(Base)<<endl;
cout<<sizeof(Derived)<<endl;
cout<<sizeof(int)<<endl;
}
Run Code Online (Sandbox Code Playgroud)
24
24
4
[0.3秒内完成]
所以我现在是一名学生,正在进行编程.
今天我们讨论了在不同类中使用sizeof(如果它有1个int或2个int等等)
我发现奇怪的例子的一部分是这样的:
class TwoIntAndACharClass
{
public:
int x_;
int y_;
char z_;
};
Run Code Online (Sandbox Code Playgroud)
以及测试它的部分
TwoIntAndACharClass o3b;
cout << "Sizeof TwoIntAndACharClass = " << sizeof(o3b) << "\n";
Run Code Online (Sandbox Code Playgroud)
所以在程序中我可以看到一个带有1个字符的类占用了1个字节.所以当我看到这个时,我以为我会看到9个字节而不是12个字节
所以首先我认为它很奇怪,但经过一段时间我得出的结论是它可能会保存一些4字节的块.
为了100%确定这是真的,我尝试在类中添加一个新变量(一个8字节的双变量),总大小从12个字节增加到现在的24个字节.那个char现在必须是8个字节,所以我的最后一个理论失败了.
我的最后一个理论是它将采用最大的已经声明的变量并使用char变量_z的大小,因为这适用于long long int(8字节)和double(也是8字节)
所以我的问题是,我的最后一个理论是真的 - 或者它是否有所不同使得char需要更多的内存然后需要?(我的老师确实说过,每个编译器都能以不同的方式处理这个问题,但是我已经在microsoft visual studio上尝试过了,而且朋友在另一个编译器上尝试了相同的结果,但这是真的,它是编译器处理它的方式吗?)
抱歉我的英语不好.
我知道什么是填充以及对齐是如何工作的。鉴于以下结构:
typedef struct {
char word[10];
short a;
int b;
} Test;
Run Code Online (Sandbox Code Playgroud)
我不明白 C 如何解释和对齐结构内的 char 数组。它应该是 9 个字符 + 终止符,它应该被认为是最长的,如下所示:
| - _ - _ - _ - _ - word - _ - _ - _ - _ - |
| - a - | - _ - b - _ - | padding the remaining 4 bytes
Run Code Online (Sandbox Code Playgroud)
“-”代表一个字节,“_”分隔字节。所以我们有 10 字节长的字,2 字节长的 a 和 4 字节长的 b 以及 4 字节的填充。但是当我打印 sizeof(Test) 它返回16.
编辑:我明白了。
typedef struct{
short age;
int money;
char c;
}Persoana;
int main(void){
Persoana *a = malloc(sizeof(Persoana));
printf("%ld %ld",sizeof(a->money),sizeof(a->age));
printf(" %ld\n",sizeof(*a)); } ~
Run Code Online (Sandbox Code Playgroud)
代码打印"4212".4和2都可以,但如何如此12 ???