| |

VerySource

 Forgot password?
 Register
Search

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

[Copy link]

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-24 15:45:01
| Show all posts
p is a pointer type, so sizeof (p) = 4 (in 32-bit systems)
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-25 10:54:01
| Show all posts
I think it is better to display the initialization NULL when the variable is defined, or to ensure that malloc is called before use. In a 32-bit system, the pointers are all 4 bits. The data pointed to is unknown. In this regard, if you use an uninitialized pointer or variable, the answer is indeterminate, and this should be guaranteed by the programmer.
Reply

Use magic Report

0

Threads

12

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 Invalid IP Address

Post time: 2020-1-25 21:36:01
| Show all posts
Good programming practice is to initialize the pointer to NULL, check whether the allocation is successful after allocating space, and return NULL immediately after the space is successfully released. If it is a pointer shared by multiple threads, plus mutual exclusion protection during operation;
In this way, when you use a pointer, you only need to check whether the pointer points to NULL, and you can know whether the pointer is valid.

If you ignore the above premise, what you can do at most is to redefine your structure so that this structure has a specific value at some specific position. Based on this, I think it will not Does anyone really do this.
Reply

Use magic Report

0

Threads

6

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-13 22:45:01
| Show all posts
sizeof (p) should work
Reply

Use magic Report

0

Threads

6

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-2-14 02:19:17
| Show all posts
It's up to the compiler. If you don't use assignment directly, it's a wild pointer. Windows is assigned a value of 0xcccccccc. Other environments are difficult to say, and may be random values, which may cause a program or system crash. When it ’s better to define pointers, it is safer to assign initial values
Reply

Use magic Report

0

Threads

19

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-2-14 10:00:01
| Show all posts
I hope that the system will help you initialize the pointer, and also hope that the system will return the pointer to NULL for you after release
Unfortunately, c / c ++ does not provide such functionality, they leave this responsibility to the programmer to control.
So developing good programming habits is the way to reduce errors. Remember to set the pointer to NULL at the time of definition and release, so you can judge it later.
Reply

Use magic Report

1

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-3-1 18:00:01
| Show all posts
I can control the function code, but I can't guarantee what other programmers will do to change the pointer before calling my code, so I must strictly check the passed pointer to prevent this. It is written by other programmers. You cannot expect other programmers who call and change functions to do what you want:

LNode * p = NULL;
// perform several operations on p, including creating and using
free (p);
// Can't expect every programmer to do this step
// p = NULL;
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-4-15 12:00:01
| Show all posts
Give the function a return value to determine whether it is successful; just write a detailed comment.

Don't think too much about LZ.
Reply

Use magic Report

1

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-4-16 05:30:01
| Show all posts
I did not think too much, this situation should be taken into account, this is a manifestation of the robustness of the function.
Reply

Use magic Report

0

Threads

19

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-4-17 05:45:01
| Show all posts
Then you simply write your own function to deal with allocation and deallocation, requiring all programmers to only call your function to allocate and deallocation memory.
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