realloc

    0熱度

    2回答

    #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]){ char buffer[103]; char **words = malloc(1 * sizeof(*words)); size_t counter = 0; size_t array_size = 2;

    2熱度

    1回答

    我發佈此示例代碼以解釋我的問題。我試圖在函數間傳遞一個指向整數的指針。如果你編譯&運行這個代碼,你會看到奇怪的數字出現,我不明白爲什麼。我認爲這是一個壞的使用realloc()。如果有人可以提供一個提示,或者可能會重定向我一些類似的問題,我會感激。我尋找這個問題,但我找不到任何類似的東西。 #include <stdio.h> #include <stdlib.h> void myFunc

    0熱度

    3回答

    #include<stdio.h> #include<string.h> #include<stdlib.h> char *func(char * str){ int len; len=strlen(str)+3; str = (char *)realloc(str,len); return str; } void main(){

    3熱度

    3回答

    我想先製作一個大小爲10的int*,然後編寫一個add()函數來添加元素。如果元素數大於10,則函數將使用realloc()來調整int*的大小。但是我收到了錯誤信息。我該如何改進? 這裏是我做過什麼: int main() { int size = 10; int* a; a = (int*)malloc(size*sizeof(int*)); int

    1熱度

    1回答

    struct model { char *cam; int *to_location, *go_location; int to_count, go_count; int length, size; }; struct model * initialize(int l) { struct model *c; c = (stru

    1熱度

    3回答

    我在C中閱讀了關於動態數組的問題,但是我無法將答案與我的問題聯繫起來。 我使用fgets從stdin獲取命令,刪除換行符,然後希望將每個由空格分隔的命令存儲在動態分配的字符串數組中。然而,我正在分配和重新分配內存的正確方式遇到很多麻煩。我與clang編制和不斷收到分段錯誤11.然後我用-fsanitize=address並不斷收到: ==2286==ERROR: AddressSanitizer:

    -2熱度

    2回答

    我給出了一個未知大小的文本文件,我必須讀到最後,計算單詞,字母和其他東西的數量。爲此,我嘗試讀取整個文件並將所有單詞保存在數組中。我被告知使用動態內存分配,因爲我事先不知道文本文件的大小。 在我進入計算單詞和字母的算法之前,我試圖做動態內存分配工作。這是我的代碼: int main(int argc, char *argv[]) { FILE *fp; // file pointer

    -1熱度

    2回答

    我正在嘗試使用realloc實現dinamically增加的數組。我創建malloc的數組,然後叫我add功能,1。此處增加數組的大小是代碼: #include <stdio.h> #include <stdlib.h> int *foo; int quantity; void add(int number) { foo = (int*) realloc(foo, sizeo

    -2熱度

    1回答

    我的程序總是在第四次重新分配中失敗,我不明白爲什麼它使得第一個3好,然後失敗。我使用三重指針,並且我試圖調試,並且所有工作都很好,直到第四次重新分配時它說它超出了範圍和分段錯誤。 #include <stdio.h> #include <stdlib.h> void rellenar(int ***movimiento, int mov, int discos) { int j,

    1熱度

    2回答

    我會擴展數組,直到程序收到「-1」作爲輸入。我每次1當前的陣列延伸,但經過7輸入我有「無效的下一個大小:0x0000000000d47010」 void extend(int *v, int i) { int *p; p= (int *) realloc(v, (i+1)*sizeof(int)); if(p!=NULL) v=p; } int ma