9 Replies Latest reply on Apr 10, 2014 11:08 AM by user_1377889

    Use of malloc with Keil compiler

    user_301665782

      Hi every one !

         

      I would need help using malloc() library function.

         

      I would need to create a fuction in C  that take an Array like argument , procces that array  and return a pointer to an array which is the product of the function's procces.

         

      I have tried using  function like   uint8 *Function_X ( uint8 &Arg_ Array[ ] );  like declaration and.....

         

      something like this as definition;

         

        uint8 * Function_X( uint8 & Arg_Array[0 ] ) {

         

       satic uint8 Array_1[[16];

         

                                    fuction body

         

                                 return (array_1);

         

      }

         

       

         

      The caller function  is something like this:

         

       

         

      In the file that call  " uint8  * Function_X ( uint8 & ASrg_Array[ ] )  " ,   I declare a pointer let say  "uint8 Ptr."

         

       

         

      When I call the function is something like this assuming that I want to process  uint8 Payload [16]:

         

         Ptr= Funtion_X ( & Payload[0] );

         

       

         

      Afterward I espect transfer the product of the process through the pointer Ptr, but I can't find the 16 element of the processed array . 

         

      That's why  I am trying to use other method  like malloc.

         

      But the keil compiller declare like mandatory to call  init mempool previously  and up to this point reach my knowledge.

         

       

         

      Can someone else help me with that ?

         

      Thank all of you for your time for help me

        • 1. Re: Use of malloc with Keil compiler
          user_1377889

          You have to call initmempool() and provide a pointer to a data-area and the size of the data-area for malloc() to use.

             

          you may use a function construct as

             

          uint8 * MyFunction(uint8 * Array);

             

          When you want MyFunction() to know about the size of Array you will have to provide it as a second parameter.

             

           

             

          Bob

          • 2. Re: Use of malloc with Keil compiler
            ki.leung

            in your function

               

            uint8  * Function_X ( uint8 & ASrg_Array[ ] )

               

            as you said you are passing the pointer of a int8,  so when you go into the function, creator would not know it is pointing to an array. So it would not be shown.

            • 3. Re: Use of malloc with Keil compiler
              user_14586677

              Maybe I am confused, does this not pass the address to the first element

                 

              in the array, ASrg_Array, to the function ? A pointer ?

                 


                 

              uint8  * Function_X ( uint8 & ASrg_Array[ ] )

                 


                 

              Regards, Dana.

              • 4. Re: Use of malloc with Keil compiler
                user_301665782

                Thank you very much Bob, HL and Dana  for those  helpful suggestions.

                   

                Sorry I made a typo in the following sentence;

                   

                 

                   

                In the file that call  " uint8  * Function_X ( uint8 & ASrg_Array[ ] )  " ,   I declare a pointer let say  "uint8 Ptr."

                   

                  At the end of the sentence should say  Uint8 *Ptr  not Uint8  Ptr; in other words a pointer is been declared.

                   

                 

                   

                But my question now is : How many times can I use  initmempool() and malloc() because I need to call functions like Function_X declared in two .c file. On the other hand the caller funcion  is definited in different .c file .

                   

                May be the following clarify my question;

                   

                 

                   

                main()  -----> call Fucnction _Caller1   and    Fucnction _Caller2

                   

                File_1.c  --->    Fucnction _Caller1  is   calling  to  Function_X1

                   

                File_2 .c  ---->   Fucnction _Caller2  is    calling   to  Function_X2

                   

                File _3.c  -->  definition of  Function_X1

                   

                File_4.c ----->  definition of  Function_X2

                   

                and every .c file has its own .h file

                   

                Whit the above scheme, where and how many times I should declare and define   initmempool() and malloc()

                   

                 

                   

                Again  thank all of you in advance.

                • 5. Re: Use of malloc with Keil compiler
                  user_1377889

                  InitMempool should be called only once (in the initialization part of your project). It tells the malloc-system how much memory shall be managed by the memory-manager.

                     

                  You may call malloc() as often as you like, but you should test the received pointer for nonzero which would otherwise indicate a failure which is usually out-of-memory.

                     

                  Do not forget to free() memory chunks that are no longer needed so that they can be returned to the memory-pool and re-used.

                     

                  There is no restriction of which function calls which at which level, as long as you do not blow the stack by creating too-deep recursions. The stack is 256 Bytes deep and is solely used for the return-addresses.

                     

                   

                     

                  Bob

                  • 6. Re: Use of malloc with Keil compiler
                    user_301665782

                    Bob.!   Thank you so much for the answer

                       

                    I wil try and let see what happend.

                       

                    Have a great day.!

                    • 7. Re: Use of malloc with Keil compiler
                      user_301665782

                      Hi Bob, HL an Dana!!! Thank you very much for your help.

                         

                      The first test I am doing using malloc() seems to be fine.

                         

                      I got  information from other place about use Static varaible for function that receive arrays like argument and return pointers but I verified that doesn't work  for any  function in our hardware platform ( PSoC3), evidently using malloc() was the solution.

                         

                      I am developing an encoder and decoder for a RF link, and I developed code  from the begining with Code Block, because in the first step it was all about software.  Using Code Block every function like I mentioned in my first post was implemented with static variable. The system which has two file, an Encoder and a Decoder  was tested separately  for simulating the RF link.

                         


                      But I've forgotten I was working with a Core I5  in my laptop........ but  this is only a comentary for  fiend people like you  who have help me.

                         

                       

                         

                       

                         

                      Thanks all of you again.

                      • 8. Re: Use of malloc with Keil compiler
                        user_301665782

                        Hi My friends again!!!!

                           

                         

                           

                        Suddently  I tried to debbug one of the .c files who has been workinf find so far,  and the compiler send me a messagee about it does not find source file and shows dissameble fiiles.

                           

                        The  source file is there, in the work space explorer, I can see it, but the debbug ends up in the Dissaembly file.

                           

                        Thank you in advance.

                        • 9. Re: Use of malloc with Keil compiler
                          user_1377889

                          Alex, it would be better to create a new thread for a new problem.

                             

                          Usually the .c files reside in the project directory, did you place some of them into a different path?

                             

                           

                             

                          Bob