[英]Multiple error messages while working with pointer and malloc in c
我對C還是很陌生,並且在我的代碼中收到了多個錯誤消息,如果有人可以向我解釋這些錯誤消息,那就太好了。
我的代碼:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char newINPUT;
char* INPUT = (char*)malloc(1 * sizeof(char));
while((INPUT = getchar()) =! EOF)
char* newINPUT = (char*)realloc(INPUT, 2* sizeof(INPUT));
if(newINPUT =! NULL)
{
INPUT = newINPUT;
}
else
{
free(INPUT);
printf("Out of memory!");
}
return 0;
}
這應該做的是:它應該分配一些內存並讀取您鍵入的內容。 如果沒有分配的內存,則應重新分配兩倍於以前分配的內存。
錯誤消息:警告行13(帶有while的行):賦值使指針從整數開始而不進行強制轉換(默認啟用)錯誤行13:要求左值作為賦值的左操作數錯誤行14:'char'警告行之前的期望表達式17:與第13行。
如果有人可以向我解釋這一點會很好。 謝謝!
編輯:感謝到目前為止的所有答案! 他們確實幫助我了解了malloc和realloc。 我昨天嘗試了另一個自己的版本,但是由於不能用足夠大的輸入來測試它,所以我不確定分配的內存是否正常工作。 在重新分配和加倍的條件下(在語法上)可行嗎?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int *READ; //input
READ = malloc(sizeof(int));
while ((*READ = getchar()) != EOF)
{
if (READ) //if read not null so memory is enough
{
putchar(*READ);
}
else
{
int x;
int* extendedREAD = realloc(READ, (x*2));//expression for "double as much as before"????
if (extendedREAD = NULL) // looking if there s enough memory
{
printf("Error, not enough memory!");
}
x = x * 2;
}
}
return 0;
}
/*
Zeichen einlesen - checken ob eof, wenn nein, dann speicher ausreichend? nein dann vergräßern, sonst zeichen dazuschreiben, dann wieder vonvorne)
*/
謝謝!
這里有很多問題...
int main()
{
char newINPUT;
char* INPUT = (char*)malloc(1 * sizeof(char));
不要將調用的結果malloc()
為malloc()
; 這是不必要的,並且可能導致意外的問題。
while((INPUT = getchar()) =! EOF)
這里有兩個問題。 首先, a =! b
a =! b
表示“將a
設置為b
的邏輯補” 。 您可能想使用a != b
形式a != b
東西( “ a
不等於b
” )。 其次, getchar()
返回一個int
值。 如果嘗試將返回值存儲在char
容器中,則可能會發現它永遠不會等於EOF
。
char* newINPUT = (char*)realloc(INPUT, 2* sizeof(INPUT));
在這里,您將newINPUT
重新聲明為char*
指針。 您之前char newINPUT
其聲明為char newINPUT
。 這將導致編譯錯誤。
if(newINPUT =! NULL)
您在此處將newINPUT
設置為NULL
的邏輯補碼。 我想你是說if (newINPUT != NULL)
。 您可以簡單地使用if (newINPUT)
代替。
(關於內存泄漏的胡言亂語已刪除-我的錯!)
總體而言,您的程序邏輯存在缺陷,因為您試圖為輸入的每個單個字符將分配的內存加倍。 因此,即使您解決了上述問題,在輸入大約32個字符后,內存也將耗盡。
下面的代碼存儲在輸入緩沖器的當前大小size
,並且在所述輸入字符串的長度nchars
。 兩者都初始化為零。 緩沖區位置本身( char *input
)可以初始化為NULL
,因為使用空指針調用realloc()
等同於調用malloc()
。 這意味着我們可以從代碼中消除一個系統調用。
#include <stdio.h>
#include <stdlib.h>
int main() {
char *input=NULL, *new_input;
int c, size=0, nchars=0;
while (1) {
if (size==nchars) {
size = size*2 + (size==0); /* (size==0) is 1 if size==0, zero othewise */
if (!(new_input = realloc(input,size))) {
puts("Out of memory");
free(input);
return 1;
}
input = new_input;
printf("Buffer reallocated; new size is %d byte(s)\n",size);
}
if ((c=getchar())==EOF) {
input[nchars] = '\0'; /* Terminate the input string */
break;
}
input[nchars++] = c;
}
printf("Buffer size: %d\n"
"String length: %d\n"
"Input was: %s\n",size,nchars,input);
free(input);
return 0;
}
修改示例
#include <stdio.h>
#include <stdlib.h>
int main(void){
int ch;
char* INPUT = (char*)malloc(1 * sizeof(char));
int input_buf_size = 1;
int input_count = 0;
while((ch = getchar()) != EOF){
INPUT[input_count] = ch;
input_count += 1;
if(input_count == input_buf_size){
char *newINPUT = (char*)realloc(INPUT, input_buf_size *= 2);
if(newINPUT != NULL){
INPUT = newINPUT;
} else {
free(INPUT);
printf("Out of memory!");
exit(EXIT_FAILURE);
}
}
}
INPUT[input_count] = '\0';
puts(INPUT);
free(INPUT);
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.