如何保证内存安全?
我想实际使用这个包装器,但问题是我不知道它是否非常安全。
我有一些关于使用 malloc()
、calloc()
和 realloc()
的简单问题。这是我到目前为止所拥有的:
string.h
typedef struct str str; // pointer for encapsulation
string.c
struct str
{
char *buf;
size_t len;
}
假设我有一个辅助函数,它可以简单地执行以下操作:
str *NEW_STRING()
{
str *temp = calloc(1, sizeof (struct str));
temp->len = 0;
temp->buf = (char *) malloc(1);
return temp;
}
这安全吗?如果是的话,如果我这样做会发生什么:
str *A_STRING = NEW_STRING();
A_STRING = NEW_STRING();
它会调用 malloc 和 calloc 两次,这很糟糕吗?初始化器会更好吗?
void str_init(str *A_STRING)
{
if (A_STRING)
{
free(A_STRING);
}
if (A_STRING->buf)
{
free(A_STRING->buf);
}
A_STRING = calloc(1, sizeof (struct str));
A_STRING->buf = (char *) malloc(1);
A_STRING->len = 0;
}
最后,这是释放内存的好方法吗?
void free_string(str *A_STRING)
{
if (A_STRING->buf)
{
free(A_STRING->buf);
}
else
{
A_STRING->buf = NULL;
}
if (A_STRING)
{
free(A_STRING);
}
else
{
A_STRING = NULL;
}
A_STRING->len = 0;
}
如果包含任何其他信息,那就太好了。我不想向公众发布任何东西,就好像它是一个好的图书馆一样,因为我这样做主要是为了学习目的。
I would like to actually use this wrapper, but the problem is i don't know if it's very safe, yet.
I have a few simple questions regarding using malloc()
, calloc()
, and realloc()
. Here's what I have so far:
string.h
typedef struct str str; // pointer for encapsulation
string.c
struct str
{
char *buf;
size_t len;
}
Say i have a helper function that simply does this:
str *NEW_STRING()
{
str *temp = calloc(1, sizeof (struct str));
temp->len = 0;
temp->buf = (char *) malloc(1);
return temp;
}
Is this safe? If it is, what would happen if i did something like this:
str *A_STRING = NEW_STRING();
A_STRING = NEW_STRING();
It would call malloc and calloc twice, is that bad? Would an initializer be better?
void str_init(str *A_STRING)
{
if (A_STRING)
{
free(A_STRING);
}
if (A_STRING->buf)
{
free(A_STRING->buf);
}
A_STRING = calloc(1, sizeof (struct str));
A_STRING->buf = (char *) malloc(1);
A_STRING->len = 0;
}
Finally, is this a good way to free memory?
void free_string(str *A_STRING)
{
if (A_STRING->buf)
{
free(A_STRING->buf);
}
else
{
A_STRING->buf = NULL;
}
if (A_STRING)
{
free(A_STRING);
}
else
{
A_STRING = NULL;
}
A_STRING->len = 0;
}
Any additional information would be great if included. I don't want to release anything to the public as if it were a good library, because i am primarily doing this for learning purposes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很多错误:
不。
下一步:
你泄漏了内存。
第二次调用基本上生成一个新对象,旧对象丢失并泄漏。
str_init 中的问题
这是第一次调用他的方法吗?
如果是这样,那么 A_STRING 包含一个随机值(您即将释放该值)。
这会破坏代码。
A_STRING 已释放(您现在无法再访问它)。
任何这样做的代码都是不好的。
不检查 calloc 的结果。
Lots of errors:
No.
Next:
You leak memory.
The second call basically generates a new object the old object is lost and leaks.
Problems in str_init
Is this the first time that his method is called?
If so then A_STRING contains a random value (that you are about to FREE).
This will blow the code up.
A_STRING is freed (you can now no longer accesses it).
Any code that does so is bad.
No checking the result of calloc.