FAQ Database Discussion Community


C dynamic memory allocation array

c,arrays,function,pointers,segmentation-fault
My program has 3 int arrays (pointers) declared in the main function. The user enters the length of array A, which is then filled with random numbers. Then, a function is called which takes all 3 arrays as it's arguments. It takes all the even numbers from array A and...

Segmentation fault on one Linux machine but not another with C++ code

c++,linux,segmentation-fault,portability
I have been having a peculiar problem. I have developed a C++ program on a Linux cluster at work. I have tried to use it home on an Ubuntu 14.04 machine, but the program, which is composed of 6 files: main.hpp,main.cpp (dependent on) sarsa.hpp,sarsa.cpp (class Sarsa) (dependent on) wec.hpp,wec.cpp, does...

Segmentation Fault if I don't say int i=0

c,arrays,segmentation-fault,initialization,int
void removeVowels(char* array){ int i,j,v; i=0; char vowel[]={'a','e','i','o','u'}; while(array[i]!='\0') { for(v=0;v<5;v++) { if (array[i]==vowel[v]) { j=i; while(array[j]!='\0') { array[j]=array[j+1]; j++; } i--; break; } } i++; } } in function removeVowels() if I don't include i=0; and just say int i; why does it give segmentation fault? Isn't it automatically...

segmentation fault in linux (socket programming (TCP) in C)

c,linux,sockets,tcp,segmentation-fault
I am just learning socket programming on Linux by some websites and here are some parts of my code on server side by using TCP: #define BufferLength 100 #define SERVPORT 3111 int main() { /* Variable and structure definitions. */ int sd, sd2, rc, length = sizeof(int); int totalcnt =...

C++ (DevC++) Segmentation Fault on Run

c++,segmentation-fault,stack-overflow
For Some reason when i'm trying to run this programm, it stops working. I can understand there's something like Stack Overflow, But Seriously how can i run this code? is there any possible way? #include <iostream> #include <ctime> #include <cstdlib> #include <conio.h> using namespace std; #define N 30000 int getUniqueNumber(int...

Navigation hierarchy error in omnet: Simulation terminated with exit code: 139

c++,segmentation-fault,omnet++,inet
I am trying to navigate to a 2 level up module using following code:- cModule* parentmod = getParentModule(); cModule* grantParentmod = parentmod->getParentModule(); for (cSubModIterator iter(*grantParentmod); !iter.end();iter++) EV<<"Current module is "<< iter()->getFullName() <<endl; And the output is:- Current module is notificationBoard Current module is mobility Current module is udpApp[0] Current module...

Cannot Properly Print Contents of Vector

c++,arrays,string,vector,segmentation-fault
I have some C++ code where I'm taking input from a user, adding it to a vector splitting the string by delimiter, and for debugging purposes, printing the vector's contents. However, the program only prints the first location of the vector, and then none of the rest. main.cpp #include <cstdlib>...

segmentation fault on c program

c,segmentation-fault,core
I want to do a c program which receives some arguments.The first argument is a file and the other arguments are words.The program will create a thread for every word and will count the number of occurences in the file.I used a shell script(prts.sh) to count the occurences of the...

Segmentation fault when calling a function in c

c,segmentation-fault
The code is to print given numbers in a spiral fashion, like, 1 2 3 8 9 4 7 6 5 I am getting a segfault while calling the function godown(). #include<stdio.h> int o[100][100],c[100][100],p=0,q=0,m=0,n=0,a,i,j,r,d,l,u; void goright(int r); void goleft(int l); void goup(int u); void godown(int d); int main() { int...

Using chdir() Causes Segmentation Fault

c++,segmentation-fault,chdir,unistd.h
I'm writing a batch emulator as a personal project. I'm trying to implement the cd command using chdir() from unistd.h. However, using this causes a segfault. main.cpp: #include <cstdlib> #include <iostream> #include <string> #include <vector> #include <stdio.h> //Custom headers #include "splitting_algorithm.hpp" #include "lowercase.hpp" #include "chdir.hpp" //Used to get and print...

segmentation fault after memcpy and free

c,segmentation-fault,free,memcpy
I a new C programmer. I try to remove one element from a static array and copy the rest on a dynamic array. The program worked fine but after free() I get a segmentation fault. I think that the pointer temp is pointing to some memory on stack that's why...

Why does this code to randomize a list of song names generate a segmentation fault?

c,arrays,function,random,segmentation-fault
I am trying to create a program that receives from the user 12 song titles and then forms a set list in a random order. I have used the gets() function and memory allocation so that the input is placed into an array like this: argv[0] = song1, argv[1] =...

Program immediately terminates with segfault when using scanf

c,pointers,segmentation-fault,scanf
When I use gets() or fgets() instead of scanf(), the program does execute completely but prints segmentation fault(core dumped) in the end! I don't understand why am I getting segfault in both the cases. Here is the code for converting an infix to postfix exp using stacks. #include<stdio.h> #include<stdlib.h> #include<string.h>...

Seg fault on GCC4.6 when trying to move a large bitset, is this a compiler bug?

c++,gcc,segmentation-fault,undefined-behavior,move-semantics
I've experienced a problem with a crash due to return an rvalue of a bitset when the bitset is large. Is this a compiler bug or have I mistakenly done something that caused undefined behaviour? The code below crashes on GCC 4.6.3 with the -std=c++0x flag set. #include <bitset> //...

qsort Segmentation Fault structs

c,arrays,struct,segmentation-fault,segment
So, my first question here, please be patient with me: My task is to sort an array of structs (name, surname and another struct for the birthday, which consists of the year, month, day). I have to sort by birthdate and by using qsort. My problem is, I looked up...

Segmentation fault in parsing

c,parsing,segmentation-fault
I'm trying to build a code that would convert HTML into text file. #include <stdio.h> #include <stdlib.h> #define BUFLEN 2048 int main(){ FILE *fp; fp = fopen("tc.txt", "r"); int i = 0; int j = 0; char storage[BUFLEN]; char title[100]; fread(storage, 1, sizeof(storage), fp); for(i=0; storage[i]; i++){ if(storage[i] == '<'...

Creating shared_ptr seems to cause segfault

c++,segmentation-fault,gdb,valgrind
I have the following function static node_ptr make_node_ptr() { return node_ptr(new node()); } where using node_ptr = std::shared_ptr<node>; I tried to find my segmentation fault with valgrind and gdb. In both I get more or less the same stack trace. Program received signal SIGSEGV, Segmentation fault. 0x00007fff8f5d7e82 in szone_malloc_should_clear ()...

SegmentationFault linux AFS [closed]

c++,linux,segmentation-fault
Can someone tell me how to debug segmentation fault in linux VIA AFS. I would appreciate if someone could refer me command. I am able to run my code via compiler but not on when i give command g++ -o file1 file2.cpp----> ./file----->segmentation fault

Segmentation fault when char array is declared inside function

c++,arrays,segmentation-fault
Today I experienced a strange behaviour while writing some C/C++ code and I was wondering what the explanation is. Here are two code snippets showing the same function, the first one has the static array "char comp[BUFFLEN];" declared outside the function while for the second one it is declared inside...

c - writing to shared memory segment causes segmentation fault

c,segmentation-fault,shared-memory
I'm having problems with writing to shared memory segment. Here's the code: EDIT: after I removed that == (mistake), now I'm getting Bus Error (Core Dumped), here's the edited code: // Struct for data from shared memory typedef struct { pthread_mutex_t shared_mutex; int last_used_job_id; } shared1; static void *job_generator(void *param)...

c++ GMP mpz_init() causes Segmentation Fault 11

c++,segmentation-fault,gmp
I have a program that receives a Segmentation Fault 11 notice the simplicity of the main. Here is my entire working script: #include <iostream> #include "gmp.h" void makeprime () { // *********************** VARIABLE DECLARATION *********************** // // initilize the variables as gmp class instances mpz_t l, rand; unsigned long seed;...

Segfault issue possibly involving scope

c,arrays,segmentation-fault
I have a header file containing char** globalScreen, which I have allocated memory inside of a function which is called as soon as the program starts in main.c. I have allocated the memory the following way; char (*globalScreen)[SSIZEY] = malloc(SSIZEX * SSIZEY * sizeof(char*)); int i,j; for(i = 0;i <...

Error with stoi and debugged with gdb [closed]

c++,c++11,segmentation-fault,coredump
My OS is ubuntu 14.04, laptop, i7. The g++ version is g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2. I tried to run a simple code to test stoi: #include <string> int main() { std::string s = "123"; int i = std::stoi(s); } When I compile it with: g++ -g prueba2.cpp, I get: prueba2.cpp:...

PHP Preg Replace on SVG file causes Segmentation Fault

php,regex,svg,segmentation-fault,preg-replace
When I try run this command on my SVG file, the browser says no data has been received and I get an error in my Apache log file. preg_match("/(<g(\s|\S)*?<\/g>)/i", $SVG, $Matches); My SVG file is here. And the actual error that I'm getting is this [core:notice] [pid 20852] AH00052: child...

Segmentation fault with Sun c++ compiler

c++,segmentation-fault,multimap,cc
I have a class AccountMap in AccountMap.h and AccountMap.cpp. It reads stuff from a binary file (which is ok) and outputs stuff. It runs perfectly fine on g++ compiler but gives me a seg fault on sun c++ CC compiler when I use new operator for the 10 iteration in...

C program: Segmentation Fault [closed]

c,arrays,pointers,segmentation-fault
I am currently trying to solve a task, which is quite hard for me, a beginner to C, to handle and so i came to this point where I do not know what to do anymore. My task is to implement polynomials with several functions.... The functions should be clear...

Seg Fault on cin. Tried the endl thing. Lousy at gdb

c++,vector,segmentation-fault,gdb
I getting a strange segfault I don't understand. [email protected] ~/code/byun-sp15 $ g++ -g all_pairs.cpp [email protected] ~/code/byun-sp15 $ ./a.out Please enter filename: table.txt Segmentation fault As you can see from the cout staements, it is segfaulting right in cin>>filename; Here's the main function: #include <iostream> #include <fstream> #include <vector> #include <sstream>...

Unknown Segfault

c,segmentation-fault,gdb
Thanks in advance for your help. I've done all the research I could trying to debug this. Adding printf's seems to change where the segfault occurs. I'm hardly familiar with gdb but somehow the program ran without issue in it. I've got some quantum observation problem going on. Let's get...

get N-th byte from mmaped file

c,memory,segmentation-fault,mmap,coredump
I'm new at C and now I'm learning about mmap. I want to get N-th byte from mmaped file, but I get this error Segmentation Fault (core dumped) When I test my program with gdb I get that something is wrong with this line printf("%d\n", (int) data[sk]); and then I...

Calculate the sum of two numbers using thread

c,unix,segmentation-fault,pthreads
I have this little program I wrote to read two numbers from a user and calculate their sum using a thread function, which is also responsible for displaying the result on the screen. int global[2]; void *sum_thread(void *arg) { int *args_array; args_array = *(int**)arg; int n1,n2,sum; n1=args_array[0]; n2=args_array[1]; sum =...

Delete raw pointer after creating a shared_ptr from it

c++,segmentation-fault,shared-ptr,raw-pointer
If I do the following, int* p = new int(10); std::shared_ptr<int>(p); delete p; What happens here? Is the shared_ptr invalid after deletion of the raw pointer? Is there any way to ensure memory access safety in such a scenario?...

Segmentation Fault in the given Program

c++,segmentation-fault
I often come across segmentation fault,and even though I know it occurs because of accessing restricted memory,I don't seem to get as to how do I rectify it. I usually come across it, when I am calling another function. #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using...

Segmentation fault when trying to allocate memory for a string via a pointer-to-pointer function call

c,pointers,file-io,segmentation-fault,malloc
I Initiate my string and call my function like this: int main() { ... char *fileBuffer; readFileToBuffer("/tmp/file.txt", &fileBuffer); ... } The purpose of this function is to get the contents of file.txt and put it into the fileBuffer variable. Because the content of file.txt is dynamic, I allocate the memory...

C/SDL - Segmentation fault when program run multiple times in succession [closed]

c,segmentation-fault,sdl
I've encountered some strange behaviour in my program. When I run my program 3-4 times and close it immediately, it's starting to give me segmentation faults before it even starts. When I haven't opened it for a while it opens the first 2-3 times without problem and then again seg...

segmentation fault while calling strlen with a previously allocated pointer

c,pointers,char,segmentation-fault,strlen
Up until now I have had some code that worked perfectly regarding a variable called wfiles. wfiles is initialized within my main file: char* wfiles = ""; Which as far as I can tell C has no complaints. Next the wfiles variable is allocated in a switch statement: switch (c)...

Segmentation Fault on second time calling mysql_fetch_row

c++,mysql,segmentation-fault
I am writing some MYSQL embedded into C++ and I am getting some behavior that I haven't been able to pinpoint the cause of. I have a main function and two functions that can search by author or publisher. If I search by author, I get the proper results the...

a simple program to sort random numbers

c++,segmentation-fault
i just made a simple program that sorts random numbers in order: #include<iostream> #include<cstdlib> #include<ctime> using namespace std; void swap(int &a,int &b){ int temp=a; a=b;b=temp; } int main(int argc, char* argv[]){ static int option; if(argv[1])option=atol(argv[1]); else option=0; srand((unsigned)time(0)); const int size=option; int array[size]; for(int i=0;i<size;i++){ array[i]=rand()%size; } //algorithm for(int i=0;i<size;i++){...

Segfault for invalid long option

c,segmentation-fault,command-line-arguments,getopt,getopt-long
I have the following code: struct option longopts[] = { {"version", no_argument, 0, 'v'} }; if (argc > 1) { int c; int longindex; while ((c = getopt_long (argc, argv, "v", longopts, &longindex)) != -1) { switch (c) { case 'v': puts (VERSION_INFO); exit (0); case '?': exit (1); }...

scanf get multiple values at once

c,char,segmentation-fault,user-input,scanf
I need to get in one single shot different inputs from one single line. In particular I need to get a single char and then, depending on which char value I just read, it can be a string and an int or a string, an int and another string and...

c++ unsigned char array allocation - segmentation fault

c,segmentation-fault,malloc,memset,unsigned-char
I can't figure out what seems to be the problem that I get a segmentation fault from this: #include <stdio.h> #include <stdlib.h> #include <cstring> void alloc(unsigned char *data) { data = (unsigned char *) malloc(20); memset(data, 0, 20); } void main() { unsigned char *data = NULL; int i; alloc(data);...

intel AVX multiplication error in C,

c,segmentation-fault,avx
When I run a simple series of load, subtract and multiply using the AVX intrinsics I'm constantly getting the following error, Process terminating with default action of signal 11 (SIGSEGV) ==2995== General Protection Fault from the C code, double res[4] = {0.0, 0.0, 0.0, 0.0}; for(int i = 0; i...

Failed to add a node to linked list

c,pointers,segmentation-fault,malloc,dynamic-memory-allocation
i make the changes but i cant add more than 2 nodes its will freez but if 1 or 2 node will work well what is the reason??? i gave_up i can do nothing for that This is my code till time #include <stdio.h> #include <stdlib.h> #include <ctype.h> struct info{...

what‘s the difference between shell-cmd ’dmesg‘ and file ’/var/log/dmesg‘

linux,segmentation-fault
I think the ouput of the command dmesg should be the subset of the file /var/log/dmesg. But when I am debugging,the segmentation fault information can only be seen in the output of the command dmesg. What is the difference between them ?...

A Safe Segmentation Fault?

c++,c,multithreading,segmentation-fault
In my simple game, there is a thread that does the drawing to the screen. I made my program throw an exit signal as an exception when I press the ESC key. Whenever this exception is caught, the clean-up function is called and the program exits. This 'expected' segmentation fault...

Seg Error while erasing from multiset C++

c++,stl,segmentation-fault,multiset
I don't know whats making this code give an error. It's a simple multiset. No compilation errors, but segmentation fault on the machine while executing. g++ version : 4.8.2 Machine : Ubuntu 14.04 #include <cstdio> #include <set> using namespace std; struct compare { bool operator() (int lhs, int rhs) {...

Trouble using fscanf to read coordinates in C

c,segmentation-fault,coordinates,fscanf
I know similar questions have been asked but none of them seemed to solve my problem. I get Segmentation fault (core dumped) when I run my code. The first line in "data.dat" has the total number of points in the file and the next lines have the point coordinates (in...

Segmentation fault while trying to test fgets()

c,segmentation-fault,fgets
I'm trying to write a program to experiment with the fgets() function, with which I have little experience. I keep getting a segmentation fault 11 error when I run the program (but no errors when I compile it) and I don't know enough about fgets to know what is going...

Segmentation fault with variable function arguments

c,variables,segmentation-fault,arguments
My program compiles without error, but when I run it, it quits with a segmentation fault after I input a value such a radius. I have it constructed to take a variable amount of arguments, but I suspect it could be something to do with the way I call each...

how to use long long int and remove segmentation fault

c,segmentation-fault
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { long long int a[10^9],sum=0; int n,i,length; scanf("%d",&n); for(i=0;i<n;i++) { if(0<=a[i]<=10^10) { scanf("%lld",&a[i]); } } for(i=0;i<n;i++) { sum=sum+a[i]; } printf("%lld",sum); /* Enter your code here. Read input from STDIN. Print output to STDOUT */ return 0; } i dont know...

Fortran Seg Fault when assigning Matrices

matrix,segmentation-fault,fortran,derived-types
[UPDATE] the code and a few sentences were changed to reflect a realization explained in my second comment. The code should compile with the line below, however, I have an older gfortran and may not be seeing some errors that you might. gfortran BLU_implementation_copy.f90 -o BLU_implementation_copy.x I'm getting an incredibly...

Can anyone see why this program generates a segmentation fault

c,segmentation-fault
I am writing a program to implement a map using a singly linked list. After writing and including this insert method the program generates a segmentation fault but i'm not sure where this comes from. int map_insert(Map *theMap, char *theKey, void *theItem){ node *newNode = malloc(sizeof(node)); node *cursor = theMap->root;...

Vector of Vector of object

c++,arrays,vector,segmentation-fault
I have a little problem using vector of vector. I have a std::vector<AObject*> _word in my class, I build my word : AObject *object = new Letters(); _word.push_back(object); Later I use : for (size_t i = 0 ; i < _word.size() ; ++i) _word[i]->draw(); ^ THIS WORKS BUT I NEED...

In C, why can't you declare a pointer and make it point to a number directly, without malloc?

c,pointers,segmentation-fault
In C, why do X and Y work but not Z? //X int num = 5; int *ptr; ptr = &num; //Y int *mptr = (int *)malloc(sizeof(int *)); *mptr = 5; //Z int* myptr; *myptr = 5; In Z, I declare a pointer and try to make it point to...

Why it is giving error “segmentation fault”?

c,segmentation-fault,char-pointer
I want to input two strings at different memory locations but after taking the first input it shows an error "segmentation fault(core dumped"). I am not getting what's wrong with this code. #include <stdio.h> #include<iostream> using namespace std; int main() { char *str; int i; scanf("%s",str+0); scanf("%s",str+1); return 0; }...

segmentation fault issue # n

c++,segmentation-fault,coredump
Is this command line wrong? FILE *TM = fopen("TM","r"); When I compile my code with: g++ -O3 -march=corei7 -mtune=corei7 -std=c++11 prueba3.cpp -o prueba3 -lstdc++ or g++ -O3 -march=corei7 -mtune=corei7 -std=c++0x prueba3.cpp -o prueba3 -lstdc++ and then run the executable, I got: Segmentation fault (core dumped) When I debug the code...

Segmentation fault when using if statement with pointers (BST tree)

c++,c,pointers,segmentation-fault,binary-search-tree
I'm trying to implement a binary search tree in C, more specifically looking for the predecessor. However, whenever I try to run the program I get the segmentation vault. Here's the code in question: #include <stdio.h> #include <stdlib.h> struct tree { int a; tree *left; tree *right; tree *prev; }*root=NULL;...

Shared Memory UNIX Segmentation Fault (fork()) in C++

c++,unix,segmentation-fault,fork,shared-memory
I have a problem with shared memory and processes. I attach an area to the shared memory, I make a child proccess but when I try to modify the shared memory area in the father process I only get segmentation fault, it just can be modified in the child. .h...

Why does gets work even when I have not allocated memory to it?

c++,c,segmentation-fault,gets
I recently answered a question here in SO where the problem was to input a few sentences of code and print it. My code is this: #include<stdio.h> int main() { clrscr(); int line, i; char (*string)[100]; printf("How many line?\n"); scanf("%d",&line); getchar(); for(i=0;i<line;i++) { gets(string[i]); } printf("You entered:\n"); for(i=0;i<line;i++) { puts(string[i]);...

A segmentation fault

c++,c,segmentation-fault
I have try the following code to judge prime: const int N = 200000; long prime[N] = {0}; long num_prime = 0; int is_not_prime[N]={1,1}; void Prime_sort(void) { for( long i = 2 ; i<N ; i++ ) { if( !is_not_prime[i] ) { prime[num_prime++] = i; } for( long j =...

how to scan a string input

c,arrays,string,segmentation-fault,scanf
I am trying to get an input string by using scanf() in a function, but it keeps failing and I don't know why. Here is part of my code. typedef struct node { int id; char * name; char * address; char * group; struct node * next; } data;...

segmentation fault while generating a number of random numbers using rand() in C

c,arrays,random,segmentation-fault
I am writing a program where I need to set a number of elements (specified by the blanks variable) of a 2D array (board) to be 0.(array size is 9x9) These locations has to be picked up in random and once I use the following code segment to do it,...

aio_write and memset invalid argument and Segmentation Fault (core dumped)

c,segmentation-fault,memset,invalid-argument
I'm trying to fill my file with 'a' characters and I need tu use aio_write. Here's my writing function int da_aio_write(const int d, struct aiocb *aiorp, void *buf, const int count){ int rv = 0; // memset( (void *)aiorp, 'a', sizeof( struct aiocb ) ); // <--- I get Invalid...

Segmentation fault strcmp in c

c,debugging,segmentation-fault,gdb,strcmp
I am trying to run a program in c that takes in a text file and string from the user and then searches the file for that string. It keeps getting a segmentation fault and gdb is pointing me towards this function but I am not sure what the problem...

Assembler with C - Segmentation Fault

c,assembly,linker,segmentation-fault
I have two files: f1.S where is my Fibonacci function (counts n-th member of Fibonacci sequence) written in assembler language and f2.c where the Fibonacci function is called. Here are these files: f1.S .global fibonacci fibonacci: push %rbp movq %rsp, %rbp push %rax movq 16(%rbp), %rax cmp $0, %rax je...

Segfault while creating a vector of avx vectors

c++,vector,segmentation-fault,intrinsics,avx
for my current project I need to create a vector of 256bit AVX vectors. I used myVector = vector<__m256d>(nrVars(), _mm256_set1_pd(1.0)); which worked fine once but after executing the line twice it gives me a segmentation fault. I was able to come up with the following piece of code vector<__m256d> temp;...

Transferring argv into a new array

c++,c,arrays,pointers,segmentation-fault
I am new to both C and C++ and cant figure out why this code is not working. I have seen this but I still didn't get it. I get a segmentation fault (11) and do not see the argv array printed from the testArray. My code so far #include...

Array defined as “_end[LEN]” causes segmentation fault in C/C++ [duplicate]

c++,arrays,segmentation-fault
This question already has an answer here: What are the rules about using an underscore in a C++ identifier? 5 answers I tried to define a global array, named _end, of size ~1000 in C/C++, but I got a segmentation fault even when I simply iterated it. Is the...

Segmentation fault in list of objects

c++,design-patterns,vector,segmentation-fault,copy-constructor
I didn't thought that this would be so hard, but I have the following program: In a multithreaded environment (using the ACE-Framework and OpenMP) I use the Observer pattern. In the init-routine of the Observer-controller-thread (ActiveObject) I create a vector of ConcreteObservers (for notifying them with their discrete job) like...

Code runs on OSX, Segmentation fault in Codeblocks VMbox

c++,segmentation-fault,codeblocks
So when I run this in OSX it works correctly, but my class requires for it to work in a Xubuntu VMbox using Codeblocks. When I try to run it in Codeblocks or terminal of VMbox I get the error 'Segmentation fault (core dumped)'. Does this maybe have something to...

Why realloc of char** gives Address is 0 bytes after alloc'd

c,segmentation-fault,realloc,dynamic-allocation
I have the following code: #include <stdio.h> #include <string.h> #include <stdlib.h> int main(int argc, char *argv[]) { char* filename = "file_prefix.txt"; FILE* file_prefix = fopen(filename, "r"); char buff[1024]; int i = 0; char** prefix = NULL; char c = fscanf(file_prefix, "%s", buff); while ( EOF != c ) { printf("%d...

Segmentation error when comparing member of an array with integer

c,arrays,segmentation-fault
I've been writing an linked graph realisation program, with the ability to find shortest path. This is my funcion for printing. I have an array with paths marked I try to sort them here, and print, but in the line if(pathTaken[i] == src) I am getting a segmentation error. What...

Maximum recursive function calls in C/C++ before stack is full and gives a segmentation fault?

c++,recursion,segmentation-fault
I was doing a question where I used a recursive function to create a segment tree. For larger values it started giving segmentation fault. So I thought before it might be because of array index value out of bound but later I thought it might be because of program stack...

Another coredump issue in C

c++,c,segmentation-fault,coredump
when I compile my code with g++ -g -o prueba prueba.cpp -lstdc++ -O3 -march=corei7 -mtune=corei7 -std=c++0x After debugging with g++ -g prueba.cpp, I got these: prueba.cpp:24:6: error: ‘stoi’ is not a member of ‘std’ tm = std::stoi(string2); ^ prueba.cpp:34:7: error: ‘stoi’ is not a member of ‘std’ ler = std::stoi(string1);...

Dynamically allocated doubly linked circular list class instances segfault C++

c++11,linked-list,segmentation-fault,dynamic-programming
Using this template class works perfectly fine when main operates with constructed variables of type dlring, yet my goal is to allow dynamic allocation, so I can handle a non-predefined number of doubly linked circular lists to allow usage of such functions as: Splitting a list into two by either...

C - Segmentation fault when adding edge to graph

c,graph,segmentation-fault,edge,adjacency-matrix
I have to use an undirected weighted graph (adjacency matrix) for this program. typedef struct graph { int n; /* Number of vertices */ Boolean *visited; /* Will be later used */ double **mat; /* It has to be a double */ } Graph; Function used to initialize the graph:...

segfault when using strtok_r

c,segmentation-fault
i want to split a string in c with this code: char *search = "+" ; char *temp1; char *temp2; char *saveptr1, *saveptr2 ; int operand1 ; int operand2 ; int result ; char sumBuff [5][25] temp1 = strtok_r(sumBuff[sumCounter-1], search, &saveptr1) ; operand2 = atoi(strtok_r(NULL, search, &saveptr1)); temp2 = strtok_r(temp1,...

Accessing a 3D array in C++

c++,multidimensional-array,segmentation-fault,dynamic-memory-allocation
I have a char*** and I dynamically allocate some memory to it. However, when I try to assign some values to it, I get segmentation fault. The size I am using is not too big. It works when I create char[768][1024][3] but doesn't work when I add this dynamically with...

Predefined string segfaults when copied to by strncpy()

c,segmentation-fault,malloc,strncpy
Why is it that ... char *dst = (char*) malloc(sizeof(char) * 11); char *src = "ABCDEFGHIJKLMNOPQRSTUVQXYZ"; strncpy(dst, src, 10); ... works fine, but ... char *dst = "ABCDEFGHIJ\0"; char *src = "ABCDEFGHIJKLMNOPQRSTUVQXYZ\0"; strncpy(dst, src, 10); ... or even ... char *dst = "ABCDEFGHIJ\0"; char *src = "KLMNOPQRST\0"; strncpy(dst, src, 10);...

Closed system shellcode experimentation (segfault)

c,sockets,segmentation-fault,shellcode,experimental-design
I am taking an online software security course. I am attempting to experiment with shellcode. I have written a vulnerable server, an injection program, a (probably broken) shellcode I convert to assembly, that I then strip with a python script. I then compile and run everything with a shell script....

Asm x86 segmentation fault in reading from file

assembly,x86,segmentation-fault,mmap
I'm receiving segmentation fault in 5th line of loop2. This is the part of my code which is reading values from mmapped file byte by byte. Could you please tell me what I'm doing wrong? .global _start _start: mov $2,%rax mov 16(%rsp),%rdi mov $02,%rsi syscall cmp $0,%rax jl exit 1:...

segmentation fault in height balance tree code [closed]

c++,segmentation-fault,avl-tree
When i try to do like this: int main(){ node * root; insert(&root, 10); insert(&root, 20); insert(&root, 30); insert(&root, 40); insert(&root, 50); insert(&root,60); preorder(root); return 0; } my code runs fine and gives output. But when i do this: int main(){ node * root; insert(&root, 10); insert(&root, 20); insert(&root, 30);...

postfix calculator having trouble with segfaults

c,linked-list,segmentation-fault,postfix-notation
i'm trying to create a postfix calculator using linked-list. when i compile, it doesn't show any errors but when it's executed it would show Segmentation Fault. i don't know how to deal with this, please help. here's my code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> typedef struct node...

Segmentation fault when initializing a std::vector

c++11,segmentation-fault,stdvector
What are the possible causes of a segmentation fault at the following line? std::vector<double> d(8); ...

Segmentation fault when writing data to a dynamic array

c,file,struct,segmentation-fault,records
My assignment is to write a file that displays an unknown number of records entered by the user. Each record has the following fields: First Name, Last Name, Address, City, State, Zip Code, and Phone Number. I assumed the best way to do this would be to define a struct...

How would checking if a value in a data structure equals NULL return a segmentation fault?

c,struct,segmentation-fault
I am trying to load a hash table of node*(s)- typedef struct node{ char word[LENGTH+1]; struct node* next; }node; (don't worry about length, it is defined in the file that calls this) -into memory, but this: // make hash table node* hashtable[729]; node* new_node = malloc(sizeof(node)); node* cursor = NULL;...

Segmentation fault spoj assist

c,pointers,struct,segmentation-fault,runtime-error
I have been trying this problem on SPOJ. I have been getting run-time error (SIGSEGV) but the code works perfectly on my computer can someone tell me what is my mistake? #include<stdio.h> #include<stdlib.h> struct node { struct node * next; int x; }; int main() { struct node *head,*temp,*temp2; int...

segmentation fault with a game of life function

c,segmentation-fault
I'm getting a segmentation fault in this function and I have no idea why. I can't isolate the problem with GDB, it just tells me this is the function that fails and I'm just looking to see if anyone sees something immediately wrong. I'm trying to learn C and optimize...

Call to malloc() causes unexplicable program crash

c,string,segmentation-fault,malloc,dynamic-memory-allocation
I'm having an issue where a call I'm making to malloc() causes my program to crash. Here's the code: void update_item(char *input, item_t *new_node){ int i, count, shelf, weight, price, quantity; char *name; char *specifier; char aisle[1]; count = 0; /*Find name of the new item and assign to the...

Template detects if T is pointer or class

c++,templates,segmentation-fault,sfinae
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;...

gdb giving a function name followed by a number instead of file and line number

c,debugging,segmentation-fault,gdb
I have a segmentation fault in my program, and I'm using gdb to identify where it's happening. However, I am not able to see a clear line number where the error is occurring. Below is a screenshot of my output. Program received signal SIGSEGV, Segmentation fault. [Switching to Thread 20065168...

Ignoring return value of fscanf and Segmentation fault

c,segmentation-fault,fopen,compiler-warnings,fscanf
I was wondering how to solve a Core dumped issue on my C code. When I compile it with: g++ -g MatSim.cpp -o MatSim -lstdc++ -O3, I get three warnings, this is one (The other two are similar and are only differentiated by the string variable name): MatSim.cpp: In function...

C++ accessing vector of vector got segmentation fault

c++,vector,segmentation-fault
I created a vector of vector (10*10000) and try to access this vector through member function. but I got a segmentation fault. I don't know what's wrong here... Here is Simple.h class Simple { private: std::vector<double> data_row; std::vector<std::vector<double> > data; public: Simple():data_row(10000), data(10, data_row){}; /*initialize data vector*/ int getSampleCounts(std::istream &File);...

std::ifstream read reading wrong size for large numbers

c++,segmentation-fault,valgrind,ifstream
I have a Problem while reading a lare chunk of binary data from a file at once. Reading the same amount of bytes bytewise is working. I have to following sample code: std::ifstream inFile; inFile.open("example.bin", std::ios::binary | std::ios::in); uint32_t bytesToAllocate = static_cast<uint32_t>(this->sectionLength)-4; this->binaryData = new uint8_t[bytesToAllocate]; inFile.read(reinterpret_cast<char*>(&this->binaryData), bytesToAllocate); if i...

How to avoid SEGMENTATION ERROR for the code below?

c++,segmentation-fault,maps,dynamic-programming
I'm getting Segmentation error for the code below. This is a solution to the SPOJ problem "Coins". I went through How to avoid SIGSEGV? and I made sure not to use uninitialized pointers, not to access out of memory etc (given n ≤ 109). I know that an array a[1000000000]...

Program gives segmantation fault while using wildcard (*) as first input

c,pointers,memory,char,segmentation-fault
/*Input string argument can be up to 3 integer numbers, separated by spaces. A wild card value, represented by a * character can replace any one of the integer numbers.*/ #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> void parse(char *data); int program = 0, version = 0, process =...

Large vector “Segmentation fault” error

c++,vector,segmentation-fault
I have gathered a large amount of extremely useful information from other peoples' questions and answers on SO, and have searched duly for an answer to this one as well. Unfortunately I have not found a solution to this problem. The following function to generate a list of primes: void...

SDL_Keycodes are too big for storage

c++,segmentation-fault,sdl-2,signed-integer
While searching up methods of detecting multiple keys at once in SDL 2, I came across this piece of code for SDL 1.x: //author: Rob Loach // Global key buffer bool keys[256]; while(SDL_PollEvent(&mainEvent)) { if(mainEvent.type == SDL_KEYDOWN) { keys[mainEvent.key.keysym.sym] = true; } else if(mainEvent.type == SDL_KEYUP) { keys[mainEvent.key.keysym.sym] = false;...

Segmentation fault caused by memcpy despite counting every bit

c++,segmentation-fault
I am trying to copy data from an array of characters into a member of my class using memcpy. I set a breakpoint in the debugger write before memcpy. I checked all of the variables that I will be using and the i calculated how much space is left in...

Segmentation fault in parsing code

c,parsing,segmentation-fault
I have this code that extracts strings between two specified strings specifically <title> and </title>. But when I run the program, I get Segmentation Fault: 11 Any solutions? int main(){ struct stat st; stat("test.txt", &st); int size = st.st_size; printf("%d\n", size); FILE *f = fopen("test.txt", "rb"); char *bytes = (char*)malloc(size);...

Strcpy just crashes the second time I use in the same context

c,string,segmentation-fault,dynamic-memory-allocation,strcpy
This code waits for a string entered by the user, then the program should sort it immediately in an pointer array. The problem is in the case cop<0. I don't know what's the problem with strcpy() . If anyone could help, I'll be thankful. Here is the code : #include...