Realloc char* 内部结构
Realloc char* inside structure
我是 C 编程新手,
我有一个带有 char 和 int 指针的结构,我曾经经常修改这个指针,found some reference in online to realloc char* 并且它工作正常,但是当我在结构内部使用时同样的事情意味着出现问题,
typedef struct MyStruct
{
int* intPtr;
char* strPtr;
} Mystruct;
里面main()
Mystruct *myStructPtr;
myStructPtr = new Mystruct();
myStructPtr->intPtr = new int();
*myStructPtr->intPtr = 10;
myStructPtr->strPtr = (char *)malloc(sizeof("original"));
myStructPtr->strPtr = "original";
printf("String = %s, Address = %u\n", myStructPtr->strPtr, myStructPtr->strPtr);
myStructPtr->strPtr = (char *)realloc(myStructPtr->strPtr, sizeof("modified original"));
myStructPtr->strPtr = "modified original";
printf("String = %s, Address = %u\n", myStructPtr->strPtr, myStructPtr->strPtr);
我在重新分配指针中的 char* 时发现以下错误
This may be due to a corruption of the heap, which indicates a bug in or any of the DLLs it has loaded.
这里的问题是,分配内存后
myStructPtr->strPtr = (char *)malloc(sizeof("original"));
你正在覆盖返回的指针
myStructPtr->strPtr = "original";
然后,您尝试在内存分配器函数未返回的指针上使用 realloc()
。这导致 undefined behavior.
引用 C11
,章节 §7.22.3.5
If ptr
is a null pointer, the realloc
function behaves like the malloc
function for the
specified size. Otherwise, if ptr
does not match a pointer earlier returned by a memory
management function, or if the space has been deallocated by a call to the free
or
realloc
function, the behavior is undefined. [....]
也就是说,你不应该使用 %u
来打印 指针 本身,你必须使用 %p
格式说明符并将相应的参数转换为 (void *)
.
解决方案你应该
malloc分配内存并将地址存储在myStructPtr->strPtr中。然后将指针重新分配给字符串常量 "original".
的位置
您不能重新分配字符串常量的位置。
与其将原始分配给指针,不如将其复制到指针指向的位置。
const char* originalStr = "original";
memcpy ( myStructPtr->strPtr, originalStr , strlen(originalStr)+1 );
我是 C 编程新手, 我有一个带有 char 和 int 指针的结构,我曾经经常修改这个指针,found some reference in online to realloc char* 并且它工作正常,但是当我在结构内部使用时同样的事情意味着出现问题,
typedef struct MyStruct
{
int* intPtr;
char* strPtr;
} Mystruct;
里面main()
Mystruct *myStructPtr;
myStructPtr = new Mystruct();
myStructPtr->intPtr = new int();
*myStructPtr->intPtr = 10;
myStructPtr->strPtr = (char *)malloc(sizeof("original"));
myStructPtr->strPtr = "original";
printf("String = %s, Address = %u\n", myStructPtr->strPtr, myStructPtr->strPtr);
myStructPtr->strPtr = (char *)realloc(myStructPtr->strPtr, sizeof("modified original"));
myStructPtr->strPtr = "modified original";
printf("String = %s, Address = %u\n", myStructPtr->strPtr, myStructPtr->strPtr);
我在重新分配指针中的 char* 时发现以下错误
This may be due to a corruption of the heap, which indicates a bug in or any of the DLLs it has loaded.
这里的问题是,分配内存后
myStructPtr->strPtr = (char *)malloc(sizeof("original"));
你正在覆盖返回的指针
myStructPtr->strPtr = "original";
然后,您尝试在内存分配器函数未返回的指针上使用 realloc()
。这导致 undefined behavior.
引用 C11
,章节 §7.22.3.5
If
ptr
is a null pointer, therealloc
function behaves like themalloc
function for the specified size. Otherwise, ifptr
does not match a pointer earlier returned by a memory management function, or if the space has been deallocated by a call to thefree
orrealloc
function, the behavior is undefined. [....]
也就是说,你不应该使用 %u
来打印 指针 本身,你必须使用 %p
格式说明符并将相应的参数转换为 (void *)
.
解决方案你应该
malloc分配内存并将地址存储在myStructPtr->strPtr中。然后将指针重新分配给字符串常量 "original".
的位置您不能重新分配字符串常量的位置。
与其将原始分配给指针,不如将其复制到指针指向的位置。
const char* originalStr = "original";
memcpy ( myStructPtr->strPtr, originalStr , strlen(originalStr)+1 );