Tag: segmentation fault

打印后出现奇怪的分段错误

写了一个简单的交换程序,效果很好; 但是在打印完所有内容后会出现分段错误 。 #include void swap(int* p1,int* p2){ int* temp; *temp = *p1; *p1 = *p2; *p2 = *temp; } int main(){ int a,b; a = 9; b = 8; printf(“%d %d \n”,a,b); swap(&a,&b); printf(“%d %d \n”,a,b); return 0; } 输出: 9 8 8 9 Segmentation fault 我应该简单地忽略这一点并继续前进或者是否有一些非常奇怪的事情发生?

Malloc语句给出分段错误11

对于一个项目我正在学习在c中使用malloc / realloc,但我无法弄清楚为什么这段代码给我一个seg错误! #include #include #include #include typedef struct word_data word_data_t; typedef struct data data_t; typedef struct index_data index_data_t; struct word_data { int docunumber; int freq; }; struct data { char *word; int total_docs; word_data_t *data; }; struct index_data { data_t *index_data_array; }; /* Inside a function called from main */ index_data_t *index_data=NULL; index_data->index_data_array = […]

将c字符串解析为指针数组时出现分段错误

函数makearg应该计算char数组中的单词数,并将每个单词分解为指针数组中自己的位置。 分段错误似乎是strncpy函数的问题。 int makearg(char s[], char ***args); int main(){ char **args = (char**)(malloc(100)); char *str = “ls is a -l file”; int argc; argc = makearg(str, &args); printf(“%d”, argc); printf(“%c”, ‘\0’); int i; for(i = 0; i < argc; i++){ puts(args); printf("%c", '\n'); } return 0; } ///////////////////////////////////////// int makearg(char s[], char ***args){ int argc […]

SDL保存窗口为BMP

我正在用SDL和C编写程序,我希望能够将窗口保存为图像。 这是我的代码: screen = SDL_GetWindowSurface(win); SDL_SaveBMP(screen,”screen”); 但是当我执行它时,我得到: Segmentation Fault 从其他来源我收集它的指针和内存访问。 有帮助吗?

使用其他子字符串替换字符串的子字符串时出现分段错误

我有字符串“{”1“:”[4,11,14,19,20,18,27]“}”。 我想把它改成“{\”1 \“:\”4,11,14,19,20,18,27 \“}”。 以下是我的代码: #include #include #include char *replace (char *this, char *withthat, char *inthis) { char *where = inthis; while ((where = strstr(where, this))) { memcpy(where, withthat, strlen(withthat)); memmove(where+strlen(withthat),where+strlen(this), strlen(where+strlen(this))+1); } return inthis; } int main(void) { char string[] = “{"1":"[4,11,14,19,20,18,27]"}”; printf(“%s\n”, replace(“"”, “\\\””, string)); printf(“%s\n”, replace(“\”[” , “\””, string)); printf(“%s\n”, replace(“]\\” , […]

C中的双重自由或损坏(快速顶部)错误/分段错误

我正在尝试动态分配一个数组来从命令行读取用户输入。 它可以工作99/100次,但是如果我反复键入一堆字符,我有时会遇到分段错误错误或双重自由或损坏(快速顶部)错误。 此错误相对难以重现。 我很确定错误的发生是因为我重新分配数组的方式。 while(1){ char *buf_in; // Holds user keyboard input int cnt = 0, length = 0; // cnt stores current read buffer size, length allows input into buf_in char ch; int buf_max = 64; // Current buffer size. Dynamically allocated buf_in = malloc(buf_max * sizeof(char)); if (buf_in==NULL){ fprintf(stderr,”Error allocating memory!\n”); exit(EXIT_FAILURE); } […]

c – 尝试编写修改后的结构时出现分段错误

这是一个旨在使用ppm图像文件的程序。 我在尝试修改结构然后将其写入新文件时遇到编译错误。 这是全局结构(在ppmIO.c和ppmIO.h中声明): ppmIO.c: struct Image *instance; ppmIO.h: struct Image { int width; int height; unsigned char *data; }; extern struct Image *instance; 这是我的imageManip.h文件: #include void ImageInvert(struct Image **toInvert); void ImageSwap(struct Image **toSwap); 这些是我的imageManip.c文件的相关部分: #include #include #include #include #include void ImageInvert(struct Image **toInvert) { int i; int pix = (*toInvert->width) * (*toInvert->height); *toInvert = realloc(*toInvert, […]

C中的二维数组指针访问分段故障

我正在尝试用C语言编写Conway的生命游戏代码。我有一些包含我的宇宙的2D数组的问题。 我将向您展示一些导致我出现问题的代码。 实际上有一个我无法处理的问题。 #include #include #include “file2ppm.h” typedef enum { false, true } bool; void *allocateMemoryFor2DArray(int x, int y); //allocate memory for 2D array void initializeUniverseFromFile(char* path, int x, int y, int array[x][y]); //set values of cells from a file void initializeUniverseRandomly(int x, int y, int array[x][y]); //set random values int getNumberOfColumns (FILE *file); //get […]

C并发UDP套接字,奇怪的分段错误

我要么非常疲倦而且没有注意到一些简单的事情,或者这完全是在与我联系。 我遇到了一个分段错误(核心转储),我已经设法将它精确定位到worker函数中的sendto()。 (在服务器中) 服务器代码: //UDPServer.c /* * gcc -o server UDPServer.c * ./server */ #include #include #include #include #include #include #include #include void err(char *str) { perror(str); exit(1); } int sock; typedef struct { struct sockaddr_in client; int buffsize; char *msg; } data; void *worker (void* asd) { int len; FILE *fp; data d; d […]

如何纠正我的C程序中的分段错误

我正在调试为knapSack编写的以下程序 #include #include #include “timer.h” #define MAX(x,y) ((x)>(y) ? (x) : (y)) #define table(i,j) table[(i)*(C+1)+(j)] int main(int argc, char **argv) { FILE *fp; long N, C, opt; // # of objects, capacity int *weights, *profits, *table, *solution; // weights and profits int verbose; // Temp variables long i, j, count, size, size1, ii, jj; // […]