| |

VerySource

 Forgot password?
 Register
Search
View: 3968|Reply: 24

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

[Copy link]

1

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-1-15 16:00:02
| Show all posts |Read mode
E.g:

struct LNode
{
    int data;
    LNode * next;
}
void main ()
{
    LNode * p;
    // So far, p has not been allocated memory,
    // I want to judge here, how can I know that p does not point to the object instance
    if (...)
    {
        // other operations
    }
}

Note: I have tried both p! = '\0' and p! = NULL, to no avail.
Reply

Use magic Report

0

Threads

41

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-1-18 21:00:02
| Show all posts
LNode * p = 0; give 0 when declared
Reply

Use magic Report

1

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-1-18 23:45:01
| Show all posts
If I release it with free (), it seems to be useless:

void main ()
{
LNode * p = 0;
p = (LNode *) malloc (sizeof (LNode));
free (p);
if (p == NULL)
{
printf ("P is NULL\n");
}
}
Reply

Use magic Report

0

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-19 08:45:01
| Show all posts
free (p);
p = NULL;
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 Argentina

Post time: 2020-1-19 21:27:01
| Show all posts
void main ()
{
    LNode * p;
    p = (LNode *) malloc (sizeof (LNode));

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

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

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-19 22:18:02
| Show all posts
Of course, pointers are best initialized when they are declared:
void main ()
{
    LNode * p = NULL;

Because if you do not initialize, some compilers will automatically initialize it, such as VC will initialize the pointer to 0xcccccccc
Reply

Use magic Report

1

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-1-20 17:54:01
| Show all posts
I don't mean this. I don't want to determine whether the malloc function is allocated successfully. I want to determine whether a pointer is allocated to an object instance. This requires considering the following situations, such as a function:

int AppendElem (LNode * header)
{
    if (...) // determine here whether the header is a null pointer
    {
        // if it is a null pointer
    }
}

Another example:

int Function (LNode * p)
{
    if (...) // determine if an object instance exists
    {
        // if an object instance exists
    }
}

Before the above two function calls, the p pointer may or may not have been malloc
Reply

Use magic Report

0

Threads

41

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 Invalid IP Address

Post time: 2020-1-21 11:00:02
| Show all posts
LNode * p = 0; give 0 when declared
p = (LNode *) malloc (sizeof (LNode));
if (0 == p) {
Allocation failed
}
Generally, assigning a value of 0 when the pointer is not used indicates an invalid pointer.
Reply

Use magic Report

1

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-1-21 17:54:02
| Show all posts
Tohzxszmh:

    Still don't understand what I mean, if so:

    LNode * p;
    AppendElem (p);

    p is neither assigned 0 nor malloc, and I now want a function entry judgment that can consider exceptions.
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-24 05:18:02
| Show all posts
Is sizeof (p) OK?
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