segmentation-fault

Segmentation Fault when attempting to print value in C

三世轮回 提交于 2019-12-17 14:49:00
问题 The following C code returns a "segmentation fault" error. I do not understand why it does not return the value 20. What is my error? #include <stdio.h> int main() { int* n; *n = 20; printf("%i\n",*n); return 0; } 回答1: You haven't allocated memory to n , so *n = 20; attempts to write unspecified memory. Try #include <stdlib.h> int *n = malloc(sizeof *n); /* use n */ free(n); 回答2: You haven't allocated space for your int , you've only declared a pointer to an int . The pointer is uninitialized

fclose() causing segmentation fault

折月煮酒 提交于 2019-12-17 14:31:09
问题 I have a tab-delimited text file that I am parsing. Its first column contains strings of the format chrX , where X denotes a set of strings, e.g., "1", "2", ..., "X", "Y". These are each stored in a char* called chromosome , as the file is parsed. The text file is sorted on the first column lexicographically, i.e., I will have a number of rows starting with "chr1", and then "chr2", etc. At each "chrX" entry, I need to open another file that is associated with this entry: FILE *merbaseIn; //

fclose() causing segmentation fault

北城以北 提交于 2019-12-17 14:30:52
问题 I have a tab-delimited text file that I am parsing. Its first column contains strings of the format chrX , where X denotes a set of strings, e.g., "1", "2", ..., "X", "Y". These are each stored in a char* called chromosome , as the file is parsed. The text file is sorted on the first column lexicographically, i.e., I will have a number of rows starting with "chr1", and then "chr2", etc. At each "chrX" entry, I need to open another file that is associated with this entry: FILE *merbaseIn; //

Why does an infinitely recursive function in PHP cause a segfault?

二次信任 提交于 2019-12-17 11:21:36
问题 A hypothetical question for you all to chew on... I recently answered another question on SO where a PHP script was segfaulting, and it reminded me of something I have always wondered, so let's see if anyone can shed any light on it. Consider the following: <?php function segfault ($i = 1) { echo "$i\n"; segfault($i + 1); } segfault(); ?> Obviously, this (useless) function loops infinitely. And eventually, will run out of memory because each call to the function executes before the previous

Returning pointer from a function

拜拜、爱过 提交于 2019-12-17 10:25:46
问题 I am trying to return pointer from a function. But I am getting segmentation fault. Someone please tell what is wrong with the code #include<stdio.h> int *fun(); main() { int *ptr; ptr=fun(); printf("%d",*ptr); } int *fun() { int *point; *point=12; return point; } 回答1: Allocate memory before using the pointer. If you don't allocate memory *point = 12 is undefined behavior. int *fun() { int *point = malloc(sizeof *point); /* Mandatory. */ *point=12; return point; } Also your printf is wrong.

Segfaults in malloc() and malloc_consolidate()

非 Y 不嫁゛ 提交于 2019-12-17 09:45:49
问题 My application segfaults sometimes and mainly in malloc() and malloc_consolidate() when I look at the backtrace in gdb. I verified that the machine has enough memory available, it didn't even start swapping. I checked ulimits for data segement and max memory size and both are set to 'unlimited'. I also ran the application under valgrind and didn't find any memory errors. Now I'm out of ideas what else might be causing these segfaults. Any Ideas ? Update: Since I'm not finding anything with

Segmentation Fault before main

二次信任 提交于 2019-12-17 07:41:56
问题 so I've been running into a problem where somehow my code is causing segmentation faults before any of my main actually runs. I've never had this happen before and I hardly have a quarter's worth of coding experience so I'm not sure if there's something I'm doing wrong. Everything compiles, at least on my computer, but upon running it my main is never reached. Context: I'm trying to connect Vertices and Edges in an adjacency matrix and then use Prim's algorithm to build an MST, but that's for

Segmentation Fault While Creating Large Arrays in C

一世执手 提交于 2019-12-17 06:56:12
问题 you guys have helped me so much with this code. Let me preface by saying I do not know C very well and am trying really hard to do this. This is what the program should do: Create a list of random numbers of length 10 Million Sort the list of random numbers using shell sort function (still doesn't work properly...i think its how I am passing the pointer to the function) Make a list 1 Million Longer repeat for up to 100 million while recording time (the time shows up as 0.0000000 for some

execve shellcode writing segmentation fault

断了今生、忘了曾经 提交于 2019-12-17 04:07:20
问题 I am trying to study execve shellcode, OS : Linux bt 2.6.39.4 root@bt:~/exploit# cat gshell.s .globl _start _start: nop jmp MyString shell: popl %esi xorl %eax,%eax movl %al,9(%esi) movl %esi,10(%esi) movl %eax,14(%esi) movb $11,%al movl %esi, %ebx leal 0xa(%esi),%ecx leal 0xe(%esi),%edx int $0x80 movl $1,%eax movl $0,%ebx int $0x80 MyString: call shell shellvar: .ascii "/bin/bashADDDDCCCC" root@bt:~/exploit# as -gstabs -o gshell.o gshell.s root@bt:~/exploit# ld -o gshell gshell.o root@bt:~

Why don't I get a segmentation fault when I write beyond the end of an array?

六眼飞鱼酱① 提交于 2019-12-16 20:05:15
问题 why is this not giving error when I compile? #include <iostream> using namespace std; int main() { int *a = new int[2]; // int a[2]; // even this is not giving error a[0] = 0; a[1] = 1; a[2] = 2; a[3] = 3; a[100] = 4; int b; return 0; } can someone explain why this is happening. Thanks in advance.) 回答1: I'm guessing you're coming from Java or a Java-like language where once you step out of the boundary of an array, you get the "array index out of bounds" exception. Well, C expects more from