簡體   English   中英

程序收到信號:“EXC_BAD_ACCESS”?

[英]Program received signal: “EXC_BAD_ACCESS”?

我正在使用XCode在C中創建命令行程序。 在運行程序時,它最初會執行它應該執行的操作(詢問我的文件路徑)。 但是,當我鍵入有效的和現有的文件路徑時,它會給我以下錯誤:

Program received signal:  “EXC_BAD_ACCESS”. sharedlibrary apply-load-rules all (gdb)

我的程序中有兩個警告,這兩個警告都與strcat函數有關。 警告是:

warning: implicit declaration of function 'strcat'

warning: incompatible implicit declaration of built-in function 'strcat'

我想知道為什么我的程序沒有正常執行。

謝謝,邁克

我的代碼發布在下面:

#include "stdlib.h"
int main (void)
{
   char *string1;
   printf("Type in your file path: ");
   scanf("%s", string1);
   char *string2 = "tar czvf YourNewFile.tar.gz ";
   strcat(string2, string1);
   system(string2);
}

也許它與分配字符有關?

你忘了為string1分配空間, scanf不會為你分配內存,你必須自己做。 此外, string2指向不可寫的內存,並且它沒有足夠的空間來向它追加string1 ,所以即使你有char string2[] = "tar czvf YourNewFile.tar.gz ";你的strcat也會溢出char string2[] = "tar czvf YourNewFile.tar.gz ";

這是一個注釋版本的東西,更接近你真正想要的東西:

#include <stdio.h>  /* printf, sprintf, fgets */
#include <string.h> /* strcat, strlen */
#include <stdlib.h> /* malloc */

#define TAR "tar czvf YourNewFile.tar.gz"

int main(void) {
    char path[100] = { 0 };  /* Initialize to all 0 bytes.           */
    char *cmd;               /* We'll allocate space for this later. */
    int   len;

    printf("Type in your file path: ");
    fgets(path, sizeof(path), stdin);   /* Read at most 100 characters into path */

    /*
     * Remove the trailing newline (if present).
     */
    len = strlen(path);
    if(path[len - 1] == '\n')
        path[len - 1] = '\0';

    /*
     * Allocate room for our command. 
     */
    cmd = malloc(
          strlen(TAR)   /* Room for the base tar command.         */
        + 1             /* One more for the space.                */
        + strlen(path)  /* Room for the path we read.             */
        + 1             /* One more for the final nul terminator. */
    );

    /*
     * You could also use a bunch of strcpy and strcat stuff for
     * this but sprintf is less noisy and safe as long as you've
     * properly allocated your memory.
     */
    sprintf(cmd, "%s %s", TAR, path);

    /*
     * This is vulnerable to unpleasant things in `path` (such as spaces,
     * &, >, <, ...) but this will do as a learning exercise. In real life
     * you'd probably want to use fork and exec for this to avoid the  
     * interface issues with the shell.
     */
    system(cmd);  

    /*
     * Free the memory we allocated.
     */
    free(cmd);

    /*
     * You need a return value because of "int main(...)". Zero is
     * the standard "all's well" return value.
     */
    return 0;
}

如果我犯了任何一個錯誤的錯誤,請有人告訴我。

您可以在此處找到上述功能的參考資料。

檢查這一行:

char *string1

這一行:

scanf("%s", string1);

你沒有為string1聲明一個大小,這意味着你總會得到一個錯誤,修復它是這樣的:

char string1[100]

如果100是輸入的最大長度。

或者按字符閱讀輸入字符。

並且,要消除警告,請將#include "string.h"添加到#include語句所在的位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM