分段故障。我不知道该怎么做

时间:2016-02-16 18:35:05

标签: segmentation-fault runtime-error

我遇到了这个错误,我整个晚上一直都在这。

Program received signal SIGSEGV, Segmentation fault.
0x00000000004019fe in TPoro::Color (this=0x7fffffffde40, 
    xx=0x401e9e "aZUL") at lib/tporo.cpp:84
84                  xx[i] = xx[i] + 'a' - 'A';

这是有问题的功能(它接收一个* char,它必须小写它并复制到颜色):

void
TPoro::Color(char * xx){

    if (xx != NULL){

        delete color;
        color =  new char[strlen(xx) + 1];


        for (int i = 0; i < strlen(xx) - 2 ; i++)
        {
            if((xx[i] <= 'Z') && (xx[i] >= 'A')){

                xx[i] = xx[i] + 'a' - 'A';// HERE IS THE ERRORR!!

            }
        }

        strcpy(color, xx);
    }else{
        delete color;
        color=NULL;
    }
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我无法帮助它,但盯着this=0x7fffffffde40,这是一个非常大/魔术般的数字。 this指的是您调用此方法的对象实例。你确定它已经正确初始化了吗?

另一个原因可能是'xx'是const char *,而不是char *。修改标记为const的内存中的数据可能会导致段错误。

在旁注中,您确实不应该像这样使用newdelete。您的方法将始终以删除color开始,该std::string在调用时可能无法初始化。另外,使用NULL而不是C风格的字符数组。请不要使用nullptr。请改用 package this.example.package; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.MediaController; import android.widget.VideoView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getActionBar().hide(); final List listA = new ArrayList<>(); listA.add("android.resource://" + getPackageName() + "/" + R.raw.c1); listA.add("android.resource://" + getPackageName() + "/" + R.raw.c2); listA.add("android.resource://" + getPackageName() + "/" + R.raw.c3); listA.add("android.resource://" + getPackageName() + "/" + R.raw.c4); listA.add("android.resource://" + getPackageName() + "/" + R.raw.c5); listA.add("android.resource://" + getPackageName() + "/" + R.raw.c6); listA.add("android.resource://" + getPackageName() + "/" + R.raw.c7); listA.add("android.resource://" + getPackageName() + "/" + R.raw.c8); listA.add("android.resource://" + getPackageName() + "/" + R.raw.c9); listA.add("android.resource://" + getPackageName() + "/" + R.raw.c10); final VideoView videoView = (VideoView) findViewById(R.id.videoView); final Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.c1); videoView.setVideoURI(video); videoView.start(); findViewById(R.id.videoView); //next and prev buttons test2 MediaController mediaController = new MediaController(this); mediaController.setPrevNextListeners(new View.OnClickListener() { @Override public void onClick(View v) { //next button clicked } }, new View.OnClickListener() { @Override public void onClick(View v) { //previous button clicked } }); //test1 new can be deleted videoView.setMediaController(mediaController); //test1 MediaController mediaController = new //test1 MediaController(this); mediaController.setAnchorView(videoView); videoView.setMediaController(mediaController); //exit button Button btn1 = (Button) findViewById(R.id.exitBtn); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); System.exit(0); } }); // end of exit button } }