gpt4 book ai didi

c - 使用 max_align_t 存储一大块字节

转载 作者:太空宇宙 更新时间:2023-11-04 07:47:43 26 4
gpt4 key购买 nike

this thread我被建议使用 max_align_t 来为任何类型正确对齐地址,我最终创建了动态数组的这个实现:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>

struct vector {
size_t capacity;
size_t typesize;
size_t size;
max_align_t data[];
};

#define VECTOR(v) ((struct vector *)((unsigned char *)v - offsetof(struct vector, data)))

static void *valloc(size_t typesize, size_t size)
{
struct vector *vector;

vector = calloc(1, sizeof(*vector) + typesize * size);
if (vector == NULL) {
return NULL;
}
vector->typesize = typesize;
vector->capacity = size;
vector->size = 0;
return vector->data;
}

static void vfree(void *data, void (*func)(void *))
{
struct vector *vector = VECTOR(data);

if (func != NULL) {
for (size_t iter = 0; iter < vector->size; iter++) {
func((unsigned char *)vector->data + vector->typesize * iter);
}
}
free(vector);
}

static void *vadd(void *data)
{
struct vector *vector = VECTOR(data);
struct vector *new;
size_t capacity;

if (vector->size >= vector->capacity) {
capacity = vector->capacity * 2;
new = realloc(vector, sizeof(*vector) + vector->typesize * capacity);
if (new == NULL) {
return NULL;
}
new->capacity = capacity;
new->size++;
return new->data;
}
vector->size++;
return vector->data;
}

static size_t vsize(void *data)
{
return VECTOR(data)->size;
}

static void vsort(void *data, int (*comp)(const void *, const void *))
{
struct vector *vector = VECTOR(data);

if (vector->size > 1) {
qsort(vector->data, vector->size, vector->typesize, comp);
}
}

static char *vgetline(FILE *file)
{
char *data = valloc(sizeof(char), 32);
size_t i = 0;
int c;

while (((c = fgetc(file)) != '\n') && (c != EOF)) {
data = vadd(data);
data[i++] = (char)c;
}
data = vadd(data);
data[i] = '\0';
return data;
}

struct data {
int key;
char *value;
};

static int comp_data(const void *pa, const void *pb)
{
const struct data *a = pa;
const struct data *b = pb;

return strcmp(a->value, b->value);
}

static void free_data(void *ptr)
{
struct data *data = ptr;

vfree(data->value, NULL);
}

int main(void)
{
struct data *data;

data = valloc(sizeof(struct data), 1);
if (data == NULL) {
perror("valloc");
exit(EXIT_FAILURE);
}
for (size_t i = 0; i < 5; i++) {
data = vadd(data);
if (data == NULL) {
perror("vadd");
exit(EXIT_FAILURE);
}
data[i].value = vgetline(stdin);
data[i].key = (int)vsize(data[i].value);
}
vsort(data, comp_data);
for (size_t i = 0; i < vsize(data); i++) {
printf("%d %s\n", data[i].key, data[i].value);
}
vfree(data, free_data);
return 0;
}

但我不确定我是否可以使用 max_align_t 来存储一大块字节:

struct vector {
size_t capacity;
size_t typesize;
size_t size;
max_align_t data[]; // Used to store any array,
// for example an array of 127 chars
};

它是否打破了数组规则的最后一个元素?

最佳答案

Does it break the one past the last element of an array rule?

没有。

Using max_align_t to store a chunk of bytes

OP 的问题并不特殊,因为它使用了一个灵活的数组成员

As a special case, the last element of a structure ... have an incomplete array type; this is called a flexible array member. ... However, when a . (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) ...

这与访问任何分配的内存或一种类型的数组是同一个问题,就好像它是另一种类型一样。

max_align_t *char *void *转换在正确完成对齐时定义明确.

A pointer to an object type may be converted to a pointer to a different object type. If the resulting pointer is not correctly aligned for the referenced type, the behavior is undefined. C11dr §6.3.2.3 7

所有经过审查的代码访问都不会尝试访问“as if”数组之外的内容。

关于c - 使用 max_align_t 存储一大块字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55873051/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com