Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. free pointer to pointer

free pointer to pointer

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Diprom
    wrote on last edited by
    #1

    Hello, there's a code.

    #include #include #include int main()
    {
    char* x = (char*)calloc(sizeof(char),150);
    char* p = x;
    free(x);
    x = NULL;
    .....
    //p = NULL (get from x)
    return 0;
    }

    Tell me how to get NULL under p when x is removed and set to NULL.

    L CPalliniC 2 Replies Last reply
    0
    • D Diprom

      Hello, there's a code.

      #include #include #include int main()
      {
      char* x = (char*)calloc(sizeof(char),150);
      char* p = x;
      free(x);
      x = NULL;
      .....
      //p = NULL (get from x)
      return 0;
      }

      Tell me how to get NULL under p when x is removed and set to NULL.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You have to set it explicitly, there is no garbage collector or reference counts in C. The two pointers have no connection, so when you free x, pointer p still points to the original block of memory. You must be very careful to manage dynamic memory properly in C, and even in C++.

      1 Reply Last reply
      0
      • D Diprom

        Hello, there's a code.

        #include #include #include int main()
        {
        char* x = (char*)calloc(sizeof(char),150);
        char* p = x;
        free(x);
        x = NULL;
        .....
        //p = NULL (get from x)
        return 0;
        }

        Tell me how to get NULL under p when x is removed and set to NULL.

        CPalliniC Online
        CPalliniC Online
        CPallini
        wrote on last edited by
        #3

        You have not a pointer to pointer. As Richard correctly pointed out, you have two independent pointers to the same block of memory; you have to explicitely set p = NULL;. With a pointer to pointer the scenario would change:

        char* x = (char*)calloc(sizeof(char),150);
        char** p = &x;
        free(x);
        x = NULL;
        printf("%p\n", *p); // here *p is NULL

        In testa che avete, signor di Ceprano?

        L 1 Reply Last reply
        0
        • CPalliniC CPallini

          You have not a pointer to pointer. As Richard correctly pointed out, you have two independent pointers to the same block of memory; you have to explicitely set p = NULL;. With a pointer to pointer the scenario would change:

          char* x = (char*)calloc(sizeof(char),150);
          char** p = &x;
          free(x);
          x = NULL;
          printf("%p\n", *p); // here *p is NULL

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          CPallini wrote:

          *p is NULL

          But p still holds the address of x.

          CPalliniC 1 Reply Last reply
          0
          • L Lost User

            CPallini wrote:

            *p is NULL

            But p still holds the address of x.

            CPalliniC Online
            CPalliniC Online
            CPallini
            wrote on last edited by
            #5

            Of course. That's the purpose of 'pointer to pointer' (I suppose).

            In testa che avete, signor di Ceprano?

            L 1 Reply Last reply
            0
            • CPalliniC CPallini

              Of course. That's the purpose of 'pointer to pointer' (I suppose).

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              I thought it was just to confuse people with small brains (me). ;)

              CPalliniC 1 Reply Last reply
              0
              • L Lost User

                I thought it was just to confuse people with small brains (me). ;)

                CPalliniC Online
                CPalliniC Online
                CPallini
                wrote on last edited by
                #7

                Nope, you are simply drunk because of English team success. :-D

                In testa che avete, signor di Ceprano?

                L 1 Reply Last reply
                0
                • CPalliniC CPallini

                  Nope, you are simply drunk because of English team success. :-D

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  What success, they lost 2:1 to South Africa. They need someone like this guy[^] to lead and inspire.

                  CPalliniC 1 Reply Last reply
                  0
                  • L Lost User

                    What success, they lost 2:1 to South Africa. They need someone like this guy[^] to lead and inspire.

                    CPalliniC Online
                    CPalliniC Online
                    CPallini
                    wrote on last edited by
                    #9

                    OMG, Rugby! :thumbsup:

                    In testa che avete, signor di Ceprano?

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups