gpt4 book ai didi

c++ - gdb 打印软数组的数组

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

#include <stdio.h>
#include <malloc.h>
typedef struct _soft_array
{
int len;
int array[];//int array[0];
}SoftArray;

int main()
{
int i = 0;
SoftArray* sa = (SoftArray*)malloc(sizeof(SoftArray) + sizeof(int) * 10);

sa->len = 10;

for(i=0; i<sa->len; i++)
{
sa->array[i] = i + 1;
}

for(i=0; i<sa->len; i++)
{
printf("%d\n", sa->array[i]);
}

free(sa);

return 0;
}

我使用 gdb 打印 sa->array 的数组,然后有些事情让我感到困惑:

(gdb) p sa->array
$1 = 0x602014
(gdb) p *sa->array@10
$2 = {[0] = 1, [1] = 2, [2] = 3, [3] = 4, [4] = 5, [5] = 6, [6] = 7, [7] = 8, [8] = 9, [9] = 10}
(gdb) p sizeof(int)
$3 = 4
(gdb) p *(char *)0x602014@40
$4 = "\001\000\000\000\002\000\000\000\003\000\000\000\004\000\000\000\005\000\000\000\006\000\000\000\a\000\000\000\b\000\000\000\t\000\000\000\n\000\000"
(gdb)

第一种方法很顺利(我在 .gdbinit 中添加了 set print array-indexes on)。

sizeof(int)==4,然后我用p *(char *)0x602014@40,由于我的电脑是小端,所以1显示01 00 00 00。

当显示7时,输出变成a?

这是怎么发生的,或者我错过了什么?

最佳答案

请注意,输出不是字符 'a' ,但是'\a' .这是 alert 字符,在 ASCII 中.就像你以后得到'\b''\t''\n'对于这些字符的 ASCII 等价物。

This character escape reference包含所有标准 C 转义字符及其 ASCII 编码值的列表。

关于c++ - gdb 打印软数组的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53250451/

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