求助一个大家关于 C 语言 数组和指针 的问题

2017-02-22 09:52:07 +08:00
 drlalll

本题出自 C Primer Plus


#include<stdio.h>
#define SIZE 10
int sump(int *start,int *stop);
int main(void)
{
    int marbless[SIZE] = {20,10,5,39,4,16,19,26,31,20};
    long answer;
    answer = sump(marbless,marbless+SIZE);
    printf("%d",answer);
    return 0;
}
int sump(int *start,int *stop)
{
    printf("*start = %d\n",*start);
    printf("*stop = %d\n",*stop);
    int total = 0;
    while(start < stop)
    {
	    printf("start = %d\n",start);
	    printf("stop = %d\n",stop);
	    printf("*start = %d\n",*start);
	    printf("*stop = %d\n",*stop);
	    total += *start;
	    start++;
    }
    return total;
    }

添加 printf 后各输出如下:

*start = 20
*stop = 1
start = 6684192
stop = 6684232
*start = 20
*stop = 1
start = 6684196
stop = 6684232
*start = 10
*stop = 1
start = 6684200
stop = 6684232
....
190

不明白的就是为什么 *stop 的值会是 1 ?? 不是 marbles + SIZE 吗?? 求解!!

2554 次点击
所在节点    C
19 条回复
drlalll
2017-02-22 09:57:22 +08:00
不是很明白 *stop 的指针地址的值, marbles [0] 的值为 0 那么 marbles [0] + 10 //size 的值 应该是 marbles [0+10] 才对吧???
whatTheGhost
2017-02-22 09:58:21 +08:00
marbless 是一个 int 型指针,比如 marbless 指向的地址是 0 , marbless++后地址就变成了 4 。
whatTheGhost
2017-02-22 10:02:00 +08:00
marbless+SIZE-1
drlalll
2017-02-22 10:05:06 +08:00
@whatTheGhost 这个我明白,比较不懂的是 marbles [0] 的值是 20 , marbles + SIZE 的值竟然是 1 ( SIZE 是 10 ),我的理解是 marbles + SIZE = marbles [10] ,但是 marbles 数组最大只有 marbles [9] , marbles [10] 应该是一个内存中之前存储的随机数,不知道这样理解对不对。
MrLin
2017-02-22 10:06:33 +08:00
正解, marbless+SIZE = marbles[10]
MrLin
2017-02-22 10:07:11 +08:00
是这个 marbless+SIZE = & marbles[10]
wevsty
2017-02-22 10:07:57 +08:00
你首先得明白 marbles + SIZE 是个什么东西。
marbles 是个数组,当成地址用的时候是指向首地址,也就是指向元素 marbles[0].
marbles 一共有 10 个元素,所以最后一个元素是 marbles[9],用指针表示也就是 marbles+9.
本例中 SIZE 是 10 , marbles+10 是一个未知的空间,里面是什么东西是完全未知的,典型的内存访问越界。
drlalll
2017-02-22 10:10:00 +08:00
@wevsty 谢谢
fliar
2017-02-22 10:10:53 +08:00
*stop 值应该不确定吧
VS2008 debug:
*start = 20
*stop = -858993460
start = 3733460
stop = 3733500
*start = 20
*stop = -858993460
start = 3733464
stop = 3733500
*start = 10
*stop = -858993460
start = 3733468
stop = 3733500
*start = 5
*stop = -858993460
start = 3733472
stop = 3733500
*start = 39
*stop = -858993460
kingddc314
2017-02-22 10:36:22 +08:00
数组访问越界了
congeec
2017-02-22 10:53:00 +08:00
其他语言写多了,已经不习惯 C 语言数组访问越界不报错了
drlalll
2017-02-22 12:24:02 +08:00
谢谢大家,已经弄明白了!!
oska874
2017-02-22 12:28:58 +08:00
都数组越界了。。。。
maplerecall
2017-02-22 12:49:01 +08:00
的确越界了,不过功能上应该一般情况下是没问题的,虽然输出了*stop 不过实际上根本没用到啊,最终目的只是为了计算数组数字的和……
woai110120130
2017-02-22 12:53:49 +08:00
stop 数组已经越界,指针是个野指针,并且 stop 应该是 marbles + SIZE   *stop=marbless[SIZE](越界)
hackpro
2017-02-22 13:39:42 +08:00
ASSERT(start == begin(marbless));
ASSERT(stop == end(marbless));
weiping1992
2017-02-22 13:46:45 +08:00
好久不写 C 了,但是数组越界还是能看得出来的啊哈哈哈
lzhCoooder
2017-02-22 13:55:24 +08:00
UB 凑巧是 1 而已
mainzo
2017-02-22 14:06:52 +08:00
调试器调试一下,看一下 marbless 数组的内存数据分布就知道怎么回事了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/342232

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX