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. Allocate memory inside function ??

Allocate memory inside function ??

Scheduled Pinned Locked Moved C / C++ / MFC
22 Posts 6 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.
  • C Cedric Moonen

    fx9200 wrote:

    the memory space was not kept after function return;

    You need to pass your pointer by reference. Otherwise it will still point at the previous memory location.


    Cédric Moonen Software developer
    Charting control [v1.1]

    T Offline
    T Offline
    toxcct
    wrote on last edited by
    #9

    he seems to doing C, not C++... so no reference available (in the sense of C++ understands it). but pointers remain good


    [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

    F 1 Reply Last reply
    0
    • T toxcct

      he seems to doing C, not C++... so no reference available (in the sense of C++ understands it). but pointers remain good


      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

      F Offline
      F Offline
      fx9200
      wrote on last edited by
      #10

      :rolleyes: yes so ?

      T P 2 Replies Last reply
      0
      • F fx9200

        :rolleyes: yes so ?

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #11

        fx9200 wrote:

        yes so ?

        so, prasad_som[^] gave you an answer... hve you read it ?


        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

        F 1 Reply Last reply
        0
        • F fx9200

          Hello I want to allocate memory for a void* pointer inside a function; i tried to do that by using malloc to the pointer passed as an argument ; the memory space was not kept after function return; how kan i do that thanks

          F Offline
          F Offline
          fx9200
          wrote on last edited by
          #12

          I tried this it works , but i cannot fix the allocated emory size by a no-static way void fill(void *input); struct mystruct { char *ch; int n; }; #define ARG_SIZE sizeof(struct mystruct) int main(){ void *ptr; ptr = (void*) malloc (ARG_SIZE); fill(ptr); printf ("%i",((struct mystruct*)ptr)->n); printf ("--%s",((struct mystruct*)ptr) ->ch); return 0; } void fill(void *input){ struct mystruct* st; st = (struct mystruct*) input; st ->n = 3;//operation is ok st->ch = (char *) malloc (3*sizeof(char)); strcpy(st->ch,"OK"); input = (void*) st; }

          F 1 Reply Last reply
          0
          • F fx9200

            :rolleyes: yes so ?

            P Offline
            P Offline
            prasad_som
            wrote on last edited by
            #13

            Though, its not applicable in C context. You need to understand theory behind Cedric's suggesstion. In you original code, you was passing pointer argument. Though its a pointer, it will be passed by value, and inside that function, memory allocated will be not at address you expected. In that case you need to pass pointer to pointer, as shown in my previous reply.

            Prasad Notifier using ATL | Operator new[],delete[][^]

            F 1 Reply Last reply
            0
            • T toxcct

              fx9200 wrote:

              yes so ?

              so, prasad_som[^] gave you an answer... hve you read it ?


              [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

              F Offline
              F Offline
              fx9200
              wrote on last edited by
              #14

              i'm trying thanks

              1 Reply Last reply
              0
              • P prasad_som

                Though, its not applicable in C context. You need to understand theory behind Cedric's suggesstion. In you original code, you was passing pointer argument. Though its a pointer, it will be passed by value, and inside that function, memory allocated will be not at address you expected. In that case you need to pass pointer to pointer, as shown in my previous reply.

                Prasad Notifier using ATL | Operator new[],delete[][^]

                F Offline
                F Offline
                fx9200
                wrote on last edited by
                #15

                but how the code below works; i allocated memory for chr* inside function

                P 1 Reply Last reply
                0
                • F fx9200

                  but how the code below works; i allocated memory for chr* inside function

                  P Offline
                  P Offline
                  prasad_som
                  wrote on last edited by
                  #16

                  Which code you are talking about ?

                  Prasad Notifier using ATL | Operator new[],delete[][^]

                  F 1 Reply Last reply
                  0
                  • F fx9200

                    I tried this it works , but i cannot fix the allocated emory size by a no-static way void fill(void *input); struct mystruct { char *ch; int n; }; #define ARG_SIZE sizeof(struct mystruct) int main(){ void *ptr; ptr = (void*) malloc (ARG_SIZE); fill(ptr); printf ("%i",((struct mystruct*)ptr)->n); printf ("--%s",((struct mystruct*)ptr) ->ch); return 0; } void fill(void *input){ struct mystruct* st; st = (struct mystruct*) input; st ->n = 3;//operation is ok st->ch = (char *) malloc (3*sizeof(char)); strcpy(st->ch,"OK"); input = (void*) st; }

                    F Offline
                    F Offline
                    fx9200
                    wrote on last edited by
                    #17

                    void fill(void *input); struct mystruct { char *ch; int n; }; #define ARG_SIZE sizeof(struct mystruct) int main(){ void *ptr; ptr = (void*) malloc (ARG_SIZE); fill(ptr); printf ("%i",((struct mystruct*)ptr)->n); printf ("--%s",((struct mystruct*)ptr) ->ch); return 0; } void fill(void *input){ struct mystruct* st; st = (struct mystruct*) input; st ->n = 3;//operation is ok st->ch = (char *) malloc (3*sizeof(char)); strcpy(st->ch,"OK"); input = (void*) st;

                    1 Reply Last reply
                    0
                    • P prasad_som

                      Which code you are talking about ?

                      Prasad Notifier using ATL | Operator new[],delete[][^]

                      F Offline
                      F Offline
                      fx9200
                      wrote on last edited by
                      #18

                      void fill(void *input); struct mystruct { char *ch; int n; }; #define ARG_SIZE sizeof(struct mystruct) int main(){ void *ptr; ptr = (void*) malloc (ARG_SIZE); fill(ptr); printf ("%i",((struct mystruct*)ptr)->n); printf ("--%s",((struct mystruct*)ptr) ->ch); return 0; } void fill(void *input){ struct mystruct* st; st = (struct mystruct*) input; st ->n = 3;//operation is ok st->ch = (char *) malloc (3*sizeof(char)); strcpy(st->ch,"OK"); input = (void*) st; }

                      P R 2 Replies Last reply
                      0
                      • F fx9200

                        void fill(void *input); struct mystruct { char *ch; int n; }; #define ARG_SIZE sizeof(struct mystruct) int main(){ void *ptr; ptr = (void*) malloc (ARG_SIZE); fill(ptr); printf ("%i",((struct mystruct*)ptr)->n); printf ("--%s",((struct mystruct*)ptr) ->ch); return 0; } void fill(void *input){ struct mystruct* st; st = (struct mystruct*) input; st ->n = 3;//operation is ok st->ch = (char *) malloc (3*sizeof(char)); strcpy(st->ch,"OK"); input = (void*) st; }

                        P Offline
                        P Offline
                        prasad_som
                        wrote on last edited by
                        #19

                        Here, though you are allocating memory for ch member of mystruct inside the function, you have passed mystruct* as parameter and , for that memory is already allocated, before calling this function. And bitwise(shallow) copy happens in this case. p.s. Dont's create multiple threads for same code.

                        Prasad Notifier using ATL | Operator new[],delete[][^]

                        F 1 Reply Last reply
                        0
                        • P prasad_som

                          Here, though you are allocating memory for ch member of mystruct inside the function, you have passed mystruct* as parameter and , for that memory is already allocated, before calling this function. And bitwise(shallow) copy happens in this case. p.s. Dont's create multiple threads for same code.

                          Prasad Notifier using ATL | Operator new[],delete[][^]

                          F Offline
                          F Offline
                          fx9200
                          wrote on last edited by
                          #20

                          ok thanks

                          1 Reply Last reply
                          0
                          • F fx9200

                            void fill(void *input); struct mystruct { char *ch; int n; }; #define ARG_SIZE sizeof(struct mystruct) int main(){ void *ptr; ptr = (void*) malloc (ARG_SIZE); fill(ptr); printf ("%i",((struct mystruct*)ptr)->n); printf ("--%s",((struct mystruct*)ptr) ->ch); return 0; } void fill(void *input){ struct mystruct* st; st = (struct mystruct*) input; st ->n = 3;//operation is ok st->ch = (char *) malloc (3*sizeof(char)); strcpy(st->ch,"OK"); input = (void*) st; }

                            R Offline
                            R Offline
                            Roger Stoltz
                            wrote on last edited by
                            #21

                            What I think you need to understand after having read your previous posts, is that when you call a function the arguments passed are copies on the stack. Have a look here[^] if you're not sure what the stack is and how it is used. Let's examine this with some code examples. A function called AddOne is used for increasing a variable by the value of 1.

                            void AddOne( int nTheVariable )
                            {
                                nTheVariable = nTheVariable + 1;
                            }
                            
                            void main()
                            {
                                int nMyValue = 0;
                                printf( "The value is %d.\n", nMyValue ); // Prints "The value is 0."
                                AddOne( nMyValue );
                                printf( "The value is %d.\n", nMyValue ); // Also prints "The value is 0."
                            }
                            

                            When AddOne() is declared as above, the value of nMyValue is copied to the stack and read by AddOne(). When the function returns is has indeed increased its local copy (nTheVariable) by one, but nMyValue still remains the same since it was a copy of it that was passed to the function. If you want to alter the value of nMyValue, you have to pass its location as argument to AddOne(). "Location" in this aspect means "the address of". In code it would look like this:

                            void AddOne( int* pnTheVariable )
                            {
                                // Here we've got a copy of the address of the variable so
                                // we can modify it at its original location 
                                *pnTheVariable = *pnTheVariable + 1;
                            }
                            
                            void main()
                            {
                                int nMyValue = 0;
                                printf( "The value is %d.\n", nMyValue ); // Prints "The value is 0."
                                AddOne( &nMyValue ); // Pass a copy of the address of nMyValue
                                printf( "The value is %d.\n", nMyValue ); // Prints "The value is 1."
                            }
                            

                            In your case you want to allocate memory and you always assign it to a pointer. To be able to alter the address assigned to a pointer, you have to pass a copy of the address of the pointer to the function. This is still the same as above, but now you want to alter the value of a pointer. In code it would look something like this if you want to allocate memory for three ints:

                            void Allocate( int** ppnTheMemory )
                            {
                                *ppTheMemory = (int*)malloc( 3 * sizeof( int ) );
                            }
                            
                            void main()
                            {
                                int* pnMyValues = NULL;
                                Allocate( &pnMyValue ); // Pass the address of the pointer
                                if( pnMyValues )
                                {
                                    free( pnMyValues );
                            
                            1 Reply Last reply
                            0
                            • F fx9200

                              how to pass reference(pointer) by reference ? please give example

                              D Offline
                              D Offline
                              David Crow
                              wrote on last edited by
                              #22

                              fx9200 wrote:

                              how to pass reference(pointer) by reference ? please give example

                              See here.


                              "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                              "Judge not by the eye but by the heart." - Native American Proverb

                              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