| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1176 人关注过本帖
标题:关于malloc 的问题
取消只看楼主 加入收藏
statham
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2009-9-13
结帖率:0
收藏
已结贴  问题点数:10 回复次数:0 
关于malloc 的问题
这个newarray为什么不能CALL两次?

int count=0;
int *newarray(){
    int *arr= (int *)malloc(sizeof(int)*9);
    int i;
    for(i=0; i<=9; i++)    arr[i]=count++;
    return arr;
}
int main(){
    int *a1;
    a1 =newarray();
    int *a2;
    a2 =newarray();
    free(a2);
    free(a1);
    return 0;
}

第一次进入int *newarray()的时候没有问题, 可是a2 =newarray();这句的时候运行到
int *arr= (int *)malloc(sizeof(int)*9);
程序就溃掉了。  注意 for(i=0; i<=9; i++)  这里是会有数据overflow的。(因为前面是只给了9个INT的空间)

程序在visual studio 2005下是没有问题, 但在ubuntu下用gcc就会出现上述的问题。
哪位知道具体是怎么回事吗? 先谢了~

搜索更多相关主题的帖子: malloc malloc 
2009-09-13 15:27
快速回复:关于malloc 的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011083 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved