簡體   English   中英

gdb打印數組的軟數組

[英]gdb print array of soft array

#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)。

sizeof(int)== 4,然后我使用p *(char *)0x602014 @ 40,由於我的計算機是低位字節序,所以1顯示為01 00 00 00。

當顯示7時,輸出變為?

這是怎么發生的,或者我錯過了什么?

請注意,輸出的不是字符'a' ,而是'\\a' 這是警報字符,以ASCII表示 就像您稍后獲得這些字符的ASCII等效字符的'\\b''\\t''\\n'

該字符轉義參考包含所有標准C轉義字符及其ASCII編碼值的列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM