如图所示,在结构内定义常量数据是否有任何特殊意义.这是来自第三方图书馆.
typedef struct
{
IntVB abc_number;
#define ABC_A 0x01
#define ADBC_E 0x02
IntVB asset;
} StructA;
Run Code Online (Sandbox Code Playgroud) 我有以下ASM代码(OllyDbg),它应该包含用c开发的结构.有人能告诉我结构在c编程语言中是怎么样的吗?也许稍微解释一下你如何弄清楚结构中存储的内容等等......
非常感谢你!
CPU Disasm
Address Hex dump Command Comments
6A27F058 /$ 68 E9A6286A PUSH 6A28A6E9 ; Entry point
6A27F05D |. 64:FF35 00000 PUSH DWORD PTR FS:[0]
6A27F064 |. 8B4424 10 MOV EAX,DWORD PTR SS:[ESP+10]
6A27F068 |. 896C24 10 MOV DWORD PTR SS:[ESP+10],EBP
6A27F06C |. 8D6C24 10 LEA EBP,[ESP+10]
6A27F070 |. 2BE0 SUB ESP,EAX
6A27F072 |. 53 PUSH EBX
6A27F073 |. 56 PUSH ESI
6A27F074 |. 57 PUSH EDI
6A27F075 |. A1 E067336A MOV EAX,DWORD PTR DS:[6A3367E0]
6A27F07A |. 3145 FC …Run Code Online (Sandbox Code Playgroud) 字节优化给你带来多大的性能提升(使它们成为8,32,64等的倍数......)?
这是一个示例结构:
[StructLayout(LayoutKind.Explicit)]
public struct RenderItem
{
[FieldOffset(0)] byte[] mCoordinates = new byte[3]; //(x,y,z)
[FieldOffset(3)] short mUnitType;
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,做这样的事情有多重要:
[StructLayout(LayoutKind.Explicit)]
public struct RenderItem
{
[FieldOffset(0)] byte[] mCoordinates = new byte[3]; //(x,y,z)
[FieldOffset(4)] short mUnitType;
[FieldOffset(6)] byte[] mPadding = new byte[2]; //make total to 8 bytes
}
Run Code Online (Sandbox Code Playgroud)
我敢肯定它是那些"随尺寸扩展"的东西之一,所以特别是我很好奇这个结构会被用来创建一个VertexBuffer对象大约150,000次:
//int objType[,,] 3 dimensional int with object type information stored in it
int i = 0;
RenderItem vboItems[16 * 16 * 16 * 36] //x - 16, y - 16, z - 16, …Run Code Online (Sandbox Code Playgroud) 我使用以下程序来交换矩形结构的长度和宽度
typedef struct rectangle
{
int len;
int wid;
} rect;
void swap(int* a, int * b)
{
int temp;
temp= *a;
*a=*b;
*b=temp;
}
int main()
{
rect rect1;
rect *r1;
r1= &rect1;
r1->len=10;
r1->wid=5;
cout<< "area of rect " << r1->len * r1->wid<<endl;
swap(&r1->len,&r1->wid);
cout<< "length=" << rect1.len<<endl;
cout<<"width=" <<rect1.wid;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我使用以下内容时:
swap(r1->len,r1->wid);
Run Code Online (Sandbox Code Playgroud)
代替:
swap(&r1->len,&r1->wid);
Run Code Online (Sandbox Code Playgroud)
我仍然得到正确的结果,我不确定它是如何工作的.根据我的理解,我应该使用(&r1->)将成员变量的地址传递给函数.有人可以解释一下吗?
我只是想说我仍在学习C ++,所以我从关于结构的模块开始,虽然我不了解所有内容,但我认为我的理解是正确的。编译器不断给我的错误是:
错误:“。”之前的预期主表达式 令牌|
#include <iostream>
#include <fstream>
using namespace std;
struct catalog
{
char title[50];
char author[50];
char publisher[30];
int yearpublish;
double price;
};
int main()
{
catalog book;
char again;
fstream fbook;
fbook.open("book.dat", ios::out | ios::binary);
do
{
//Read data about the book
cout << "Enter the following data about a book:\n";
cout << "Title:";
cin.getline (book.title,50);
cout << "Author:";
cin.getline (book.author,50);
cout << "Publisher name:";
cin.getline (book.publisher,30);
cout << "Year publish:";
cin >> book.yearpublish;
cin.ignore();
cout << …Run Code Online (Sandbox Code Playgroud) 在Matlab中,我有S一些带有一些字段的标量结构.每个字段都包含一个数字向量,并且所有这些向量都具有相同的大小nx1.
现在我想根据选择的字段创建一个数字矩阵.
起点是一个大小合理的逻辑掩码mx1,其中m是字段数S.mask(i)是true如果的第i个场S应被包括在基体中.所以矩阵大小就是n x sum(mask).
示例(在我的代码中,结构不是以这种方式构建的,当然:-)
vec = rand(1000,1);
S.f1 = vec;
S.f2 = vec;
S.f3 = vec;
S.f4 = vec;
S.f5 = vec;
mask = [false true true false false]; % 5 elements because S has 5 fields
Run Code Online (Sandbox Code Playgroud)
预期的产出是:
output = [S.f2 S.f3];
Run Code Online (Sandbox Code Playgroud)
但是,当然,创建output应该依赖于字段S和动态mask.
有没有办法实现这一点,而不使用丑陋的结构,包括结构字段名称,循环等的过滤器?
非常感谢你!
菲利普
我想知道如何检索实例的父结构.
我不知道如何实现这一点.
例如:
type Hood struct {
name string
houses []House
}
type House struct {
name string
people int16
}
func (h *Hood) addHouse(house House) []House {
h.houses = append(h.houses, house)
return h.houses
}
func (house *House) GetHood() Hood {
//Get hood where the house is situated
return ...?
}
Run Code Online (Sandbox Code Playgroud)
干杯
有谁知道为什么成员Node_ptr next;生成数组的元素poly[1]并poly[2]显示错误的值?如果我Node_ptr next;从结构(struct node)中删除 ,我能够获得索引1和2的正确值.
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct node *Node_ptr;
struct node {
int coef;
int exp;
Node_ptr next;
};
int main()
{
struct node p1_terms[] = {10, 1000, 5, 14, 1, 0};
struct node p2_terms[] = {3, 1990, 2, 1492, 11, 5};
struct node poly[20];
poly[0] = p1_terms[0];
poly[1] = p1_terms[1];
poly[2] = p1_terms[2];
printf("Your polynomials are: \n%dx^%d+%dx^%d+%dx^%d", poly[0].coef, poly[0].exp, poly[1].coef, poly[1].exp, poly[2].coef, poly[2].exp);
int …Run Code Online (Sandbox Code Playgroud) 我创建了一个名为Register的结构,其中包含大约8个字段.我现在想要创建一个名为Instrument的结构,它应该具有可变数量的字段,6对于每个乐器都是相同的,加上一定数量的字段取决于归属于它的寄存器数量.我该如何创建呢?
为清楚起见,这是我想要创建的内容(虽然可能不准确).
typedef struct {
int x;
int y;
int z;
} Register;
typedef struct {
int x;
int y;
int z;
Register Reg1;
Register Reg2;
...
} Instrument;
Run Code Online (Sandbox Code Playgroud) 以下结构化绑定代码在clang上运行良好.现场演示
但是,它在GCC编译器上失败了.现场演示
#include <iostream>
struct st {
bool b = true;
};
template <class T>
bool func() noexcept {
auto [a] = T{};
return a;
}
int main() {
const bool b1 = func<st>();
}
Run Code Online (Sandbox Code Playgroud)
为什么结构化绑定在GCC上失败?