Cách sử dụng realloc trong C [trùng lặp]
Tôi đang cố gắng phân bổ lại bộ nhớ bằng cách sử dụng hàm realloc, tôi thấy rằng bạn cần sử dụng malloc trước đó nhưng tôi không hiểu liệu bạn có PHẢI sử dụng nó không vì giả sử tôi đang tạo chuỗi sau:
char string[] = "fun";
liệu hàm realloc có hoạt động không nếu tôi cố gắng thêm nhiều không gian hơn?
điều đó đưa tôi đến câu hỏi của mình, tôi đang cố gắng chỉ cần thêm một chữ cái vào cuối chuỗi, giả sử 'p', nhưng vì lý do nào đó mà chương trình đè lên dòng phân bổ lại mỗi khi tôi chạy nó.
Đây là mã đầy đủ của tôi:
int main()
{
char string[] = "fun" ;
str_func(string);
printf("%s", string);
return 0;
}
void str_func(char* str)
{
str = (char*)realloc(str, strlen(str) + 2);
strcat(str, "p");
}
Tôi cũng đã thử tạo một con trỏ tới 'chuỗi' và gửi con trỏ, kết quả tương tự.
Trả lời
liệu hàm realloc có hoạt động không nếu tôi cố gắng thêm nhiều không gian hơn?
Không, bởi vì mảng đó không được phân bổ trên heap - trong trường hợp của bạn, nó rất có thể được phân bổ trên ngăn xếp và không thể thay đổi kích thước. Nói một cách đơn giản: reallockhông nhận ra con trỏ và không biết phải làm gì với nó, nhưng vẫn cố gắng làm điều gì đó, do đó xảy ra sự cố.
You can only call realloc on a pointer that was previously passed to malloc, or on a null pointer. That's just how these functions work.
For details, see What gets allocated on the stack and the heap?.
I saw that you need to use malloc before but I don't understand if you MUST use it
If you need to use malloc before you can realloc something, then by definition you must only realloc things originally allocated with malloc.
You're trying to find some space between "need" and "must" that doesn't exist.
... for some reason the program crushes on the realloc
You already said you know you need to use malloc. Then you didn't use malloc, and you're asking why this is a problem. You could at least try doing the thing you "know" you need to do, to see if that solves the problem.
The program should probably look like
int main()
{
/* array is an automatic local variable. It wasn't dynamically allocated
in the first place, so can't be dynamically re-allocated either.
You cannot (and don't need to) free it either, it just goes out of scope
like any other automatic variable.
*/
char array[] = "fun";
/* you need to use malloc (or one of the other dynamic allocation functions)
before you can realloc, as you said yourself */
char *dynamic = malloc(1+strlen(array));
memcpy(dynamic, array, 1+strlen(array));
/* realloc can move your data, so you must use the returned address */
dynamic = str_func(dynamic);
printf("old:'%s', new:'%s'\n", array, dynamic);
/* not really essential since the program is about to exit anyway */
free(dynamic);
}
char* str_func(char* str)
{
char* newstr = realloc(str, strlen(str) + 2);
if (newstr) {
strcat(newstr, "p");
return newstr;
} else {
/* we failed to make str larger, but it is still there and should be freed */
return str;
}
}
Your original condition isn't quite correct: actually the pointer passed to realloc
... must be previously allocated by
malloc(),calloc()orrealloc()and not yet freed with a call to free or realloc
[OR] If ptr is NULL, the behavior is the same as calling
malloc(new_size).
The realloc function only works with things that were originally created with a small group of allocation functions (such as malloc, calloc, or realloc itself), or the null pointer. Since string is none of those things, your code is not well-defined.