function redefined problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wei Li

    function redefined problem

    Hi,
    In my project , I has to include a head file "alloc.h". The malloc()
    function was wrapped in this file as :

    alloc.h
    #define malloc(a) PROJ_MALLOC(a)

    alloc.c
    void *PROJ_MALLOC(si ze_t a){
    ............... ......

    }


    So that everytime I use malloc() it will be invoked to PROJ_MALLOC actually.
    My question is how can I use system malloc() and avoid using PROJ_MALLOC ?


    Thanks!
    Wei


  • Martin Ambuhl

    #2
    Re: function redefined problem

    Wei Li wrote:
    [color=blue]
    > Hi,
    > In my project , I has to include a head file "alloc.h".[/color]

    Unless you are using a broken implementation, there is no reason to
    include a non-standard header named "alloc.h" for malloc. malloc() is
    prototyped in the standard header <stdlib.h>.
    [color=blue]
    > The malloc()
    > function was wrapped in this file as :
    >
    > alloc.h
    > #define malloc(a) PROJ_MALLOC(a)
    > alloc.c
    > void *PROJ_MALLOC(si ze_t a){
    > ............... .....
    > }
    >
    > So that everytime I use malloc() it will be invoked to PROJ_MALLOC actually.
    > My question is how can I use system malloc() and avoid using PROJ_MALLOC ?[/color]

    If you _must_ include that non-standard header for some reason,
    #include "alloc.h"
    #if defined(malloc)
    #undef malloc
    #include <stdlib.h>


    Comment

    • Martin Ambuhl

      #3
      Re: function redefined problem

      Wei Li wrote:
      [color=blue]
      > Hi,
      > In my project , I has to include a head file "alloc.h".[/color]

      Unless you are using a broken implementation, there is no reason to
      include a non-standard header named "alloc.h" for malloc. malloc() is
      prototyped in the standard header <stdlib.h>.
      [color=blue]
      > The malloc()
      > function was wrapped in this file as :
      >
      > alloc.h
      > #define malloc(a) PROJ_MALLOC(a)
      > alloc.c
      > void *PROJ_MALLOC(si ze_t a){
      > ............... .....
      > }
      >
      > So that everytime I use malloc() it will be invoked to PROJ_MALLOC actually.
      > My question is how can I use system malloc() and avoid using PROJ_MALLOC ?[/color]

      If you _must_ include that non-standard header for some reason,
      #include "alloc.h"
      #if defined(malloc)
      #undef malloc
      #endif
      #include <stdlib.h>



      Comment

      • CBFalconer

        #4
        Re: function redefined problem

        Wei Li wrote:[color=blue]
        >
        > In my project , I has to include a head file "alloc.h". The
        > malloc() function was wrapped in this file as :[/color]

        Wrong. The appropriate header file is <stdlib.h>

        --
        Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
        Available for consulting/temporary embedded and systems.
        <http://cbfalconer.home .att.net> USE worldnet address!


        Comment

        • Francois Grieu

          #5
          Re: function redefined problem

          "Wei Li" <liwei_guard-pub@yahoo.com> wrote:
          [color=blue]
          > In my project , I have to include a head file "alloc.h". The malloc()
          > function was wrapped in this file as :
          >
          > alloc.h
          > #define malloc(a) PROJ_MALLOC(a)
          >
          > alloc.c
          > void *PROJ_MALLOC(si ze_t a){
          > ............... .....
          >
          > }
          >
          >
          > So that everytime I use malloc() it will be invoked to PROJ_MALLOC actually.
          > My question is how can I use system malloc() and avoid using PROJ_MALLOC ?[/color]


          Assuming file "alloc.h" defines size_t in a manner compatible with <stdlib.h>

          // misc.c, to be compiled separatly and linked with the rest
          #include <stdlib.h>
          void *stdlibmalloc(s ize_t size) { return malloc(size): }

          // main.c
          #include "alloc.h" // the above "alloc.h" with: #define malloc(a) PROJ_MALLOC(a)
          void *stdlibmalloc(s ize_t size);
          int main(void)
          {
          char *p1, *p2;
          p1 = stdlibmalloc(8) ; // malloc() from the implementation of <stdlib.h>
          p2 = malloc(8); // PROJ_MALLOC() from "alloc.c"
          return p1!=NULL & p2!=NULL;
          }


          François Grieu

          Comment

          • Francois Grieu

            #6
            Re: function redefined problem

            "Wei Li" <liwei_guard-pub@yahoo.com> wrote:
            [color=blue]
            > In my project , I have to include a head file "alloc.h". The malloc()
            > function was wrapped in this file as :
            >
            > alloc.h
            > #define malloc(a) PROJ_MALLOC(a)
            >
            > alloc.c
            > void *PROJ_MALLOC(si ze_t a){
            > ............... .....
            >
            > }
            >
            >
            > So that everytime I use malloc() it will be invoked to PROJ_MALLOC actually.
            > My question is how can I use system malloc() and avoid using PROJ_MALLOC ?[/color]


            Assuming file "alloc.h" defines size_t in a manner compatible with <stdlib.h>

            // misc.c, to be compiled separatly and linked with the rest
            #include <stdlib.h>
            void *stdlibmalloc(s ize_t size) { return malloc(size); }

            // main.c
            #include "alloc.h" // the above "alloc.h" with: #define malloc(a) PROJ_MALLOC(a)
            void *stdlibmalloc(s ize_t size);
            int main(void)
            {
            char *p1, *p2;
            p1 = stdlibmalloc(8) ; // malloc() from the implementation of <stdlib.h>
            p2 = malloc(8); // PROJ_MALLOC() from "alloc.c"
            return p1!=NULL & p2!=NULL;
            }


            François Grieu

            Comment

            • Francois Grieu

              #7
              Re: function redefined problem

              "Wei Li" <liwei_guard-pub@yahoo.com> wrote:
              [color=blue]
              > In my project , I have to include a head file "alloc.h". The malloc()
              > function was wrapped in this file as :
              >
              > alloc.h
              > #define malloc(a) PROJ_MALLOC(a)
              >
              > alloc.c
              > void *PROJ_MALLOC(si ze_t a){
              > ............... .....
              >
              > }
              >
              >
              > So that everytime I use malloc() it will be invoked to PROJ_MALLOC actually.
              > My question is how can I use system malloc() and avoid using PROJ_MALLOC ?[/color]


              Assuming file "alloc.h" defines size_t and NULL in a manner compatible
              with what <stdlib.h> does, this should work:

              // misc.c, to be compiled separatly and linked with the rest
              #include <stdlib.h>
              void *stdlibmalloc(s ize_t size) { return malloc(size); }

              // main.c
              #include "alloc.h" // the above "alloc.h" with: #define malloc(a) PROJ_MALLOC(a)
              void *stdlibmalloc(s ize_t size);
              int main(void)
              {
              char *p1, *p2;
              p1 = stdlibmalloc(8) ; // malloc() from the implementation of <stdlib.h>
              p2 = malloc(8); // PROJ_MALLOC() from "alloc.c"
              return p1!=NULL & p2!=NULL;
              }


              François Grieu

              Comment

              • SM Ryan

                #8
                Re: function redefined problem

                Use (malloc)(size), assuming the system malloc is a function declaration
                and not another #define.

                (cd /tmp
                cat >x.c <<':eof'
                int x(int a,int b) {return a+b;}
                #define x(a,b) ((a)*(b))

                int y(void) {
                int q = x(1,2);
                int r = (x)(1,2);
                return q/r;
                }
                :eof
                cc -E x.c)

                # 1 "x.c"
                #pragma GCC set_debug_pwd "/tmp"
                # 1 "<built-in>"
                # 1 "<command line>"
                # 1 "x.c"
                int x(int a,int b) {return a+b;}


                int y(void) {
                int q = ((1)*(2));
                int r = (x)(1,2);
                return q/r;
                }

                --
                SM Ryan http://www.rawbw.com/~wyrmwif/
                Where do you get those wonderful toys?

                Comment

                • Dan Pop

                  #9
                  Re: function redefined problem

                  In <412BFA43.D027E A65@yahoo.com> CBFalconer <cbfalconer@yah oo.com> writes:
                  [color=blue]
                  >Wei Li wrote:[color=green]
                  >>
                  >> In my project , I has to include a head file "alloc.h". The
                  >> malloc() function was wrapped in this file as :[/color]
                  >
                  >Wrong. The appropriate header file is <stdlib.h>[/color]

                  You forgot to engage your brain, again.

                  Dan
                  --
                  Dan Pop
                  DESY Zeuthen, RZ group
                  Email: Dan.Pop@ifh.de

                  Comment

                  • Francois Grieu

                    #10
                    Re: function redefined problem

                    In article <10iotusgh2egg7 f@corp.supernew s.com>,
                    SM Ryan <wyrmwif@tang o-sierra-oscar-foxtrot-tango.fake.org> didn't wrote:
                    [color=blue]
                    > OP wrote[color=green]
                    >> My question is how can I use system malloc() and avoid using PROJ_MALLOC ?[/color][/color]
                    [color=blue]
                    > Use (malloc)(size), assuming the system malloc is a function declaration
                    > and not another #define.[/color]

                    Cool !
                    Illustrated below. Is this
                    - directly implied by the standard ?
                    - portable across actual implementations ?


                    int foo(int x) { return 2*x; }
                    int bar(void) { return 2; }
                    #define foo(x) (3*x)
                    #define bar() (3)
                    #include <stdio.h>
                    int main(void)
                    {
                    printf("%d %d %d %d\n",
                    foo(1), /* 3 */
                    (foo)(1), /* 2 */
                    bar(), /* 3 */
                    (bar)() /* 2 */
                    );
                    return 0;
                    }


                    François Grieu

                    Comment

                    • Ben Pfaff

                      #11
                      Re: function redefined problem

                      Francois Grieu <fgrieu@francen et.fr> writes:

                      [on avoiding a macro definition of a library function by
                      enclosing the name in parentheses][color=blue]
                      > Illustrated below. Is this
                      > - directly implied by the standard ?
                      > - portable across actual implementations ?[/color]

                      Implied? Actually it's explicitly stated. See C99 7.1.4#1:

                      Any macro definition of a function can be suppressed locally
                      by enclosing the name of the function in parentheses,
                      because the name is then not followed by the left
                      parenthesis that indicates expansion of a macro function
                      name. For the same syntactic reason, it is permitted to take
                      the address of a library function even if it is also defined
                      as a macro.
                      --
                      "Some programming practices beg for errors;
                      this one is like calling an 800 number
                      and having errors delivered to your door."
                      --Steve McConnell

                      Comment

                      • Wei Li

                        #12
                        Re: function redefined problem

                        It's wonderful !

                        "SM Ryan" <wyrmwif@tang o-sierra-oscar-foxtrot-tango.fake.org> wrote in
                        message news:10iotusgh2 egg7f@corp.supe rnews.com...[color=blue]
                        > Use (malloc)(size), assuming the system malloc is a function declaration
                        > and not another #define.
                        >
                        > (cd /tmp
                        > cat >x.c <<':eof'
                        > int x(int a,int b) {return a+b;}
                        > #define x(a,b) ((a)*(b))
                        >
                        > int y(void) {
                        > int q = x(1,2);
                        > int r = (x)(1,2);
                        > return q/r;
                        > }
                        > :eof
                        > cc -E x.c)
                        >
                        > # 1 "x.c"
                        > #pragma GCC set_debug_pwd "/tmp"
                        > # 1 "<built-in>"
                        > # 1 "<command line>"
                        > # 1 "x.c"
                        > int x(int a,int b) {return a+b;}
                        >
                        >
                        > int y(void) {
                        > int q = ((1)*(2));
                        > int r = (x)(1,2);
                        > return q/r;
                        > }
                        >
                        > --
                        > SM Ryan http://www.rawbw.com/~wyrmwif/
                        > Where do you get those wonderful toys?[/color]


                        Comment

                        Working...