segmentation-fault

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

戏子无情 提交于 2019-12-16 20:05:06
问题 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

SDL Saving window as BMP

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-16 18:03:45
问题 I am writing a program in SDL and C and I want to be able to save the window as an image. This is my code: screen = SDL_GetWindowSurface(win); SDL_SaveBMP(screen,"screen"); But when I execute it I get: Segmentation Fault From other sources I gather that its about pointers and memory access. Any help? 回答1: Call SDL_LockSurface on a window surface before saving bitmap, and SDL_UnlockSurface after that. 回答2: SDL_Surface *sshot = SDL_CreateRGBSurface(0, 750, 750, 32, 0x00ff0000, 0x0000ff00,

Segmentation Fault When Using Variable To Initiate Array

无人久伴 提交于 2019-12-14 03:49:19
问题 This is my first question posting so sorry if I make make any faux pas' Using C In my program I create a global variable pointer double *correlationData; In main I create this local variable: int arrayLength = 0; in main I have an if statement inside a for loop which contains arrayLength++; after the for loop I initiate an array and assign it to the pointer double correlationArray[arrayLength]; correlationData = correlationArray; but I get a "segmentation fault" at this part of the code and I

Creating own malloc for an assignment. Getting a segmentation fault

烂漫一生 提交于 2019-12-14 03:35:37
问题 The segmentation fault occurs at the point with the comment. I think it has to do with the fact that I'm not initializing the head and tail Nodes. I've tried to initialize the to NULL as well and that didn't work. Unfortunately, I don't really know how to initialize them without using malloc. Any help would be great. Thanks. #include <stdio.h> #include <stdlib.h> #include <unistd.h> //the structure of the node in the linked list typedef struct Node{ int size; int status; struct Node* next;

Another coredump issue in C

荒凉一梦 提交于 2019-12-14 03:32:11
问题 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); ^ prueba.cpp:77:8: error: ‘stoi’ is not a member of ‘std’ C[i]=std::stoi(string); ^ The way I declare stoi as std::stoi was based on this example. And the broken block is

C++ trouble with deallocating memory taken by vector elements

心已入冬 提交于 2019-12-14 03:23:56
问题 So the problem is that when i try to push non-dynamic obj to playerList or when I try to delete n I get segfault (core dump). I assume that the problem is caused when Helper class is being destroyed so the vector also is being destroyed so it tries to destroy object in itself which does not exist anymore. However when i use playerList.clear() the problem still exist. I think i could just destroy objects in playerList() with ~Helper(). But I would like to know why i cannot use non-dynamic

Segmentation fault found but message before that is being optimized out

℡╲_俬逩灬. 提交于 2019-12-14 03:18:24
问题 I wrote the following code in GDB online debugger : #include <stdio.h> int main() { printf("jkjkkjkj"); int p , n; FILE *fp; printf("jkjkkjkj2"); fp = fopen("abc.txt","r"); while ( (n = getc(fp))!= EOF) { printf( "the chareacter here is %d \n", n); } n = fclose(fp); return 0; } While executing the code I am getting a segmentation fault at the line where I am trying to fetch the characters from the file. I know that as the file does not exist the segmentation fault error is coming. However,

Segfault when allocating large array in Fortran

我是研究僧i 提交于 2019-12-14 03:13:54
问题 I have a very simple example of a strange segfault I am having and it is as follows: program big_array_segfault integer :: nX = 13000 integer :: nY = 100000 real(kind = 8), allocatable :: bigarr(:,:) allocate(bigarr(nX, nY)) end program big_array_segfault Note that I have 20 GB of RAM to work with and this does not even begin to approach that. Everything I have seen online suggests that this may be a problem with Stack space vs Heap space but I don't know how to control the memory in that way

clang segfaults when compiling LLVM IR

安稳与你 提交于 2019-12-14 03:08:45
问题 I am trying to compile a LLVM IR file. However, when I try to compile it, I get this stack trace: warning: overriding the module target triple with x86_64-pc-linux-gnu [-Woverride-module] #0 0x00007f415c9179fa llvm::sys::PrintStackTrace(llvm::raw_ostream&) (/usr/lib/llvm-6.0/bin/../lib/libLLVM-6.0.so.1+0x8549fa) #1 0x00007f415c915c76 llvm::sys::RunSignalHandlers() (/usr/lib/llvm-6.0/bin/../lib/libLLVM-6.0.so.1+0x852c76) #2 0x00007f415c915dab (/usr/lib/llvm-6.0/bin/../lib/libLLVM-6.0.so.1

Segmentation fault on initializing a 2D array

此生再无相见时 提交于 2019-12-14 01:33:52
问题 I've checked that my code is properly carving out memory space, but as soon as I try to initialize my 2D array to some values and then sum up the values, I receive a segmentation fault on just a 2x2 array. I would like to eventually scale my code up to a much larger array, but I can't even get it working here. I know there are many posts about segmentation fault regarding malloc and 2D arrays, but I've been unable to find one that helps me with my problems since my C knowledge is just