如何保证内存安全?

发布于 2024-12-22 23:54:07 字数 1513 浏览 3 评论 0原文

我想实际使用这个包装器,但问题是我不知道它是否非常安全。

我有一些关于使用 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 技术交流群。

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

发布评论

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

评论(1

谁的年少不轻狂 2024-12-29 23:54:07

很多错误:

这样安全吗?如果是的话,如果我这样做会发生什么:

不。

str *NEW_STRING()
{
    str *temp = calloc(1, sizeof (struct str));

    // If calloc fails and returns NULL all the code below is invalid and blows the code up.

下一步:

它会调用 malloc 和 calloc 两次,这很糟糕吗?初始化器会更好吗?

你泄漏了内存。
第二次调用基本上生成一个新对象,旧对象丢失并泄漏。

str_init 中的问题

void str_init(str *A_STRING)
{

这是第一次调用他的方法吗?
如果是这样,那么 A_STRING 包含一个随机值(您即将释放该值)。
这会破坏代码。

    if (A_STRING)
    {
        free(A_STRING);
    }

A_STRING 已释放(您现在无法再访问它)。
任何这样做的代码都是不好的。

    if (A_STRING->buf)   // Bang blow up code.
    {
        free(A_STRING->buf);
    }

    A_STRING = calloc(1, sizeof (struct str));

不检查 calloc 的结果。

    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;  // Its already NULL pointless work
    }

    if (A_STRING)
    {
        free(A_STRING);
    }
    else
    {
        A_STRING = NULL;  // ITs already NULL pointless work
    }

    // BANG you just blew up the space shuttle.
    A_STRING->len = 0;
}

Lots of errors:

is this safe? if it is, what would happen if i did something like this:

No.

str *NEW_STRING()
{
    str *temp = calloc(1, sizeof (struct str));

    // If calloc fails and returns NULL all the code below is invalid and blows the code up.

Next:

it would call malloc and calloc twice, is that bad? would an initializer be better?

You leak memory.
The second call basically generates a new object the old object is lost and leaks.

Problems in str_init

void str_init(str *A_STRING)
{

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.

    if (A_STRING)
    {
        free(A_STRING);
    }

A_STRING is freed (you can now no longer accesses it).
Any code that does so is bad.

    if (A_STRING->buf)   // Bang blow up code.
    {
        free(A_STRING->buf);
    }

    A_STRING = calloc(1, sizeof (struct str));

No checking the result of calloc.

    A_STRING->buf = (char *) malloc(1);
    A_STRING->len = 0;
}

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;  // Its already NULL pointless work
    }

    if (A_STRING)
    {
        free(A_STRING);
    }
    else
    {
        A_STRING = NULL;  // ITs already NULL pointless work
    }

    // BANG you just blew up the space shuttle.
    A_STRING->len = 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文