Tag: segmentation fault

C将char附加到char *

所以我试图将char添加到char* 。 例如,我有char *word = ” “; 我也有char ch = ‘x’; 我确实append(word, ch); 使用这种方法.. void append(char* s, char c) { int len = strlen(s); s[len] = c; s[len+1] = ‘\0’; } 它给了我一个分段错误,我理解为什么我想。 因为s[len]超出范围。 我该如何制作呢? 我需要清除char* ,如果我要使用char word [500]之类的东西; 一旦附加了一些字符,我该如何清除? 它的strlen总是500吗? 提前致谢。

你找到段错的原因是什么?

或者只是调试一般,你如何去寻找代码中的bug。 特别适用于C / C ++,但一般来说都是所有语言。 我一直试图找到这个令人讨厌的段错误的原因,但我喜欢自己找到它的挑战,而不是在网上发布它。 你对像我这样的padawan有什么建议吗?

映射区域的权限错误

尝试运行以下函数时出错: char* reverseInPlace(char* src) { //no need to alloc or free memory int i=0; int size=mystrlen(src); for(i=0;i<size;i++) { int j=size-i-1; if(i<j) { char temp; printf("Interchange start %d:%c with %d:%c",i,src[i],j,src[j]); temp=src[i]; src[i]=src[j];//error occurs here src[j]=temp; printf("Interchange complete %d:%c and %d:%c",i,src[i],j,src[j]); } } return src; } 我把这个代码称为: char* rev2=reverseInPlace(“BeforeSunrise”); printf(“The reversed string is %s\n”,rev2); 错误如下所示: Interchange start […]

CC / GCC但不是G ++的奇数分段错误(C / SDL2 / Linux)

发布的代码直接从流行的SDL2教程的示例中复制,以确保不是我犯了一些愚蠢的错误。 我对示例所做的只是更改有问题的图像文件的路径,我将类型bool更改为int,将false更改为0并将true更改为1.据我所知,没有任何特定于C ++的内容。 无论我做什么,一切似乎都有效,但是当用CC / GCC进行编译时(我认为这真的是同样的交易)我最终得到了一个分段错误,我怀疑是close(),我一直无法确定。 用G ++编译可以防止分段错误。 解决方案当然很简单,只需使用G ++,但我非常想知道问题所在。 main.c中: //Using SDL and standard IO #include #include //Screen dimension constants const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; //Starts up SDL and creates window int init(); //Loads media int loadMedia(); //Frees media and shuts down SDL void close(); //The window we’ll be […]

C – strtok上的意外分段错误(…)

我正在使用库的strtok(…),它似乎工作正常,直到结束条件,它导致分段错误和程序崩溃。 API声称strtok(…)将在没有更多令牌被找到时输出NULL,这意味着,我想,你必须捕获这个NULL才能终止你使用strtok运行的任何循环( …)。 我需要做什么才能捕获此NULL以防止程序崩溃? 我想象允许使用NULL作为终止条件。 我准备了一个SSCCE供你观察这种行为。 我需要strtok(…)来处理我正在编写的更大的软件,并且我得到完全相同的分段行为。 命令行的输出显示在此代码插图下方(是的,我知道您使用来封装库,但是我很难获得此post来显示代码库)。 我在Windows 8操作系统上使用gcc 4.5.3版本,下面显示了两种不同的方式,我想象一个人可以尝试在循环中捕获NULL。 #include #include #include #include #include #include #include main(){ char* from = “12.34.56.78”; char * ch = “.”; char * token = strtok(from, ch); printf(“%s\n”,token); while(token != NULL){ token = strtok(NULL, ch); printf(“%s\n”, token); } printf(“Broke out of loop!”); while(strcmp(token, 0) != 0){ printf(“%s\n”,token); token […]

c:在exec()中捕获一个在子进程中运行的段错误

编辑: 我正在尝试编写一个简单的冒烟测试,其中测试了所有选项和合理的参数。 我使用popen()来执行应该测试的程序。 使用这种方法不起作用,因为如果进程因信号(SIGINT,SIGSEGV …)而死亡,来自popen()的管道不会告诉我发生了什么。 编写信号处理程序并没有帮助,因为popen创建了一个接收信号而不是我的冒烟的新过程。 感谢答案我使用pipe(),fork()和execv()来创建我自己的popen() – 版本。 当程序现在段错误时,管道是无用的(读取导致奇怪的行为 – >阻止进程,直到我向父进程发送sigkill!) 为了避免这种情况,我尝试了不同的东西,我的解决方案如下(这很简单,但我需要一段时间来弄明白)。 所以这是我的示例代码: static int child_dead = 0; void sigaction_sigchld(int signal) { /* Child died */ child_dead = 1; } int main(int argc, char *argv[], char *env[]) { char *crashing_program = “/program_path/and_name”; int ret; int byte; pid = fork(); if(pid == 0) /* Child […]

strcpy 的分段错误

我想知道为什么我在下面的代码中出现分段错误。 int main(void) { char str[100]=”My name is Vutukuri”; char *str_old,*str_new; str_old=str; strcpy(str_new,str_old); puts(str_new); return 0; }

使用mmap将文件读取到字符串

我正在尝试使用mmap将文件读取到字符串。 我跟随这个例子: http : //www.lemoda.net/c/mmap-example/index.html 我的代码看起来像这样 unsigned char *f; int size; int main(int argc, char const *argv[]) { struct stat s; const char * file_name = argv[1]; int fd = open (argv[1], O_RDONLY); /* Get the size of the file. */ int status = fstat (fd, & s); size = s.st_size; f = (char *) […]

尝试在结构上使用scanf时出现分段错误

我对c很陌生,此刻我也非常沮丧。 这是我的代码: typedef struct { char* fName; char* lName; char* pNum; char* address; char* email; } contactInfo; void addContact(){ contactInfo *contact; contact = (contactInfo *) malloc (sizeof(contactInfo)); printf(“\n[Add a contact]\nFirst Name: “); scanf(“%s”, contact->fName); printf(“%s”, contact->fName); } 出于某种原因,当我输入scanf的值时,它会给我一个分段错误。 如果我尝试在contact-> fName前添加&,我也会收到错误。 代码有什么问题?

glibc检测到错误

任何人都可以帮我理解这个错误信息吗? *** glibc detected *** ./kprank_new3_norm: munmap_chunk(): invalid pointer: 0x00000000096912d0 *** ======= Backtrace: ========= /lib64/libc.so.6(cfree+0x1b6)[0x3df6e75a36] ./kprank_new3_norm[0x409277] ./kprank_new3_norm[0x4092a9] ./kprank_new3_norm[0x4092ea] ./kprank_new3_norm[0x40941f] ./kprank_new3_norm[0x40943b] ./kprank_new3_norm[0x40945f] ./kprank_new3_norm[0x409628] ./kprank_new3_norm[0x4096a7] ./kprank_new3_norm[0x40968d] ./kprank_new3_norm[0x409750] ./kprank_new3_norm[0x4097a5] ./kprank_new3_norm[0x404846] /lib64/libc.so.6(__libc_start_main+0xf4)[0x3df6e1d974] ./kprank_new3_norm(__gxx_personality_v0+0x99)[0x4017f9] ======= Memory map: ======== 00400000-00412000 r-xp 00000000 08:21 25795429 /home/rkrish/abhik/kprank/kprank_new3_norm 00611000-00612000 rw-p 00011000 08:21 25795429 /home/rkrish/abhik/kprank/kprank_new3_norm 09691000-096d3000 rw-p 09691000 00:00 0 [heap] 3df6a00000-3df6a1c000 r-xp 00000000 08:02 […]