segmentation-fault

C - Off by one error, but no segmentation fault?

跟風遠走 提交于 2020-03-05 07:55:49
问题 I recently wrote this code in C: #include <stdio.h> #define N_ROWS 100 int main() { char *inputFileName = "triangle_data.txt"; FILE *inputFile = fopen(inputFileName, "r"); if (inputFile == NULL) { printf("ERROR: Failed to open \"%s\".\n", inputFileName); return -1; } int triangle[(N_ROWS*(N_ROWS+1))/2 - 1]; size_t size = sizeof(triangle)/sizeof(int); size_t index; for (index = 0; !feof(inputFile); ++index) { fscanf(inputFile, "%d", &triangle[index]); } return 1; } and was expecting a

SIGSEGV, (seemingly) caused by printf

青春壹個敷衍的年華 提交于 2020-03-02 06:48:07
问题 First and foremost, apologies for any cross-posting. Hope I'm not repeating an issue here, but I was unable to find this elsewhere (via Google and Stack Overflow). Here's the gist of the error. If I call printf , sprintf or fprintf anywhere within my code, to display a float, I get a SIGSEGV (EXC_BAD_ACCESS) error. Let me give an example. The following throws the error: float f = 0.5f; printf("%f\n",f); This code does not: float f = 0.5f; printf("%d\n",f); I realize there's an implicit

Scanf produces segmentation fault for non-pointer argument

南楼画角 提交于 2020-02-25 08:13:39
问题 I am writing a program in c that outputs the html file, actually I am learning CGI programming. This program stops at run time when it executes the loop ('for' and 'while' both). The error returned is Segmentation fault (core dumped) The code(only the segment of program that is causing the problem) is: void main() { int track=0; int question_no; printf("\nHow many questions?\t"); scanf("%d",question_no); while(track<=question_no) { printf("\nAshish"); track++; } } Actually I get the answer: I

second argument of the command line arguments in a format other than char** argv or char* argv[] [closed]

三世轮回 提交于 2020-02-16 14:22:09
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 12 days ago . To solve my problem here, I want to know if/how I can define the second variable of the command line arguments in a format other than char** argv or char* argv[] . The reason is that pybind11 doesn't allow either of those in the inputs of a function. Here are the methods I have tried: Method 1:

SegFault error when computing square root (Newton's method)

ⅰ亾dé卋堺 提交于 2020-02-15 06:30:53
问题 I'm very new to C++, but I'm aware that there are tons of ways to receive a SegFault error, but I'm not sure why I'm getting one here. The goal of the program is to compute the square root of a number using Newton's method. I'm assuming it has something to do with the recursion, but I'm pretty sure that the code would run in Java. #include <iostream> #include <sstream> using namespace std; double sqrt(double eps, double num, double last_guess) { if(num == 0 || num == 1) { return num; } int

SegFault error when computing square root (Newton's method)

我是研究僧i 提交于 2020-02-15 06:30:50
问题 I'm very new to C++, but I'm aware that there are tons of ways to receive a SegFault error, but I'm not sure why I'm getting one here. The goal of the program is to compute the square root of a number using Newton's method. I'm assuming it has something to do with the recursion, but I'm pretty sure that the code would run in Java. #include <iostream> #include <sstream> using namespace std; double sqrt(double eps, double num, double last_guess) { if(num == 0 || num == 1) { return num; } int

segfault on jump from PLT

非 Y 不嫁゛ 提交于 2020-02-06 07:41:06
问题 I am trying to find the cause of a segfault, and narrowed it to the PLT using gdb's btrace. The segfault occurs during a jump from the PLT to the GOT, which I interpret to signify that the PLT became corrupted during execution. Based on the analysis presented below, is this interpretation correct? What are likely culprits for corruption of the PLT? Stack overflow? I believe that installing a watchpoint on the GOT address could be helpful in this instance. Would watch -l 0x55555562f048 be the

32b x86 assembly scanf usage

被刻印的时光 ゝ 提交于 2020-01-30 06:34:48
问题 So, I am trying to use the scanf function in 32bit ATT assembly and keep getting segmentation faults, despite using pretty much the same code as the example shown in Computer Systems: A Programmer's Perspective and the assembly generated from my own simple C input program. I have no idea what it is I am doing wrong and would appreciate some help in figuring it out. My test assembly code(which segfaults): .data .align 4 fmt: .string "%d" str: .string "Input a number: " .text .global main .type

32b x86 assembly scanf usage

▼魔方 西西 提交于 2020-01-30 06:34:45
问题 So, I am trying to use the scanf function in 32bit ATT assembly and keep getting segmentation faults, despite using pretty much the same code as the example shown in Computer Systems: A Programmer's Perspective and the assembly generated from my own simple C input program. I have no idea what it is I am doing wrong and would appreciate some help in figuring it out. My test assembly code(which segfaults): .data .align 4 fmt: .string "%d" str: .string "Input a number: " .text .global main .type

Why does this code to modify a string not work?

♀尐吖头ヾ 提交于 2020-01-28 04:38:47
问题 With c-style strings, how do you assign a char to a memory address that a character pointer points to? For example, in the example below, I want to change num to "123456", so I tried to set p to the digit where '0' is located and I try to overwrite it with '4'. Thanks. #include <stdio.h> #include <stdlib.h> int main() { char* num = (char*)malloc(100); char* p = num; num = "123056"; p = p+3; //set pointer to where '4' should be p = '4'; printf("%s\n", num ); return 0; } 回答1: That code won't