Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
520 views
in Technique[技术] by (71.8m points)

求解决一个c语言输出的问题

#include <stdio.h>
#define rstype_t long // type of the array
typedef struct {
    rstype_t *b, *e; // begin and end of each bucket
} rsbucket_t;
int main(){
    int size=8;
    rstype_t *beg;
    rsbucket_t *k, b[size], *be = b + size; // b[] keeps all the buckets
    for (k = b; k != be; ++k){ 
        k->b = k->e = beg;
        printf("b=%ld, e=%ld
", k->b, k->e); // 输出 b=4202800, e=4202800
    }
    printf("b=%ld, e=%ld
", k->b, k->e); // 输出 b=8, e=4203809
    return 0;
}

输出

b=4202800, e=4202800
b=4202800, e=4202800
b=4202800, e=4202800
b=4202800, e=4202800
b=4202800, e=4202800
b=4202800, e=4202800
b=4202800, e=4202800
b=4202800, e=4202800
b=8, e=4203809

问一下 同样的输出语句,为什么最后一行输出的和上面的不一样啊


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

size为8,b这个数组有效大小为8,这段内存系统为你初始化了,而最后一个,已经跨出了这个区域,内存没定义。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...