简体   繁体   中英

segmentation fault (core dumped) without errors or warnings

I made coded this header and there were no errors no warnings everything seemed great, but then I tried to run it and it said:

segmentation fault (core dumped)

I looked trough the code several times tried <string.h> and <malloc.h> (maybe I had a faulty implementation of them idk), but none of it seemed to work main:

#include "SimpleScreen.h"

int main(){
    ScreenHeight = 10;
    ScreenWidth = 40;

    screen("write",5,7,12,0);
    screen("print",0,0,0,0);
}

SimpleScreen.h

#include <stdio.h>
#include "PrintColor.h"
#include "AdMath.h"

char background_occupacy[101] = {' ','@','@','@','@','@','@','W','W','M','M','M','M','M','M','M','M','B','Q','G','P','N','N','&','O','D','g','S','E','m','w','A','8','Z','K','H','6','9','X','C','p','b','q','5','U','2','O','e','3','a','V','V','F','4','h','k','o','s','y','u','n','n','c','T','z','z','7','J','x','L','L','v','?','?','=','f','t','t','j',')','(','(','r','r','i','^','/','*','_',';',';',':','-',',','.','.',' ',' ',' ',' '};

int ScreenHeight = 20;
int ScreenWidth = 35;

void screen(char mode[5],int x, int y, int color, int AssignToType){
  //copy vaules of adresses

  char write[5] = {"write"};
  char print[5] = {"print"};

  int ScreenSpace[ScreenHeight][ScreenWidth][3];//[][][3] 0 ^= foreground_color, 1 ^= background_color, 2 ^= (background)occupacy

  if(*mode = *write){
    ScreenSpace[x][y][AssignToType] = color;

  }

  if(*mode = *print){
    for(int i = 0; i < ScreenHeight; i++){
      for(int j = 0; j < ScreenWidth; j++){
        HighIntensity = (IsEven(ScreenSpace[i][j][0] == 1));

        text_color(((ScreenSpace[i][j][0]+1) / 2) -1);

        background_color((((ScreenSpace[i][j][1]+1) / 2) -1)*!(ScreenSpace[i][j][2] == 0)  +  (((ScreenSpace[i][j][0]+1) / 2) -1)*(ScreenSpace[i][j][2] == 0));

        printf("%c%c",background_occupacy[ScreenSpace[i][j][2]],background_occupacy[ScreenSpace[i][j][2]]);
      }
    }
  }
}

The if statements if(*mode = *write){ and if(*mode = *print){ are writing to string literals passed as the argument. Modifying string literals is prohibited.

If you want to check only the first characters, you should use == operator (two equal signs, not one) like if(*mode == *write){ and if(*mode == *print){ .

If you want to check if the whole strings matches, you should

  • Stop explicitly specifying the element size not to prevent it from putting the terminating null-character.
  • Use strcmp() to compare strings.

In other words, the lines

char write[5] = {"write"};
char print[5] = {"print"};

should be

char write[] = "write";
char print[] = "print";

and the lines

if(*mode = *write){
if(*mode = *print){

should be

if(strcmp(mode, write) == 0){
if(strcmp(mode, print) == 0){

Also

#include <string.h>

should be added to use strcmp() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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