function redefined problem

W

Wei Li

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
 
M

Martin Ambuhl

Wei said:
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 said:
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>
 
M

Martin Ambuhl

Wei said:
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 said:
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>
 
C

CBFalconer

Wei said:
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>
 
F

Francois Grieu

Wei Li said:
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
 
F

Francois Grieu

Wei Li said:
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
 
F

Francois Grieu

Wei Li said:
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
 
S

SM Ryan

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;
}
 
F

Francois Grieu

SM Ryan said:
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
 
B

Ben Pfaff

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top