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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  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.
  • F Offline
    F Offline
    fx9200
    wrote on last edited by
    #1

    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

    T C F 3 Replies 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

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

      please show a brief piece of the code you wrote for that...


      [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

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

        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]

        F T 2 Replies Last reply
        0
        • T toxcct

          please show a brief piece of the code you wrote for that...


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

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

          the code is too long but : #define STRUCT struct mystruct void function (void *input){ .... pointer = (STRUCT*) malloc (sizeof(STRUCT)); //filling in input attributes... input = pointer; } main(){ void *ptr; function (ptr); printf("%...", ptr->..........); } this work only if i allocate the memory for ptr in the main program

          T P 2 Replies Last reply
          0
          • F fx9200

            the code is too long but : #define STRUCT struct mystruct void function (void *input){ .... pointer = (STRUCT*) malloc (sizeof(STRUCT)); //filling in input attributes... input = pointer; } main(){ void *ptr; function (ptr); printf("%...", ptr->..........); } this work only if i allocate the memory for ptr in the main program

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

            fx9200 wrote:

            pointer = (STRUCT*) malloc (sizeof(STRUCT));

            why don't you directly write into input ?

            input = (STRUCT*) malloc (sizeof(STRUCT));


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

            F 1 Reply Last reply
            0
            • T toxcct

              fx9200 wrote:

              pointer = (STRUCT*) malloc (sizeof(STRUCT));

              why don't you directly write into input ?

              input = (STRUCT*) malloc (sizeof(STRUCT));


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

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

              i tried it i had the same result in all cases

              1 Reply Last reply
              0
              • F fx9200

                the code is too long but : #define STRUCT struct mystruct void function (void *input){ .... pointer = (STRUCT*) malloc (sizeof(STRUCT)); //filling in input attributes... input = pointer; } main(){ void *ptr; function (ptr); printf("%...", ptr->..........); } this work only if i allocate the memory for ptr in the main program

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

                Change this to,

                void function (void **input){
                ....
                *pointer = (STRUCT*) malloc (sizeof(STRUCT));

                //filling in input attributes...

                input = pointer;
                }

                main(){
                void *ptr;
                function (&ptr);
                printf("%...", ptr->..........);
                }

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

                1 Reply Last reply
                0
                • 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]

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

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

                  D 1 Reply Last reply
                  0
                  • 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
                                          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