【问题标题】:Why is this wrong!? Generating strings为什么这是错误的!?生成字符串
【发布时间】:2012-11-16 19:10:53
【问题描述】:

我一直在尝试以这种方式生成字符串:

一个

b

.

.

z

ab

.

.

zz

.

.

.

.

zzzz

我想知道为什么当它到达'yz'时会提示Segmentation fault(core dumped)错误。我知道我的代码没有涵盖所有可能的字符串,例如“zb”或“zc”,但这还不是重点,我想知道为什么会出现这个错误。如您所见,我不是编码大师,因此请尝试清楚地解释。谢谢:)

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

void move_positions (char s[]);

int main (int argc, char *argv[])
{
    char s[28]; 
    s[0] = ' ';
    s[1] = '\0';
    int a = 0;
    for(int r = 'a'; r <= 'z'; r++)
    {
        for(int t ='a';t <='z'; t++)
        {   
            for(int u = 'a';u <= 'z'; u++)
            {   
                for(int y = 'a'; y <= 'z'; y++)                                    
                {                                          
                    s[a] = (char)y;  
                    printf ("%s\n", s);                                                                    
                    if (s[0] == 'z')
                    {                                                                                      
                        move_positions(s);
                        a++;
                    } 
                }
                s[a-1] = (char)u;
            }
            s[a-2] = (char)t;
        }
        s[a-3] = (char)r;
    }
return 0;
}


void move_positions (char s[])
{
    char z[28];
    z[0] = ' ';
    z[1] = '\0';
    strcpy(s, strcat(z, s));
}

【问题讨论】:

  • 请更新标题,以便问题对未来的访问者有用。否则,它可能会因为过于本地化而被关闭。

标签: string compiler-errors segmentation-fault


【解决方案1】:

首先,让我们在打开调试的情况下进行编译:

gcc -g prog.c -o prog

现在让我们在调试器下运行它:

> gdb prog
GNU gdb 6.3.50-20050815 (Apple version gdb-1822) (Sun Aug  5 03:00:42 UTC 2012)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin"...Reading symbols for shared libraries .. done

(gdb) run
Starting program: /Users/andrew/Documents/programming/sx/13422880/prog 
Reading symbols for shared libraries +............................. done
a
b
c
d
e
...

yu
yv
yw
yx
yy
yz

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x00007fffc0bff6c5
0x0000000100000c83 in main (argc=1, argv=0x7fff5fbff728) at prog.c:22
22                      s[a] = (char)y;  

好的,它在第 22 行崩溃,尝试执行 s[a] = (char)ya 是什么?

(gdb) p a
$1 = 1627389953

因此,您正在设置数组 s 的第 160 万个条目。什么是s

(gdb) ptype s
type = char [28]

在 28 个元素的数组中保存 160 万个条目?那是行不通的。看起来您需要在某些循环开始时将 a 重置为零。

【讨论】:

  • 谢谢安德鲁!你是对的!但我不知道为什么它会从 1 变为 160 万。我试图解决这个问题,但它突然发生了变化,而没有进入“a”改变其值的语句。这有点奇怪。它打印出 'za' 并在 a=1 之前和之后(但在进入 if 条件之前,它会改变它的值)a= 160 万 :( 为什么?你可以在这张图片中看到:link跨度>
  • @user1830562 很高兴我能提供帮助。屏幕截图中的代码与您上面问题中的代码不同 - 我必须查看所有代码才能弄清楚。无论如何,由于您是该网站的新手,请务必upvote 任何有用的回复,mark as accepted 是您问题的最佳答案。如果您不经常这样做,那么人们将来回答您的问题的可能性就会降低。谢谢!
  • 再次感谢安德鲁!好吧,是相同的代码,但我将“a”更改为 long long int 而不仅仅是一个 int。此外,还有三个 printf 和两个系统暂停,但只是为了检查代码和获取我发布的图像!对不起,我没有投票给你,因为我需要 15 名声望,但我还没有:(
猜你喜欢
  • 1970-01-01
  • 2012-09-12
  • 2018-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-14
  • 1970-01-01
相关资源
最近更新 更多