locked
how to find out if pointer is empty RRS feed

  • Question

  • hi im trying to find out if pointer is empty but im sure how.

     

    STRUC1 *struct1 = new STRUCT1[10];

     

    STRUC1 *struct2 = new STRUCT1;

    STRUC1 *struct3 = new STRUCT1;

    struct1[0] = *struct2;

    struct1[1] = *struct3;

     

    for(int i = 0; i < 2; i++)

    {

    struct1[i]; 

    }

     

    it would fail at last pointer so how do u know if its empty or not ?

    thanks

     

     

    Friday, August 13, 2010 1:32 AM

Answers

  • memset (struct1, 0, 10 * sizeof (STRUCT1*));

     

    • Proposed as answer by Jesse Jiang Tuesday, August 17, 2010 8:21 AM
    • Marked as answer by Jesse Jiang Thursday, August 19, 2010 3:33 AM
    Friday, August 13, 2010 4:18 AM
  • What do you mean each pointer?  I only see one pointer here, struct1 , and it is pointing to memory of a dynamic array of ten STRUCT1 objects.

    Are you perhaps trying to create a dynamic array of 10 STRUCT1 pointers?  In which case you might try:

    STRUCT1* * struct1 = new STRUCT1*[10];

    std::fill_n(struct1, 10, nullptr);

     

    • Proposed as answer by Jesse Jiang Tuesday, August 17, 2010 8:21 AM
    • Marked as answer by Jesse Jiang Thursday, August 19, 2010 3:33 AM
    Friday, August 13, 2010 5:11 PM
  • Use std::vector<T*>, and not only will this simplify memory allocation and exception safety a bit, the contents will be default-initialized for you, which for pointers means they will start off equal to NULL. After that, you're on your own WRT maintaining valid values.

     


    Doug Harrison (Visual C++ MVP)
    • Proposed as answer by Jesse Jiang Tuesday, August 17, 2010 8:21 AM
    • Marked as answer by Jesse Jiang Thursday, August 19, 2010 3:33 AM
    Friday, August 13, 2010 7:06 PM

All replies

  • There is no such thing as an empty pointer.  Pointers that you have not set contain garbage values.  It is common practice to set pointers to NULL so you can detect or test for 'empty.'  But this only works if you set it to NULL.

     

    Friday, August 13, 2010 2:05 AM
  • thanks for reply.

    so how do i set STRUC1 *struct1 = new STRUCT1[10]; so each pointer is null without using some sort of loop to go through each pointer

    Friday, August 13, 2010 4:00 AM
  • memset (struct1, 0, 10 * sizeof (STRUCT1*));

     

    • Proposed as answer by Jesse Jiang Tuesday, August 17, 2010 8:21 AM
    • Marked as answer by Jesse Jiang Thursday, August 19, 2010 3:33 AM
    Friday, August 13, 2010 4:18 AM
  • What do you mean "it would fail at last pointer"?  The code you wrote looks like it should be OK, unless we are talking about memory allocation failures.

    Is there a difference between STRUC1 and STRUCT1 or is that a typo?

    Friday, August 13, 2010 4:52 PM
  • What do you mean each pointer?  I only see one pointer here, struct1 , and it is pointing to memory of a dynamic array of ten STRUCT1 objects.

    Are you perhaps trying to create a dynamic array of 10 STRUCT1 pointers?  In which case you might try:

    STRUCT1* * struct1 = new STRUCT1*[10];

    std::fill_n(struct1, 10, nullptr);

     

    • Proposed as answer by Jesse Jiang Tuesday, August 17, 2010 8:21 AM
    • Marked as answer by Jesse Jiang Thursday, August 19, 2010 3:33 AM
    Friday, August 13, 2010 5:11 PM
  • Use std::vector<T*>, and not only will this simplify memory allocation and exception safety a bit, the contents will be default-initialized for you, which for pointers means they will start off equal to NULL. After that, you're on your own WRT maintaining valid values.

     


    Doug Harrison (Visual C++ MVP)
    • Proposed as answer by Jesse Jiang Tuesday, August 17, 2010 8:21 AM
    • Marked as answer by Jesse Jiang Thursday, August 19, 2010 3:33 AM
    Friday, August 13, 2010 7:06 PM
  •  

    Hi ,

     

    Did you solve the issue? Please let me know what I can help. Thanks.

     

    Regards!

    Jesse


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, August 17, 2010 8:20 AM