For Programmers: Free Programming Magazines  


Home > Archive > C > August 2004 > function redefined problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author function redefined problem
Wei Li

2004-08-26, 3:56 pm

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(size_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

2004-08-26, 3:56 pm

Wei Li wrote:

> Hi,
> In my project , I has to include a head file "alloc.h".


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>.

> The malloc()
> function was wrapped in this file as :
>
> alloc.h
> #define malloc(a) PROJ_MALLOC(a)
> alloc.c
> void *PROJ_MALLOC(size_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 ?


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


Martin Ambuhl

2004-08-26, 3:56 pm

Wei Li wrote:

> Hi,
> In my project , I has to include a head file "alloc.h".


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>.

> The malloc()
> function was wrapped in this file as :
>
> alloc.h
> #define malloc(a) PROJ_MALLOC(a)
> alloc.c
> void *PROJ_MALLOC(size_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 ?


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



CBFalconer

2004-08-26, 3:57 pm

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


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

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


Francois Grieu

2004-08-26, 3:57 pm

"Wei Li" <liwei_guard-pub@yahoo.com> wrote:

> 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(size_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 ?



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(size_t size) { return malloc(size): }

// main.c
#include "alloc.h" // the above "alloc.h" with: #define malloc(a) PROJ_MALLOC(a)
void *stdlibmalloc(size_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
Francois Grieu

2004-08-26, 3:57 pm

"Wei Li" <liwei_guard-pub@yahoo.com> wrote:

> 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(size_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 ?



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(size_t size) { return malloc(size); }

// main.c
#include "alloc.h" // the above "alloc.h" with: #define malloc(a) PROJ_MALLOC(a)
void *stdlibmalloc(size_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
Francois Grieu

2004-08-26, 3:57 pm

"Wei Li" <liwei_guard-pub@yahoo.com> wrote:

> 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(size_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 ?



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(size_t size) { return malloc(size); }

// main.c
#include "alloc.h" // the above "alloc.h" with: #define malloc(a) PROJ_MALLOC(a)
void *stdlibmalloc(size_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
SM Ryan

2004-08-26, 3:57 pm

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?
Dan Pop

2004-08-26, 3:57 pm

In <412BFA43.D027EA65@yahoo.com> CBFalconer <cbfalconer@yahoo.com> writes:

>Wei Li wrote:
>
>Wrong. The appropriate header file is <stdlib.h>


You forgot to engage your brain, again.

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

2004-08-26, 3:57 pm

In article <10iotusgh2egg7f@corp.supernews.com>,
SM Ryan <wyrmwif@tango-sierra-oscar-foxtrot-tango.fake.org> didn't wrote:

> OP wrote
[color=darkred]
> Use (malloc)(size), assuming the system malloc is a function declaration
> and not another #define.


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
Ben Pfaff

2004-08-26, 3:57 pm

Francois Grieu <fgrieu@francenet.fr> writes:

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


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
Wei Li

2004-08-30, 8:55 am

It's wonderful !

"SM Ryan" <wyrmwif@tango-sierra-oscar-foxtrot-tango.fake.org> wrote in
message news:10iotusgh2egg7f@corp.supernews.com...
> 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?



Francois Grieu

2004-08-31, 3:55 pm

"Wei Li" <liwei_guard-pub@yahoo.com> wrote:

> 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(size_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 ?



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(size_t size) { return malloc(size); }

// main.c
#include "alloc.h" // the above "alloc.h" with: #define malloc(a) PROJ_MALLOC(a)
void *stdlibmalloc(size_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
Martin Ambuhl

2004-08-31, 3:55 pm

Wei Li wrote:

> Hi,
> In my project , I has to include a head file "alloc.h".


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>.

> The malloc()
> function was wrapped in this file as :
>
> alloc.h
> #define malloc(a) PROJ_MALLOC(a)
> alloc.c
> void *PROJ_MALLOC(size_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 ?


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


Martin Ambuhl

2004-08-31, 3:55 pm

Wei Li wrote:

> Hi,
> In my project , I has to include a head file "alloc.h".


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>.

> The malloc()
> function was wrapped in this file as :
>
> alloc.h
> #define malloc(a) PROJ_MALLOC(a)
> alloc.c
> void *PROJ_MALLOC(size_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 ?


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



Francois Grieu

2004-08-31, 8:55 pm

In article <10iotusgh2egg7f@corp.supernews.com>,
SM Ryan <wyrmwif@tango-sierra-oscar-foxtrot-tango.fake.org> didn't wrote:

> OP wrote
[color=darkred]
> Use (malloc)(size), assuming the system malloc is a function declaration
> and not another #define.


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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com