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. problem in memory allocation for a pointer

problem in memory allocation for a pointer

Scheduled Pinned Locked Moved C / C++ / MFC
performancehelpquestion
10 Posts 5 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.
  • L Offline
    L Offline
    laksh2204
    wrote on last edited by
    #1

    Hi all, I am getting seg fault when trying this piece of code. I'm not getting what is went wrong here. void fun(int* ptr){ ptr = (int*)malloc(sizeof(int)); } int main(){ int *ptr = NULL; fun(ptr); *ptr = 5; //seg fault cout<<*ptr; }

    C 1 Reply Last reply
    0
    • L laksh2204

      Hi all, I am getting seg fault when trying this piece of code. I'm not getting what is went wrong here. void fun(int* ptr){ ptr = (int*)malloc(sizeof(int)); } int main(){ int *ptr = NULL; fun(ptr); *ptr = 5; //seg fault cout<<*ptr; }

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Well of course, you are passing a pointer to a function (a certain address, which is NULL). In the function, you assign a new address to this pointer but the pointer is only a copy of the original, so, your original pointer is not affected. To make things more easier, think that a pointer is just a variable that holds an address. When you pass this variable to a function, the function makes a copy of it. It still points to the same address but if you change the address, you only modify the local copy.

      Cédric Moonen Software developer
      Charting control [v1.5] OpenGL game tutorial in C++

      L 1 Reply Last reply
      0
      • C Cedric Moonen

        Well of course, you are passing a pointer to a function (a certain address, which is NULL). In the function, you assign a new address to this pointer but the pointer is only a copy of the original, so, your original pointer is not affected. To make things more easier, think that a pointer is just a variable that holds an address. When you pass this variable to a function, the function makes a copy of it. It still points to the same address but if you change the address, you only modify the local copy.

        Cédric Moonen Software developer
        Charting control [v1.5] OpenGL game tutorial in C++

        L Offline
        L Offline
        laksh2204
        wrote on last edited by
        #3

        ok then what change is required here. so that i can i can put some value into memory allocated in fun() function.

        C C 2 Replies Last reply
        0
        • L laksh2204

          ok then what change is required here. so that i can i can put some value into memory allocated in fun() function.

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          Exactly the same as if it was a variable: pass it by reference.

          Cédric Moonen Software developer
          Charting control [v1.5] OpenGL game tutorial in C++

          L 1 Reply Last reply
          0
          • C Cedric Moonen

            Exactly the same as if it was a variable: pass it by reference.

            Cédric Moonen Software developer
            Charting control [v1.5] OpenGL game tutorial in C++

            L Offline
            L Offline
            laksh2204
            wrote on last edited by
            #5

            Like this??? void fun(int* ptr){ ptr = (int*)malloc(sizeof(int)); } int main(){ int ptr; fun(&ptr); ptr = 5; cout<<ptr; } I dont think so.. probably you mean something else.. that i am not getting.

            modified on Monday, September 29, 2008 8:40 AM

            D C 2 Replies Last reply
            0
            • L laksh2204

              Like this??? void fun(int* ptr){ ptr = (int*)malloc(sizeof(int)); } int main(){ int ptr; fun(&ptr); ptr = 5; cout<<ptr; } I dont think so.. probably you mean something else.. that i am not getting.

              modified on Monday, September 29, 2008 8:40 AM

              D Offline
              D Offline
              Daniel Kanev
              wrote on last edited by
              #6

              Use &. void fun(int*&ptr) { } then in the main function fun(ptr) Is it working now?

              1 Reply Last reply
              0
              • L laksh2204

                Like this??? void fun(int* ptr){ ptr = (int*)malloc(sizeof(int)); } int main(){ int ptr; fun(&ptr); ptr = 5; cout<<ptr; } I dont think so.. probably you mean something else.. that i am not getting.

                modified on Monday, September 29, 2008 8:40 AM

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                Well, you need something a bit more elaborated (plain C):

                void myAlloc(int ** ptr)
                {
                *ptr = (int *) malloc(sizeof(int));
                }
                int main()
                {
                int * pMyInt;
                myAlloc( &pMyInt);
                *pMyInt = 5;
                printf("%d\n", *pMyInt);
                free( pMyInt)
                }

                :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                L 1 Reply Last reply
                0
                • C CPallini

                  Well, you need something a bit more elaborated (plain C):

                  void myAlloc(int ** ptr)
                  {
                  *ptr = (int *) malloc(sizeof(int));
                  }
                  int main()
                  {
                  int * pMyInt;
                  myAlloc( &pMyInt);
                  *pMyInt = 5;
                  printf("%d\n", *pMyInt);
                  free( pMyInt)
                  }

                  :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  L Offline
                  L Offline
                  laksh2204
                  wrote on last edited by
                  #8

                  Yes, this is exactly what I was trying to do.. Thanks a lot, all of you :-D

                  C 1 Reply Last reply
                  0
                  • L laksh2204

                    Yes, this is exactly what I was trying to do.. Thanks a lot, all of you :-D

                    C Offline
                    C Offline
                    CPallini
                    wrote on last edited by
                    #9

                    :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    1 Reply Last reply
                    0
                    • L laksh2204

                      ok then what change is required here. so that i can i can put some value into memory allocated in fun() function.

                      C Offline
                      C Offline
                      cofi
                      wrote on last edited by
                      #10

                      try this int **ptr;

                      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