简体   繁体   English

捕捉到意外信号:SIGSEGV (11)。 无效的内存访问

[英]Caught unexpected signal: SIGSEGV (11). Invalid memory access

I solved the codewars training as below.我解决了codewars培训如下。 I'm getting an error message, what's wrong in the code?我收到一条错误消息,代码有什么问题? I don't know what I made a mistake, so please let me know.我不知道我犯了什么错误,所以请告诉我。

Test Crashed Caught unexpected signal: SIGSEGV (11).测试崩溃 捕捉到意外信号:SIGSEGV (11)。 Invalid memory access.无效的内存访问。

#include <string.h>
#include <stdlib.h>
#include <ctype.h>
char *work_on_strings(const char *a, const char *b) {
size_t a_size = strlen(a), b_size = strlen(b), count;
char *a2 = a, *b2 = b,
 *str = (char*)calloc(a_size + b_size + 1,sizeof(char)),
 *base = str;

*a2 = a;
while(*a2)//文字列aから
{
  count = 0; 
  *b2 = b;
  while(*b2){if(*a2 == *b2++)count++;}  //bにいくつ同じ文字があるか
  if(count % 2 == 0)*str++ = *a2;  //同じ文字の数が0もしくは2の倍数だったら
  else  //同じ文字の数が奇数だったら
  {
    if(*a2 >= 'A' && *a2 <= 'Z')*str++ = tolower(*a2);  //大文字は小文字に
    else if(*a2 >= 'a' && *a2 <= 'z')*str++ = toupper(*a2);  //小文字は大文字にして格納
    else ;
  }    
  a2++;
}

*b2 = b;
while(*b2)  //文字列bも同様に
{
  count = 0; 
  *a2 = a;
  while(*a2){if(*b2 == *a2++)count++;}
  if(count % 2 == 0)*str++ = *b2;
  else
  {
    if(*b2 >= 'A' && *b2 <= 'Z')*str++ = tolower(*b2);
    else if(*b2 >= 'a' && *b2 <= 'z')*str++ = toupper(*b2);
    else ;
  }  
  b2++;
}
  return base;
}

It also gives a warning as it may be a hint.它还给出警告,因为它可能是一个提示。 I'm not sure about the warning about "const" either.我也不确定关于“const”的警告。 warning警告

solution.c:6:7: warning: initializing 'char *' with an expression of type 'const char *' discards 
qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
char *a2 = a, *b2 = b,
      ^    ~
solution.c:6:16: warning: initializing 'char *' with an expression of type 'const char *' discards 
qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
char *a2 = a, *b2 = b,
           ^    ~
solution.c:10:5: warning: incompatible pointer to integer conversion assigning to 'char' from 'const 
char *' [-Wint-conversion]
*a2 = a;
    ^ ~
solution.c:14:7: warning: incompatible pointer to integer conversion assigning to 'char' from 'const 
char *' [-Wint-conversion]
  *b2 = b;
      ^ ~
solution.c:26:5: warning: incompatible pointer to integer conversion assigning to 'char' from 'const 
char *' [-Wint-conversion]
*b2 = b;
    ^ ~
solution.c:30:7: warning: incompatible pointer to integer conversion assigning to 'char' from 'const 
char *' [-Wint-conversion]
  *a2 = a;
      ^ ~
6 warnings generated.

The idea behind using const is that you don't want to change the data stored at that memory address.使用 const 背后的想法是您不想更改存储在该内存地址的数据。 It's called an immutable character/string.它被称为不可变字符/字符串。

So if you do this:所以如果你这样做:

const char* a = "This is a string";
char* a2 = something;

Now both a and a2 point to the same area in the memory, and that area/region of memory doesn't allow changes, so every time you try to change it you will get an error.现在 a 和 a2 都指向内存中的同一区域,并且该内存区域/区域不允许更改,因此每次尝试更改它时都会出现错误。

Possible solution: you can try and copy the content into a different region in memory.可能的解决方案:您可以尝试将内容复制到内存中的不同区域。

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

int main()
{
    const char *a = "This is a string";
    char *a2 = malloc(sizeof(a)/sizeof(char));
    strcpy(a2, a); // copy data from a to a2

    printf("First string (a) = %s\n", a);
    printf("Second string (a2) = %s\n", a2);

    // signal to operating system program ran fine
    return 0;
}

Output:输出:

First string (a) = This is a string
Second string (a2) = This is a string

Here a and a2 are 2 variables which have the same data, both point to a region in memory which holds the characters "This is a string", but those region are different from each other, on region allows changes while the other doesn't.这里 a 和 a2 是具有相同数据的 2 个变量,都指向内存中的一个区域,该区域保存字符“这是一个字符串”,但这些区域彼此不同,区域允许更改而另一个则不允许.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM