473,395 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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(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
Nov 14 '05 #1
11 3496
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>
Nov 14 '05 #2
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>

Nov 14 '05 #3
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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #4
"Wei Li" <li*************@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
Nov 14 '05 #5
"Wei Li" <li*************@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
Nov 14 '05 #6
"Wei Li" <li*************@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
Nov 14 '05 #7
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?
Nov 14 '05 #8
In <41***************@yahoo.com> CBFalconer <cb********@yahoo.com> writes:
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>


You forgot to engage your brain, again.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #9
In article <10*************@corp.supernews.com>,
SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> didn't wrote:
OP wrote
My question is how can I use system malloc() and avoid using PROJ_MALLOC ?
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
Nov 14 '05 #10
Francois Grieu <fg****@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
Nov 14 '05 #11
It's wonderful !

"SM Ryan" <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> wrote in
message news:10*************@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?

Nov 14 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Marc Schellens | last post by:
Including Python.h I got the following warining warning: "_POSIX_C_SOURCE" redefined in Python.h: #define _POSIX_C_SOURCE 200112L in features.h (included from string):
7
by: James | last post by:
Should be a simple question...not sure why this ISN'T having a problem: For x = 1 to 5 Dim y Next Shouldn't that fire a "Name Redefined" error the way this does: For x = 1 to 5 Dim y
5
by: Honestmath | last post by:
Hello everyone, I'm using a root finding algorithm in a function that only takes as arguments a function pointer and two variables that represent guesses at the roots of the function. int...
23
by: David McCulloch | last post by:
QUESTION-1: How can I detect if Norton Internet Security is blocking pop-ups? QUESTION-2a: How could I know if a particular JavaScript function has been declared? QUESTION-2b: How could I...
4
by: deanfamily | last post by:
Here's the problem: I have a function that references another one in this manner: class circleType: public pointType so, pointType is redefined by circleType, thereby giving circleType access to...
25
by: JeffS | last post by:
Honest, I scoured the comp.lang.c.faq for this but found nothing. :) Is there a library function for placing the cursor position in the console? Or is it something that can only be done with a...
5
by: dragon | last post by:
error! 33 E:\program\wukexin\file\file.h `bool My_lib::Cfileinfo::initialize(const char*)' is private 19 E:\program\wukexin\dir\dir.cpp within this context ////////////////////////////////////...
3
by: hugheslin | last post by:
Hi, Please consider the following classes: ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class Shape {...
12
by: shyam | last post by:
Hi all is there any way the determine which function currently is executing in a process ? For example we can find the file name using __FILE__ and line number using __LINE__ macros ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.