English 中文(简体)
程序接收到 SIGSEGV 信号, 分割错误
原标题:String to vector in C, Program received signal SIGSEGV, Segmentation fault

我写一个简单的函数, 使 > C 字符串 (NOT C+++) 成为 Linux 函数 execvp 的矢量。

这是我的代码:

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

char** vecting(char *cstring) {

    int w_count = 0;           //word count
    char *flag = cstring;

    while (*flag !=   ) {
        if (*flag ==     || *flag ==  
  || *flag ==  	 )
            *flag =   ;
            flag++;
        else {
            w_count++;
            while (*flag !=     && *flag !=  
  && *flag !=  	  && *flag !=   )
                flag++;
        }
    }

    char **cvector = (char **)malloc(sizeof(char *)*(w_count+1));
    cvector[w_count] = NULL;
    int v_count;                //vector count

    for (v_count = 0, flag = cstring; v_count < w_count; v_count++) {
        while (*flag ==   )
            flag++;
        cvector[v_count] = flag;
        while (*flag !=   )
            flag++;
    }
    return cvector;
}

int main()
{
    char *p = "This is a BUG";
    char **argv = vecting(p);
    char **temp;

    for (temp = argv; *temp != NULL; temp++)
        printf("%s
", *temp);
    return 0;
}

当我运行它时,它会得到 < code> 分层错误 。

然后我调试它,我刚发现,一跑

程序收到SIGSEGV信号,分解故障

在那个时间 =

我无法理解为什么程序在程序更改 cstring 时会收到 SIGSEGV 信号

问题回答
char *p = "This is a BUG";

是一个字符串字典,它有 < 坚固 > 未定义的行为 < / 坚固 > 来修改它。 < code> charles *flag = cstring; 表示 < code > flag 指向与 < code> p 相同的地点( 恰好是只读存储器) 。 您试图做的事情( 如现在一样) 是非法的 。

尝试

char p[] = "This is a BUG"; 

获得 SIGSEGV 的原因是 < code> “ 这是臭虫” 字符串被放入 Const 区域。 当程序装入相应的内存区域时, 只标记为只读。 当程序试图写入只读的内存区域时, 它会收到分割错误 。





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签