在 C/C 中使用 realloc 时出错++

发布于 2024-12-10 03:13:46 字数 208 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

注定孤独终老 2024-12-17 03:13:46

这是您的问题:

char *t = malloc(2);
t = "as";

您可能认为这会将两个字符的字符串 "as" 复制到您刚刚分配的缓冲区中。它实际上所做的是丢弃(泄漏)缓冲区,并将指针更改为指向字符串常量 ” ”,它存储在机器代码旁边的只读内存中,而不是存储在 malloc 堆上。因为它不在堆上,所以 realloc 会查看指针并说“不行,那不是我的”。 (计算机给你这个错误是对你很好;当你给 realloc 一个 mallocrealloc 没有返回的指针时,如果计算机愿意的话,它可以让恶魔从你的鼻子里飞出来。)

这就是你想做的事情的方法:

char *t = malloc(3);
strcpy(t, "as");

请注意,由于隐式的 NUL 终止符,你需要三个字符的空间,而不是两个字符。

顺便说一句,您永远不需要将任何内容乘以 sizeof(char);根据定义,它是 1。

This is your problem:

char *t = malloc(2);
t = "as";

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 the malloc 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 give realloc a pointer that wasn't returned by malloc or realloc, 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:

char *t = malloc(3);
strcpy(t, "as");

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.

朮生 2024-12-17 03:13:46

这不是在 C 中分配字符串的方式。

正确的语法是:

char* t = malloc(3);  // Reserve enough space for the null-terminator \0

strncpy(t, "as", 3);
// Copy up to 3 bytes from static string "as" to char* t.
// By specifying a maximum of 3 bytes, prevent buffer-overruns

分配 2 个字节对于 "as" 来说是不够的。
C 字符串有一个 1 字节的空终止符,因此您至少需要 3 个字节来保存 "as\0"
\0 代表空终止符)

您编写的代码:t = "as"; 使指针t“放弃”以前的指针分配的内存,而是指向静态字符串“as”。使用 malloc 分配的内存被“泄漏”并且无法恢复(直到程序终止且操作系统回收它)。

之后,您可以像原来一样调用realloc
但是,您不应该执行t = realloc(t,6);。如果 realloc 由于任何原因失败,你就失去了记忆。

首选方法是:

new_t = realloc(t, 6);
if (new_t != NULL)  // realloc succeeded
{   t = new_t;  
}
else
{  // Error in reallocating, but at least t still points to good memory!
}

That is not how you assign strings in C.

The correct syntax is:

char* t = malloc(3);  // Reserve enough space for the null-terminator \0

strncpy(t, "as", 3);
// Copy up to 3 bytes from static string "as" to char* t.
// By specifying a maximum of 3 bytes, prevent buffer-overruns

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 pointer t "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:

new_t = realloc(t, 6);
if (new_t != NULL)  // realloc succeeded
{   t = new_t;  
}
else
{  // Error in reallocating, but at least t still points to good memory!
}
嘿看小鸭子会跑 2024-12-17 03:13:46

您的代码重新分配t,使其指向其他地方

char *t = malloc(2); //t=0xf00ba12
t = "as"; //t=0xbeefbeef
t = realloc(t,sizeof(char)*6); //confused because t is 0xbeefbeef, not 0xf00b412.

而是使用strcpy

char *t = malloc(3); //don't forget about the '\0'
strcpy(t, "as");
t = realloc(t, 6); //now the string has room to breathe

Your code reassigns t, making it point elsewhere

char *t = malloc(2); //t=0xf00ba12
t = "as"; //t=0xbeefbeef
t = realloc(t,sizeof(char)*6); //confused because t is 0xbeefbeef, not 0xf00b412.

Instead use strcpy

char *t = malloc(3); //don't forget about the '\0'
strcpy(t, "as");
t = realloc(t, 6); //now the string has room to breathe
明月松间行 2024-12-17 03:13:46

首先,不要这样做:

char *t = malloc(2);

改为这样做:

char *t = malloc(2 * sizeof(char));
/* or this: */
char *t = calloc(2, sizeof(char));

这可能看起来不值得付出努力,但否则稍后当您处理大于 1 字节的类型时可能会遇到问题。

在这一行中:

t = "as";

您正在分配字符串文字 "as" 的地址,因此您的指针不再指向您分配的内存。您需要将文字的内容复制到分配的内存中:

char *t = calloc(3, sizeof(char));
/* "ar" is 3 char's: 'a', 'r' and the terminating 0 byte. */
strncpy(t, "ar", 3);
/* then later: */
t = realloc(t,sizeof(char)*6);

您也可以只使用 strdup,这更安全:

#include <string.h>

char *t = strdup("ar");
t = realloc(t,sizeof(char)*6);

并且不要忘记释放内存

free(t);

First off, don't do that:

char *t = malloc(2);

Do this instead:

char *t = malloc(2 * sizeof(char));
/* or this: */
char *t = calloc(2, sizeof(char));

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:

t = "as";

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:

char *t = calloc(3, sizeof(char));
/* "ar" is 3 char's: 'a', 'r' and the terminating 0 byte. */
strncpy(t, "ar", 3);
/* then later: */
t = realloc(t,sizeof(char)*6);

You can also just use strdup, which is safer:

#include <string.h>

char *t = strdup("ar");
t = realloc(t,sizeof(char)*6);

And don't forget to free the memory

free(t);
泪痕残 2024-12-17 03:13:46
char *t = malloc(2);

这意味着您已经创建了一个指向可以容纳 2 个字节的内存位置的指针,

     +-+-+ 
t -> | | |
     +-+-+ 

当您现在

t = "as";

使 t 指向它最初指向的其他位置时。现在它不再指向堆,

t = realloc(t,sizeof(char)*6);

现在您正在使用指向只读内存的指针并尝试重新分配它。

当您使用 malloc 时,您在堆上分配空间。在这种情况下,t是指向该位置的指针,即块所在位置的地址。

为了在该位置放置一些内容,您需要通过取消引用 t 来复制数据,这是通过在 t 前面写入 * 来完成的:

*t = 'a';   // now 'a' is where t points
*(t+1)='s'; // now 's' is behind a, t still pointing to 'a'

但是在 C 中,字符串始终以 0(ASCII 值)结尾,写为 '\ 0' 因此,为了使其成为字符串,您需要附加 \0

     +-+-+--+ 
t -> |a|s|\0|
     +-+-+--+ 

才能执行此操作,您需要 malloc 3 个字节,而不是通过编写 *(t+2)=' 添加 \0 \0';

现在t 可以被视为指向字符串并在以字符串作为参数的函数中使用,例如 strlen( t ) 返回 2

char *t = malloc(2);

this means you have created a pointer to a memory location that can hold 2 bytes

     +-+-+ 
t -> | | |
     +-+-+ 

when you do

t = "as";

now you made t point to somewhere else than what it originally was pointing to. now it no longer points to the heap

t = realloc(t,sizeof(char)*6);

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:

*t = 'a';   // now 'a' is where t points
*(t+1)='s'; // now 's' is behind a, t still pointing to 'a'

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

     +-+-+--+ 
t -> |a|s|\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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文