在 C/C 中使用 realloc 时出错++
char *t = malloc(2);
t = "as";
t = realloc(t,sizeof(char)*6);
我收到错误“无效指针:0x080488d4 *”。
我在使用内存分配函数时遇到奇怪的错误。有没有好的教程/指南可以解释我的内存分配函数。 我正在使用linux..
请帮忙..
char *t = malloc(2);
t = "as";
t = realloc(t,sizeof(char)*6);
I am getting error "invalid pointer: 0x080488d4 *"..
I am getting strange errors in using memory allocation functions. Is there any good tuts/guides which could explain me memory allocation functions.
I am using linux..
Please help..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是您的问题:
您可能认为这会将两个字符的字符串
"as"
复制到您刚刚分配的缓冲区中。它实际上所做的是丢弃(泄漏)缓冲区,并将指针更改为指向字符串常量” ”
,它存储在机器代码旁边的只读内存中,而不是存储在malloc
堆上。因为它不在堆上,所以realloc
会查看指针并说“不行,那不是我的”。 (计算机给你这个错误是对你很好;当你给realloc
一个malloc
或realloc
没有返回的指针时,如果计算机愿意的话,它可以让恶魔从你的鼻子里飞出来。)这就是你想做的事情的方法:
请注意,由于隐式的 NUL 终止符,你需要三个字符的空间,而不是两个字符。
顺便说一句,您永远不需要将任何内容乘以
sizeof(char)
;根据定义,它是 1。This is your problem:
You probably thought this would copy the two-character string
"as"
into the buffer you just allocated. What it actually does is throw away (leak) the buffer, and change the pointer to instead point to the string constant"as"
, which is stored in read-only memory next to the machine code, not on themalloc
heap. Because it's not on the heap,realloc
looks at the pointer and says "no can do, that's not one of mine". (The computer is being nice to you by giving you this error; when you giverealloc
a pointer that wasn't returned bymalloc
orrealloc
, the computer is allowed to make demons fly out of your nose if it wants.)This is how to do what you meant to do:
Note that you need space for three characters, not two, because of the implicit NUL terminator.
By the way, you never need to multiply anything by
sizeof(char)
; it is 1 by definition.这不是在 C 中分配字符串的方式。
正确的语法是:
分配 2 个字节对于
"as"
来说是不够的。C 字符串有一个 1 字节的空终止符,因此您至少需要 3 个字节来保存
"as\0"
。(
\0
代表空终止符)您编写的代码:
t = "as";
使指针t
“放弃”以前的指针分配的内存,而是指向静态字符串“as”
。使用 malloc 分配的内存被“泄漏”并且无法恢复(直到程序终止且操作系统回收它)。之后,您可以像原来一样调用
realloc
。但是,您不应该不执行
t = realloc(t,6);
。如果 realloc 由于任何原因失败,你就失去了记忆。首选方法是:
That is not how you assign strings in C.
The correct syntax is:
Allocating 2-bytes is NOT enough for
"as"
.C-strings have a 1-byte null-terminator, so you need at least 3 bytes to hold
"as\0"
.(
\0
represents the null-terminator)The code you wrote:
t = "as";
makes the pointert
"abandon" the formerly allocated memory, and instead point to the static string"as"
. The memory allocated with malloc is "leaked" and cannot be recovered (until the program terminates and the OS reclaims it).After this, you can call
realloc
as you originally did.However, you should not do
t = realloc(t,6);
. If realloc fails for any reason, you've lost your memory.The preferred method is:
您的代码重新分配
t
,使其指向其他地方而是使用
strcpy
Your code reassigns
t
, making it point elsewhereInstead use
strcpy
首先,不要这样做:
改为这样做:
这可能看起来不值得付出努力,但否则稍后当您处理大于 1 字节的类型时可能会遇到问题。
在这一行中:
您正在分配字符串文字
"as"
的地址,因此您的指针不再指向您分配的内存。您需要将文字的内容复制到分配的内存中:您也可以只使用 strdup,这更安全:
并且不要忘记释放内存
First off, don't do that:
Do this instead:
It may not seem worth the effort, but otherwise you may run into problems later when you deal with types larger than 1 byte.
In this line:
You're assigning the address of the string literal
"as"
, so your pointer no longer points to the memory you allocated. You need to copy the contents of the literal to your allocated memory:You can also just use strdup, which is safer:
And don't forget to free the memory
这意味着您已经创建了一个指向可以容纳 2 个字节的内存位置的指针,
当您现在
使 t 指向它最初指向的其他位置时。现在它不再指向堆,
现在您正在使用指向只读内存的指针并尝试重新分配它。
当您使用 malloc 时,您在堆上分配空间。在这种情况下,t是指向该位置的指针,即块所在位置的地址。
为了在该位置放置一些内容,您需要通过取消引用 t 来复制数据,这是通过在 t 前面写入 * 来完成的:
但是在 C 中,字符串始终以 0(ASCII 值)结尾,写为 '\ 0' 因此,为了使其成为字符串,您需要附加 \0
才能执行此操作,您需要 malloc 3 个字节,而不是通过编写
*(t+2)=' 添加 \0 \0';
现在t 可以被视为指向字符串并在以字符串作为参数的函数中使用,例如
strlen( t )
返回 2this means you have created a pointer to a memory location that can hold 2 bytes
when you do
now you made t point to somewhere else than what it originally was pointing to. now it no longer points to the heap
now you are taking the pointer pointing to read only memory and try to realloc it.
when you use malloc you allocate space on the heap. t in this case is a pointer to that location, an address of where the block is.
in order to put something in that spot you need to copy the data there by dereferencing t, this is done by writing * in front of t:
however in C, a string is always terminated with a 0 (ASCII value) written as '\0' so in order to make it a string you need to append a \0
in order to do this you need to malloc 3 bytes instead, than you can add the \0 by writing
*(t+2)='\0';
now t can be treated as pointing to a string and used in functions that takes strings as arguments e.g.
strlen( t )
returns 2