分段错误很奇怪
问题:当我尝试编译文件并运行时,它存在分段问题。当我将文件传递给我的朋友(他正在使用相同版本的ubuntu)时,服务器将能够运行。我想知道为什么?
下面是我整个页面的代码。 我个人觉得没有什么问题,不过我就贴出来供大家参考。
void readNStoreData ()
{
char words[MAX];
char *wholeLine;
char* delimiter = ",";
int cflag = 0;
int x, count = 0;
char input;
FILE *countryFile;
countryFile = fopen("Countries.txt","r");
if (!countryFile) {
exit(EXIT_FAILURE);
}
while (fgets (words, MAX - 1, countryFile) != NULL)
{
//atof to convert string to double
//split a single line into individual tokens indicating , as the delimeter
//afterwards store them into array
wholeLine = strtok (words, delimiter);
strcpy (records [count].TDL, wholeLine);
wholeLine = strtok (NULL, ",");
strcpy (records [count].cName, wholeLine);
wholeLine = strtok (NULL, delimiter);
strcpy (records [count].FIPS104, wholeLine);
wholeLine = strtok (NULL, delimiter);
strcpy (records [count].ISO2, wholeLine);
wholeLine = strtok (NULL, delimiter);
strcpy (records [count].ISO3, wholeLine);
wholeLine = strtok (NULL, delimiter);
records [count].ISO = atof(wholeLine);
wholeLine = strtok (NULL, delimiter);
strcpy (records [count].cCapital, wholeLine);
wholeLine = strtok (NULL, delimiter);
strcpy (records [count].cRegion, wholeLine);
wholeLine = strtok (NULL, delimiter);
strcpy (records [count].cCurrencyName, wholeLine);
wholeLine = strtok (NULL, delimiter);
strcpy (records [count].cCurrencyCode, wholeLine);
wholeLine = strtok (NULL, delimiter);
records [count].cPopulation = atof(wholeLine);
count++;
}
fclose(countryFile); //close file
}
我希望有人能够在某个地方发现错误。 感谢提前提供帮助的人!
运行gdb,错误实际上是这一行。 它位于这个函数中。
l(gdb) frame 1
l#1 0x08048936 in readNStoreData () at testserver.c:61
61 strcpy (records [count].cName, wholeLine);
Problem: When i tried to compile my file and run, it has segmentation problem. When I passed the file to my friend (he is using the same version of ubuntu), the server will be able to run. I am wondering why?
Below will be my code for the whole page.
Personally I feel that there is no problem with it but I will just paste it for reference in case anyone asks for it.
void readNStoreData ()
{
char words[MAX];
char *wholeLine;
char* delimiter = ",";
int cflag = 0;
int x, count = 0;
char input;
FILE *countryFile;
countryFile = fopen("Countries.txt","r");
if (!countryFile) {
exit(EXIT_FAILURE);
}
while (fgets (words, MAX - 1, countryFile) != NULL)
{
//atof to convert string to double
//split a single line into individual tokens indicating , as the delimeter
//afterwards store them into array
wholeLine = strtok (words, delimiter);
strcpy (records [count].TDL, wholeLine);
wholeLine = strtok (NULL, ",");
strcpy (records [count].cName, wholeLine);
wholeLine = strtok (NULL, delimiter);
strcpy (records [count].FIPS104, wholeLine);
wholeLine = strtok (NULL, delimiter);
strcpy (records [count].ISO2, wholeLine);
wholeLine = strtok (NULL, delimiter);
strcpy (records [count].ISO3, wholeLine);
wholeLine = strtok (NULL, delimiter);
records [count].ISO = atof(wholeLine);
wholeLine = strtok (NULL, delimiter);
strcpy (records [count].cCapital, wholeLine);
wholeLine = strtok (NULL, delimiter);
strcpy (records [count].cRegion, wholeLine);
wholeLine = strtok (NULL, delimiter);
strcpy (records [count].cCurrencyName, wholeLine);
wholeLine = strtok (NULL, delimiter);
strcpy (records [count].cCurrencyCode, wholeLine);
wholeLine = strtok (NULL, delimiter);
records [count].cPopulation = atof(wholeLine);
count++;
}
fclose(countryFile); //close file
}
I hope someone will be able to spot the mistake somewhere.
Thanks to advance to those who helped!
run the gdb and the error is actually this line.
it is situated in this function.
l(gdb) frame 1
l#1 0x08048936 in readNStoreData () at testserver.c:61
61 strcpy (records [count].cName, wholeLine);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我强烈建议您学习使用调试器,例如< a href="http://www.gnu.org/software/gdb/" rel="nofollow">GDB。您可以通过
sudo apt-get install gdb
在 ubuntu 上安装它,这是一个简短的 教程。
Google 发现许多更多示例
编辑:
由于您现在已经运行了 GDB,请尝试设置 断点之前
run
:(gdb) br testserver.c:61
在你执行
run
之后你应该能够打印
各种变量,看看哪一个是非法的。I strongly suggest you to learn to use a debugger, such as GDB. You can install it on ubuntu by
sudo apt-get install gdb
Here is a short tutorial.
Google finds many more examples
EDIT:
Since you now have GDB running, try setting a breakpoint before
run
:(gdb) br testserver.c:61
and after you do
run
you should be able toprint
the various variables and see which one is illegal.对
strcpy
的调用失败。您向其中传递了错误的参数(请参阅 http://www.opengroup.org/sud /sud1/xsh/strcpy.htm 用于文档)。如果您希望我们对此进行分析,您需要向我们展示
records
、records
数组、元素数量、count
不超出records
数组的容量,records[count].cName
(大概是char*
或 < code>char[]) 足够大以包含由strtok
解析的令牌<块引用>
注意最大可能的标记是
MAX
个字符,因为它可能是fgets
返回的最大行长度+ 1 个尾随 NUL 字符,前提是输入中没有单个delimiter
字符。This should get you underway.
我偷偷怀疑您可能忘记了一起初始化接收数组(
记录
),但同样,除非您显示更多代码,否则我们无法知道。Your call to
strcpy
fails. You pass bad parameters into it (see http://www.opengroup.org/sud/sud1/xsh/strcpy.htm for documentation).If you want us to analyze this, you need to show us
records
is defined,records
array, to how many elements,count
to not go beyond the capacity of therecords
arrayrecords[count].cName
(presumably eitherchar*
orchar[]
) is large enough to contain the token parsed bystrtok
This should get you underway.
I have a sneaking suspicion that you may have forgotten to initialize the receiving array (
records
) alltogether, but again, we can't know unless you show more code.