segmentation-fault

C++ segfault, reproducible example

若如初见. 提交于 2019-12-31 05:31:34
问题 Reposting with full code, as suggested from others. Just updated the main function with hard coded arguments that causes segmentation fault. Changing the width and height to something else lets the program run fine, for example 500 and 433 respectively. Main file: #include <cstdint> #include <iostream> #include <sstream> #include <vector> #include <cmath> #include "sierpinski.h" Triangle::Triangle() { this->pixels = nullptr; this->top = Coordinate(); this->left = Coordinate(); this->right =

Got stuck with Segmentation Fault Error

折月煮酒 提交于 2019-12-31 05:20:10
问题 I'm writing a simple program in c and I'm got stuck with this error Segmentation Fault (Core dumped) I know Segmentation fault error occurs due to memory access violation. But I'm unable to figure out where in the below simple program bad memory access is happening. #include<stdio.h> int main() { int a = 0; scanf("%d", a); printf("%d\n", a); return(0); } I'm compiling online here, In code blocks also it's giving the same error. 回答1: You must pass a pointer to int instead of the int itself.

Got stuck with Segmentation Fault Error

三世轮回 提交于 2019-12-31 05:18:08
问题 I'm writing a simple program in c and I'm got stuck with this error Segmentation Fault (Core dumped) I know Segmentation fault error occurs due to memory access violation. But I'm unable to figure out where in the below simple program bad memory access is happening. #include<stdio.h> int main() { int a = 0; scanf("%d", a); printf("%d\n", a); return(0); } I'm compiling online here, In code blocks also it's giving the same error. 回答1: You must pass a pointer to int instead of the int itself.

Not receiving a seg fault when expected

旧时模样 提交于 2019-12-31 04:19:05
问题 I'm in the process of learning how to use pointers and structs in C. Naturally, I'm trying to deliberately break my code to further understand how the language works. Here is some test code that works as I expected it to work: #include <stdio.h> #include <stdlib.h> struct pair { int x; int y; }; typedef struct pair pair; void p_struct( pair ); //prototype int main( int argc, char** argv ) { pair *s_pair; int size, i; printf( "Enter the number of pair to make: " ); scanf( "%d", &size );

Segmentation-fault error happening with Assembly code program

允我心安 提交于 2019-12-31 04:12:53
问题 I keep getting a segmentation fault error when running my code. Everything has compiled well, but I can't seem to get it to do what I want. The program is to ask the user to enter 3 integers, then ask the user what they think the average of the numbers would be, take that into account and then come back with whether or not the user guessed correctly segment .data ; ; Output strings ; prompt1 db "Enter a positive integer: ", 0 prompt2 db "Enter a second positive integer: ", 0 prompt3 db "Enter

C appending char to char*

隐身守侯 提交于 2019-12-30 18:26:06
问题 So I'm trying to append a char to a char* . For example I have char *word = " "; I also have char ch = 'x'; I do append(word, ch); Using this method.. void append(char* s, char c) { int len = strlen(s); s[len] = c; s[len+1] = '\0'; } It gives me a segmentation fault, and I understand why I suppose. Because s[len] is out of bounds. How do I make it so it works? I need to clear the char* a lot as well, if I were to use something like char word[500]; How would I clear that once it has some

Bad permissions for mapped region [duplicate]

妖精的绣舞 提交于 2019-12-30 10:44:13
问题 This question already has answers here : Why is this string reversal C code causing a segmentation fault? [duplicate] (8 answers) Closed 5 years ago . I get an error when trying to run the following function: char* reverseInPlace(char* src) { //no need to alloc or free memory int i=0; int size=mystrlen(src); for(i=0;i<size;i++) { int j=size-i-1; if(i<j) { char temp; printf("Interchange start %d:%c with %d:%c",i,src[i],j,src[j]); temp=src[i]; src[i]=src[j];//error occurs here src[j]=temp;

Template detects if T is pointer or class

会有一股神秘感。 提交于 2019-12-30 08:32:12
问题 Considering the following code: class MyClass { ... }; template <typename Object> class List { public: void insert(const Object & x) { // call when Object is MyClass } void insert(const Object & x) { // call when Object is MyClass* } } int main() { MyClass a; List<MyClass> lst; List<MyClass*> plst; lst.insert(a); plst.insert(new Myclass); return 0; } How to tell the compiler call different methods based on if the template is a class or a pointer? How to fix the code above? 回答1: You can use a

What error code does a process that segfaults return?

大兔子大兔子 提交于 2019-12-30 06:02:11
问题 What error code does a process that segfaults return? From my experiments, it seems to be "139", but I'd like to find why this is so and how standard it is. 回答1: The relevant syscall (giving the status of a terminated process) is waitpid(2). The 139 is for WIFSIGNALED and WTERMSIG etc... On Linux the actual bits are described in internal file /usr/include/bits/waitstatus.h which is included from <sys/wait.h> header The wait , waitpid call is standard in POSIX and so are the macro names (like

How to debug segmentation fault?

冷暖自知 提交于 2019-12-30 05:27:33
问题 It works when, in the loop, I set every element to 0 or to entry_count-1. It works when I set it up so that entry_count is small, and I write it by hand instead of by loop (sorted_order[0] = 0; sorted_order[1] = 1; ... etc). Please do not tell me what to do to fix my code. I will not be using smart pointers or vectors for very specific reasons. Instead focus on the question: What sort of conditions can cause this segfault? Thank you. ---- OLD ----- I am trying to debug code that isn't working