【发布时间】:2016-01-03 12:36:51
【问题描述】:
我有一个练习,我制作了一个生成随机字符串的函数!当我在 Windows 中运行该程序时,它运行良好,但是当我尝试在 linux 中运行相同的程序时,我遇到了分段错误。我在linux中做了一个调试 我去这个:
** 程序收到信号 SIGSEGV,分段错误。 0x08048be5 in get_unique_name() () **
get_unique_name()的代码
/*Function that generates random strings*/
string get_unique_name( )
{
char* s ;
for (int i = 0; i < 3 ; ++i)
{
int randomChar = rand()%(26+26+10);
if (randomChar < 26)
s[i] = 'a' + randomChar;
else if (randomChar < 26+26)
s[i] = 'A' + randomChar - 26;
else
s[i] = '0' + randomChar - 26 - 26;
}
s[3] = 0;
return s ;
}
【问题讨论】:
-
嗨。您的动态数组未分配。您的错误基本上意味着您正在尝试访问您无法使用的内存。看这里,例如:web.cs.swarthmore.edu/~newhall/unixhelp/C_arrays.html
标签: c++ linux string debugging segmentation-fault