| |

VerySource

 Forgot password?
 Register
Search

Ask about how to determine whether the pointer has been allocated memory Thank you!

[Copy link]

1

Threads

13

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-4-21 13:00:01
| Show all posts
void main ()
{
    LNode * p = NULL;
    p = (LNode *) malloc (sizeof (LNode));

    if (NULL == P)
    {
        printf ("alloc memory failed.\n");
    }

    if (...)
    {
        // Other operations
    }
    free (p);
    p = NULL;
}

That's all it takes, lz delved deeper?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-7 00:45:02
| Show all posts
There is no way to judge that after free(p), the address pointed to by p is unchanged, only that address is recovered.
Generally, the pointer is set to 0 immediately after free, and when the judgment is made, it is only necessary to determine whether p is 0.
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-7-18 05:15:01
| Show all posts
1. Strictly speaking, a system must uniformly allocate and release memory, that is to say, there are encapsulated memory operation functions.
2. Another principle is to call the function to ensure the validity of the parameters passed to the called function.

It is recommended to develop a programming specification. Standards are the guarantee of stability. It is impossible to rely on the strength of one person alone. After all, development is a team matter.
Reply

Use magic Report

0

Threads

9

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-7-19 09:15:01
| Show all posts
Personally feel that there is no way to judge this situation, and the person calling the function must ensure that the passed pointer is valid.

Similar situations also include functions such as strcpy and strcat,
For such a function, the second parameter requires a string ending with '\x00', but if you must pass an unterminated character array into it, the function itself cannot refuse it, and a calculation error is likely to occur.
The responsibility should be borne by the person who calls these functions.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-2 16:30:01
| Show all posts
Not initialized
LNode* p = NULL;
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list