| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1543 人关注过本帖
标题:谁能解决问题,重谢!
只看楼主 加入收藏
demon90s
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:44
专家分:121
注 册:2016-1-3
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:6 
谁能解决问题,重谢!
在GCC下编译,运行错误。在VC下,没问题


代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main(void)
{
int count = 5;


char *p, *q;
int i, j;
char **trans = (char**)malloc(count * 4);
char buf[] = "hahahahahha";


//q = (char*)malloc(strlen(p) + 1);
//strcpy(q, "hiiii");
//printf("%p: %s\n", q, q);


trans[0] = (char*)malloc(strlen(buf) + 1);
strcpy(trans[0], "hello");


for (i = 1; i < count; i++)
{
trans[i] = (char*)malloc(strlen("qwerty") + 1);
strcpy(trans[i], "qwerty");

printf("%p: %s\ttrans[0]: %s\n", trans[i], trans[i], trans[0]);
}


//printf("%p: %s\n", q, q);
//free(q);


getchar();


return 0;
}


GCC下运行的结果:
0x1fd7050: qwertytrans[0]: hello
0x1fd7070: qwertytrans[0]: hello
0x1fd7090: qwertytrans[0]: hello
0x1fd70b0: qwertytrans[0]: ?p?


第四次输出,trans[0]所指的内容发生了变化,但我不知道为什么
搜索更多相关主题的帖子: include qwerty hello count 
2016-01-03 15:23
ehszt
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:40
帖 子:1744
专家分:3216
注 册:2015-12-2
收藏
得分:4 
你软件有问题吧,刚用gcc编译正常
图片附件: 游客没有浏览图片的权限,请 登录注册
2016-01-03 16:24
天天学习_
Rank: 2
等 级:论坛游民
帖 子:6
专家分:11
注 册:2016-1-2
收藏
得分:4 
回复 楼主 demon90s
--------------------Configuration: 看看 - Win32 Debug--------------------
Compiling...
看看.cpp
c:\users\administrator\desktop\c examlple\看看.cpp(11) : warning C4101: 'p' : unreferenced local variable
c:\users\administrator\desktop\c examlple\看看.cpp(11) : warning C4101: 'q' : unreferenced local variable
c:\users\administrator\desktop\c examlple\看看.cpp(12) : warning C4101: 'j' : unreferenced local variable
Linking...

看看.exe - 0 error(s), 0 warning(s)


用vc++6.0,可以运行。。。
2016-01-03 18:28
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:4 
除了 malloc(count * 4) 应该是 malloc(count * sizeof(char*)) 之外,看不出有其它错
2016-01-04 09:39
demon90s
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:44
专家分:121
注 册:2016-1-3
收藏
得分:0 
回复 2楼 ehszt
我在LINUX虚拟机里面用GCC编译出错了,但是跑到windows下面用GCC编译又正确了。

不知道是不是我的LINUX虚拟机的问题。。
2016-01-04 11:00
demon90s
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:44
专家分:121
注 册:2016-1-3
收藏
得分:0 
回复 5楼 rjsp
问题不在这里
2016-01-04 11:00
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:4 
vs2010 中运行没啥问题

DO IT YOURSELF !
2016-01-04 11:02
快速回复:谁能解决问题,重谢!
数据加载中...
 
   



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

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