相关疑难解决方法(0)

MSVC默认内存对齐为8

根据MSDN,/Zp命令默认为8,这意味着使用64位对齐边界.我一直认为对于32位应用程序,MSVC编译器将使用32位边界.例如:

struct Test
{
   char foo;
   int bar;
};
Run Code Online (Sandbox Code Playgroud)

编译器会像这样填充它:

struct Test
{
   char foo;
   char padding[3];
   int bar;
};
Run Code Online (Sandbox Code Playgroud)

因此,/Zp8默认情况下使用,这是否意味着我的填充使用上面相同的示例变为7 + 4字节:

struct Test
{
   char foo;
   char padding1[7];
   int bar;
   char padding2[4];
}; // Structure has 16 bytes, ending on an 8-byte boundary
Run Code Online (Sandbox Code Playgroud)

这有点荒谬不是吗?我误会了吗?为什么使用如此大的填充物,似乎浪费了空间.32位系统上的大多数类型甚至不会使用64位,因此大多数变量都有填充(可能超过80%).

c++ padding memory-alignment visual-c++

5
推荐指数
1
解决办法
6111
查看次数

JPEG参数结构不匹配

在编译libjpeg然后使用它时,我偶然发现了这个问题:

JPEG parameter struct mismatch: library thinks size is 512, caller expects 492
Run Code Online (Sandbox Code Playgroud)

之后程序停止。这种情况特别是在结构struct jpeg_decompress_struct和上发生的struct jpeg_compress_struct。如果取消了对这些结构的检查,则程序将崩溃,并出现空指针异常,并且结构成员包含垃圾。

从版本6b的预编译二进制文件,或者从版本9a的预编译二进制文件,到版本9a的编译错误都会发生此错误;在Windows(32位)上使用MinGW,MSYS 1.0进行编译。在配置文件中乱七八糟没有帮助;该数字492可以更改,但决不能等于512,以使程序正常运行。

libjpeg编译确实创建了一个有效的cjpeg.exedjpeg.exe

有任何想法吗?

c struct libjpeg

5
推荐指数
1
解决办法
2682
查看次数

sizeof运算符在C#中给出了结构的额外大小

我试图使用sizeof运算符检查所有变量(值类型)的大小.我通过一个走了MSDN文章,里面写到那

对于所有其他类型(包括结构),sizeof运算符只能用于不安全的代码块

并且结构不应包含任何引用类型的字段或属性

为此,我在项目属性中启用了不安全的编译,并创建了如下结构 -

struct EmployeeStruct
{
    int empId;
    long salary;       
}
Run Code Online (Sandbox Code Playgroud)

并按如下方式使用 -

unsafe
{
   size = sizeof(EmployeeStruct);
}

Console.WriteLine("Size of type in bytes is: {0}", size);
Run Code Online (Sandbox Code Playgroud)

在这里我得到输出,因为字体大小是16:但是通过查看结构它应该是12(4表示int,8表示长).有人可以帮我理解为什么我得到4字节的额外大小?

c# struct sizeof

5
推荐指数
1
解决办法
660
查看次数

如何消除结构中未使用的元素?

背景

我正致力于嵌入式设备的模块化架构,其中不同的抽象层必须彼此通信.当前的方法是拥有大量的函数,变量和定义它们所属的模块名称.

这种方法有点痛苦,我想为API定义某种通用接口.关键的想法是更好地了解哪些模块共享相同的HAL接口.

提案

我想使用OOP启发的架构,我将结构用作接口.所有这些结构都是静态填充的.

这个解决方案看起来不错但我可能会浪费大量内存,因为编译器不知道如何切断结构并只保留它真正需要的内容.

以下示例可以使用或不使用构建,-DDIRECT并且行为应该完全相同.

来源(test.c)

#include <stdlib.h>

int do_foo(int a) {
    return 42 * a;
}

#ifdef DIRECT
int (*foo)(int) = do_foo;
int (*bar)(int);
int storage;
#else
struct foo_ts {
    int (*do_foo)(int);
    int (*do_bar)(int);
    int storage;
} foo = {.do_foo = do_foo};
#endif

int main(char argc) {
    #ifdef DIRECT
    return foo(argc);
    #else
    return foo.do_foo(argc);
    #endif
}
Run Code Online (Sandbox Code Playgroud)

Makefile文件

CFLAGS=-O2 -mthumb -mcpu=cortex-m3 -g --specs=nosys.specs
CC=arm-none-eabi-gcc

upper=$(shell echo $(1) | tr a-z A-Z)

EXEC=direct.out with_struct.out

all: $(EXEC) …
Run Code Online (Sandbox Code Playgroud)

c embedded gcc arm

5
推荐指数
1
解决办法
339
查看次数

struct和volatile int的C-union对struct的更新是否也是volatile?

如下所示,我有一个具有几个位域的结构。我也有一个包含易失性64位值和此结构的联合。通过联合访问的结构更改是否也易变?

也就是说,由于结构和volatile int共享相同的内存位置,对结构的访问是否还会导致每次访问都访问内存,还是编译器将它们存储在寄存器中?

typedef struct {
  uint64 my_info_1:12;
  uint64 my_info_2:16;
  uint64 reserved: 36;
  } my_info_s;

typedef union {
  my_info_s my_info_struct;
  volatile unsigned long long my_info_vol; //64 bit
} my_info_u;

my_info_u my_info;
//Are these volatile accesses?
my_info.my_info_1 = 4;
my_info.my_info_2 = 8;

//This is the motivation- update a bunch of bitfields, but set them in one shot.
atomic_set(atomic_location, my_info.my_info_vol);
Run Code Online (Sandbox Code Playgroud)

由于某些实时限制,我无法使用锁/互斥锁来实现此目的。

c struct volatile unions

5
推荐指数
1
解决办法
214
查看次数

有什么工具可以优化 C 中的结构大小吗?

寻找一种以 C 结构作为输入并输出尺寸最小的结构的工具。

例如,给定一个只有 3 个成员的初始结构

struct Book {
   char  title[50];
   char  author[25];
   int   book_id;
};
Run Code Online (Sandbox Code Playgroud)

有6种排列

struct Book1 {
   char  title[50];
   char  author[25];
   int   book_id;
};

struct Book2 {
   char  title[50];
   int   book_id;
   char  author[25];   
};

struct Book3 {
   char  author[25];     
   char  title[50];
   int   book_id; 
};

struct Book4 {
   char  author[25];     
   int   book_id; 
   char  title[50];   
};

struct Book5 {
   int   book_id; 
   char  author[25];     
   char  title[50];   
};

struct Book6 {
   int   book_id; 
   char  title[50];     
   char  author[25];     
};
Run Code Online (Sandbox Code Playgroud)

输出显示 80 …

c optimization

5
推荐指数
1
解决办法
444
查看次数

用 PHP 读取二进制文件

是否有关于如何读取二进制文件的综合信息?我在 PHP 网站 ( http://www.php.net/manual/en/function.pack.php )上找到了信息,但我很难理解如何处理“typedef struct”和结构使用。

我有一个很长的二进制文件,里面有很多块,每个块都可以用 C 结构来表示。这个 C 结构有各种类似于我在下面提出的“typedef 结构”:

typedef struct
{ 
 unsigned char Day;
 unsigned char Month;
 unsigned char Year;
} DATE_OF_BIRTH;
#define USER_TYPE 5
DATE_OF_BIRTH Birth[2];\
Run Code Online (Sandbox Code Playgroud)

编辑:

我在下面有一个结构,这是更大结构的一部分

typedef struct FILE_HEADER_tag
{
    int Type;
    int Version;
    unsigned long Model;
    unsigned long Number;
    int Class;

    int TemplateLoaded;
    char TemplateName[32];
    RTC_TIME_DATE StartTime;
    RTC_TIME_DATE CurrentCal;
    RTC_TIME_DATE OriginalCal;
    TEMPLATE_SETTINGS;
    int EndType;
} FILE_HEADER;

typedef struct
{
 unsigned char Second;
 unsigned char Minute;
 unsigned char Hour;
 unsigned char Day; …
Run Code Online (Sandbox Code Playgroud)

php c struct

4
推荐指数
1
解决办法
2万
查看次数

在c中使用struct进行位字段提取

我使用这两种方法从寄存器中获取位字段信息.我需要提取的位字段的位置由Intel Manual提供.就像下面的代码一样.但是我得到的结果与这两种方法不同.

我找不到这两种方法的任何问题.但根据我的理解,maximum_power字段不应该是'0'作为第一种方法(这是英特尔已经在寄存器中定义的值.)

方法1:

typedef struct rapl_parameters_msr_t {
  uint64_t thermal_spec_power        : 15;
  uint64_t                           : 1;
  uint64_t minimum_power             : 15;
  uint64_t                           : 1;
  uint64_t maximum_power             : 15;
  uint64_t                           : 1;
  uint64_t maximum_limit_time_window : 6;
  uint64_t                           : 10;
} rapl_parameters_msr_t;

uint64_t              msr;
read_msr(cpu, 0x614, &msr);

rapl_parameters_msr_t domain_msr = *(rapl_parameters_msr_t *)&msr;
printf("%ld\n", domain_msr.thermal_spec_power);  //print: 280
printf("%ld\n", domain_msr.minimum_power);  //print: 192
printf("%ld\n", domain_msr.maximum_power);  //print: 0
printf("%ld\n", domain_msr.maximum_limit_time_window);  //print: 16
Run Code Online (Sandbox Code Playgroud)

方法2:

uint64_t 
extractBitField(uint64_t inField, uint64_t width, uint64_t offset)
{
    uint64_t bitMask;
    uint64_t outField;

    if ((offset+width) == …
Run Code Online (Sandbox Code Playgroud)

c struct bit-manipulation intel cpu-registers

3
推荐指数
1
解决办法
1379
查看次数

如何在没有填充位的情况下在C中输出二进制文件

我想将结构数据输出到二进制文件,但每个变量的信息之间没有任何填充位.例如:

struct s {
    int i1;
    short s1;
    char c1;
};
struct s example[2];
Run Code Online (Sandbox Code Playgroud)

如果我使用fwrite(&example, sizeof(struct s), 2, file)的二进制文件仍然之间,例如,填充比特s1c1,也从c1i1 (of the 2nd struct).

从输出文件中删除这些填充位的好方法是什么?

谢谢!任何帮助表示赞赏

c binary bits padding output

3
推荐指数
1
解决办法
1045
查看次数

将 QByteArray 中的数据放入结构中

我正在使用返回字节数组的串行设备。该数组中的值存储在无符号短整型和无符号字符中。我有以下结构:

    typedef struct {
    unsigned short RPM;             //0
    unsigned short Intakepress;     //1
    unsigned short PressureV;       //2
    unsigned short ThrottleV;       //3
    unsigned short Primaryinp;      //4
    unsigned short Fuelc;           //5
    unsigned char Leadingign;       //6
    unsigned char Trailingign;      //7
    unsigned char Fueltemp;         //8
    unsigned char Moilp;            //9
    unsigned char Boosttp;          //10
    unsigned char Boostwg;          //11
    unsigned char Watertemp;        //12
    unsigned char Intaketemp;       //13
    unsigned char Knock;            //14
    unsigned char BatteryV;         //15
    unsigned short Speed;           //16
    unsigned short Iscvduty;        //17
    unsigned char O2volt;           //18 …
Run Code Online (Sandbox Code Playgroud)

c++ arrays qt data-structures

3
推荐指数
1
解决办法
4143
查看次数