什么情况下可以在同一个分配的内存不同的数据类型存储在C 2 [英] Is it okay to store different datatypes in same allocated memory in C?

查看:181
本文介绍了什么情况下可以在同一个分配的内存不同的数据类型存储在C 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要存储在相同的分配的存储器中的不同数据类型用于通过仅一次分配的存储器减少的执行时间。我发现它实际上是可以创建的uint8_t有变量数组,并创建一个新的 uint16_t 指针指向相同的内存地址,然后我可以读出的值是双向的。

这允许我创建一个指向假设所分配内存的中间,在下半年的值存储在不同的数据类型。

是好一些呢?我知道我需要照顾与内存边界,但是这是不好的风格?

下面我code:

 的#include<&stdio.h中GT;
#包括LT&;&inttypes.h GT;
#包括LT&;&stdint.h GT;
#包括LT&;&stdlib.h中GT;诠释主要(无效){
    uint8_t有*阵列;
    uint16_t *数组2;
    阵=释放calloc(6,1);
    数组[0] = 257;    的printf(数组[0] =%PRIu8\\ n,数组[0]);
    的printf(数组[1] =%PRIu8\\ n,数组[1]);
    的printf(住址冯数组[0] =%P \\ N,&安培;数组[0]);
    数组2 =放大器;数组[0];
    的printf(住址AUF模具数组2 zeigt =%P \\ N,数组2);    数组2 [0] = 257;
    输出(数组2 [0] =%PRIu16\\ N,数组2 [0]);
    输出(数组2 [1] =%PRIu16\\ N,数组2 [1]);
    的printf(数组[0] =%PRIu8\\ n,数组[0]);
    的printf(数组[1] =%PRIu8\\ n,数组[1]);    的getchar();
    返回0;
}


解决方案

使用联盟来创建一个变量,有时存储一个类型,有时另一回事。

 工会{
  uint8_t有U8;
  uint16_t U16;
} * array_u;
为size_t nmemb个= 6;array_u =释放calloc(nmemb个,sizeof的* array_u);
断言(array_u);的printf(array_u [0] = .u8%PRIu8\\ n,array_u [0] .u8);array_u [0] .u16 = 1234;
的printf(array_u [0] = .u16%PRIu16\\ n,array_u [0] .u16);
...

这不只有一个使用所有的空间uint8_t有每工会U8 。下使用2 uint8_t有

 工会{
  uint8_t有U8 [2];
  uint16_t U16;
} * array_u;输出(array_u [0] .u8 [0] =%PRIu8\\ N,array_u [0] .u8 [0]);array_u [0] .u16 = 1234;
的printf(array_u [0] = .u16%PRIu16\\ n,array_u [0] .u16);

OTOH如果code需要覆盖固定长度的整个阵列

 工会{
  uint8_t有U8 [12];
  uint16_t U16 [6];
} * array_u;array_u =释放calloc(1,sizeof的* array_u);
断言(array_u);输出(array_u-> U8 [0] =%PRIu8\\ N,array_u-> U8 [0]);array_u-> U16 [0] = 1234;
输出(array_u-> U16 [0] =%PRIu16\\ n,array_u-> U16 [0]);
...

I want to store different datatypes in the same allocated memory for reducing the execution time by only allocating the memory once. I found out it is actually possible to create an array of uint8_t variables and create a new uint16_t pointer to the same memory address and then I can read the values both ways.

That allows me to create a pointer let's say to the middle of the allocated memory and store the values in the second half in a different datatype.

Is that okay to do so? I know I need to take care with the memory boundaries but is this bad style?

Here my code:

#include <stdio.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdlib.h>

int main(void){
    uint8_t *array;
    uint16_t *array2;
    array = calloc(6, 1);
    array[0] = 257;

    printf("array[0]= %" PRIu8 "\n", array[0]);
    printf("array[1]= %" PRIu8 "\n", array[1]);
    printf("Adresse von array[0] = %p\n", &array[0]);
    array2 = &array[0];
    printf("Adresse auf die array2 zeigt = %p\n", array2);

    array2[0] = 257;
    printf("array2[0]= %" PRIu16 "\n", array2[0]);
    printf("array2[1]= %" PRIu16 "\n", array2[1]);
    printf("array[0]= %" PRIu8 "\n", array[0]);
    printf("array[1]= %" PRIu8 "\n", array[1]);

    getchar();
    return 0;
}

解决方案

Use a union to create a variable that sometimes stores one type, sometimes another.

union {
  uint8_t u8;
  uint16_t u16;
} *array_u;
size_t nmemb = 6; 

array_u = calloc(nmemb, sizeof *array_u);
assert(array_u);

printf("array_u[0].u8 = %" PRIu8 "\n", array_u[0].u8);

array_u[0].u16 = 1234;
printf("array_u[0].u16 = %" PRIu16 "\n", array_u[0].u16);
...

This does not use all the space with only one uint8_t u8 per union. The below uses 2 uint8_t.

union {
  uint8_t u8[2];
  uint16_t u16;
} *array_u;

printf("array_u[0].u8[0] = %" PRIu8 "\n", array_u[0].u8[0]);

array_u[0].u16 = 1234;
printf("array_u[0].u16 = %" PRIu16 "\n", array_u[0].u16);

OTOH If code needs to overlay an entire array of fixed length

union {
  uint8_t u8[12];
  uint16_t u16[6];
} *array_u;

array_u = calloc(1, sizeof *array_u);
assert(array_u);

printf("array_u->u8[0] = %" PRIu8 "\n", array_u->u8[0]);

array_u->u16[0] = 1234;
printf("array_u->u16[0] = %" PRIu16 "\n", array_u->u16[0]);
...

这篇关于什么情况下可以在同一个分配的内存不同的数据类型存储在C 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆