segmentation-fault

Concatenation of string and int results in segmentation fault in C

左心房为你撑大大i 提交于 2019-12-13 08:17:35
问题 I am not sure what I'm doing wrong. I'm trying to concatenate hostname with pid to create id . char *generate_id(void) { int ret; char id[1048]; char hostname[1024]; pid_t pid = getpid(); //hostname[1023] = '\0'; if ((ret = gethostname(hostname,1024) < 0)) { perror("gethostname"); exit(EXIT_FAILURE); } sprintf(id, "%s%d", pid); printf("hostname is %s\n", hostname); printf("The process id is %d\n", pid); printf("The unique id is %s", id); return id; } EDIT: Updated code after reading some

How to increase limit of array?

本秂侑毒 提交于 2019-12-13 07:52:49
问题 // C Program to find average of numbers given by user #include <stdio.h> #include <stdlib.h> #include <string.h> void main() { double sum = 0; int ii = 0,c; char buf[256], *token; printf("Enter the numbers to average on a single line, separated by space and press enter when done\n"); fgets(buf, 255, stdin); token = strtok(buf, " "); while (token != NULL) { sum += atof(token); ii++; token = strtok(NULL, " "); //Get next number } printf("Average is %lf", sum / (double)ii); } On line 8: char buf

Boost-Extension-Reflection How to fix segmentation fault error when compiling official sample not with bjam?

大城市里の小女人 提交于 2019-12-13 07:03:44
问题 So I try to port some Boost.Extension samples for standart IDEs - to make tham free from BJAM and to be able to work with them in standard ways across platforms. The sample I have trobules with now is described here. Here is my code port (library we try to load in main code file, main application, general all port idea is described here, and some current linux progress here (most of the samples really work as needed!)). When I compile this sample under linux it compiles, it finds library but

GDB Quirks with Thread Process

﹥>﹥吖頭↗ 提交于 2019-12-13 06:43:57
问题 I am debugging a process with multiple threads in GDB. I compiled the sole source file with the -g flag. However, while running in GDB, the following scenario occurs: Program received signal SIGSEGV, Segmentation fault. [Switching to Thread 0xb7fe2b70 (LWP 2604)] 0x00000011 in ?? () Prior to the switch, the particular thread executes a sleep(5); Why can't GDB identify the point from which the code "segfaulted"? 回答1: 0x00000011 is not a valid address, especially not for code. This tells us,

Segmentation Fault (core dumped) in pixel manipulation

跟風遠走 提交于 2019-12-13 06:23:13
问题 I am trying to get the bird's eye view of an image. I am altering the pixel intensity in two for-loops going over row and column respectively. birdeyeview_img.at<uchar>(p,q)=(int)undistor_img.at<uchar>(round(corr_x),round(corr_y); I am getting: Segmentation Fault (Core Dumped) . How do I alter the pixel intensity, without getting nay such error? My undistorted image is a grayscale image. Now I made few changes, it runs but doesn't give proper result, only a part of the code shows pixel

Pointers and Strings causing segmentation fault [duplicate]

徘徊边缘 提交于 2019-12-13 05:58:19
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: What is the difference between char a[] = “string”; and char *p = “string”; char *str = "Hello"; printf("%c",++*str); This gives segmentation fault on linux with gcc. The moment the first statement is changes to as char str[10] = "Hello"; It works. What may be the reason? 回答1: It is undefined behaviour to attempt to modify a string literal. The compiler is free to place it in read-only memory (as it probably

Segmentation fault

江枫思渺然 提交于 2019-12-13 05:46:57
问题 Tried to trace, but did not find a reason why the following code is giving "Access violation" in VC++, and segmentation fault in gcc.. #include <vector> #include <iostream> using namespace std; typedef struct node { std::string data; vector <struct node*> child; }NODE, *PNODE; int main() { PNODE head; head = (PNODE) malloc(sizeof(NODE)); head->data.assign("hi"); printf("data %s", head->data.c_str()); getchar(); } 回答1: Use new rather than malloc to create C++ objects on the heap. The following

Segmentation Fault 11 when running Swift app

旧时模样 提交于 2019-12-13 05:45:14
问题 I have just updated to the newest Xcode 6.3 beta and I am receiving an error that I can't figure out the solution to. When I run my app, I am getting the error: Command failed due to signal: Segmentation Fault 11 . I had a look through the full error message and I have taken the parts out that I think are relevant: (unresolved_dot_expr type='@lvalue String!' location=/Filename/FifthViewController.swift:423:19 range=[/Filename/FifthViewController.swift:423:9 - line:423:19] field 'text'

Split string with delimiter in C - segmentation faults, invalid free

送分小仙女□ 提交于 2019-12-13 05:18:40
问题 I wrote a simple code to split string in C with delimiter. When I remove all my frees, code works great but gives memory leaks. When I dont remove free, it does not show memory leaks but gives segmentation fault .. What is wring and how to solve it? #include <stdio.h> #include <stdlib.h> #include <string.h> unsigned int countWords(char *stringLine) { unsigned int count = 0; char* tmp = stringLine; char* last = 0; const char delim = '/'; while (*tmp) { if (delim == *tmp) { count++; last = tmp;

Segmentation fault when trying to access a vector - cpp

隐身守侯 提交于 2019-12-13 04:47:46
问题 I have written some code in c++. It reads in data from a CSV file and then simply prints the second line to the screen: vector<string> readCsvFileContent() { vector<string> buffer; try { ifstream inputFile; string line; inputFile.open("Input.csv", static_cast<std::ios::openmode>(std::ios::in) ); while (getline(inputFile,line)) { buffer.push_back(line); } inputFile.close(); } catch (ifstream::failure e) { cout<<"No file read"<<endl; throw e; } return buffer; } This function is called as