473,626 Members | 3,198 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Difference between Macro and Function...

Hi...in some posts i've read...somethin g about using macro rather then
function...but difference ??.

Best regards....

Nov 14 '05 #1
8 10860
lasek <cl************ **@acrm.it> scribbled the following:
Hi...in some posts i've read...somethin g about using macro rather then
function...but difference ??.


Have you read a C textbook? Macros and functions are entirely
different. A macro is, in principle, text processing. Like "Find and
replace" in your text editor. Only it happens automatically in the
compiling process, after tokenisation but before lexical analysis.
Functions, OTOH, are genuine C language constructs and not merely text
processing features.
Functions are handled in all states of the compiling process, right up
to linking. The generated object files make a distinction between each
function, and the linker then links all calls between functions
together. In some cases, even the run-time program knows which function
it's currently executing.
None of this is the case with a macro. Once the preprocessor has
expanded the macro, the rest of the process is handled as if the macro
never existed.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"It's not survival of the fattest, it's survival of the fittest."
- Ludvig von Drake
Nov 14 '05 #2
Thanks a lot for your explanation...b ut i need more info about how a
variable was handled into a macro...in particular someone wrote that
free()..is a macro or function i don't remember so which difference
between a free() macro and a free() function ??....
remember ny head is very strong :-)))
Nov 14 '05 #3
lasek wrote:
Thanks a lot for your explanation...b ut i need more info about how a
variable was handled into a macro...in particular someone wrote that
free()..is a macro or function i don't remember so which difference
between a free() macro and a free() function ??....
remember ny head is very strong :-)))


free() is the companion function to malloc() and is definitely a function.
Look up the library documentation; malloc is used to request memory space
and initialize a pointer to it and free() is used to release that memory
space.

macros only exist in the text pre-processing stage of compilation during
which they are expanded into code whereas functions are items which exist
throughout the whole process of compilation, link and execution.

I agree with Joona, the best way forward for you is to read a C text book;
you will learn a lot more about this than from ad-hoc queries about it in
this newsgroup.
Nov 14 '05 #4

KOn Thu, 21 Oct 2004, lasek wrote:
Thanks a lot for your explanation...b ut i need more info about how a
variable was handled into a macro...in particular someone wrote that
free()..is a macro or function i don't remember so which difference
between a free() macro and a free() function ??....
remember ny head is very strong :-)))


Yeah i wrote that it was todo with being able to change the
contents of a variable using a macros but the variable would have
been out of scope for a function to change it.

Take the follwing example.
Which prints
Macro: 1 Func: 0

#include <stdio.h>

#define ADD(x) x++;
void add(int x) { x++; }

int main() {
int macro = 0;
int func = 0;

ADD(macro);
add(func);

printf("Macro: %d Func: %d\n", macro, func);
return 0;
}

Now if you run the same program though the c pre processor which is the
first stage of the compile you actually get the following program
though i cut stdio.h from this output.

After
gcc -E file.c

# 5 "t.c"
void add(int x) { x++; }

int main() {
int macro = 0;
int func = 0;

macro++;;
add(func);

printf("Macro: %d Func: %d\n", macro, func);
return 0;
}

The macro has completly disappeared and the functionallity of it has been
placed into the code. eg Search and Replace

James

--
--------------------------
Mobile: +44 07779080838
http://www.stev.org
2:00pm up 1 day, 26 min, 3 users, load average: 0.01, 0.02, 0.20

Nov 14 '05 #5
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

john blackburn wrote:
| lasek wrote:
|
|
|>Thanks a lot for your explanation...b ut i need more info about how a
|>variable was handled into a macro...in particular someone wrote that
|>free()..is a macro or function i don't remember so which difference
|>between a free() macro and a free() function ??....
|>remember ny head is very strong :-)))
|
|
| free() is the companion function to malloc() and is definitely a
function.

Maybe, maybe not. It's up to the implementation.

| Look up the library documentation; malloc is used to request memory space
| and initialize a pointer to it and free() is used to release that memory
| space.

free() could be a wrapper around a function that programmers aren't
supposed to call directly, or it might not use a function at all, just
odd compiler magic.

|
| macros only exist in the text pre-processing stage of compilation during
| which they are expanded into code whereas functions are items which exist
| throughout the whole process of compilation, link and execution.

Again, maybe, maybe not. If a function is always inlined, it's not going
to exist through the linking or execution phases.

|
| I agree with Joona, the best way forward for you is to read a C text book;
| you will learn a lot more about this than from ad-hoc queries about it in
| this newsgroup.

This is the only part of your answer I agree with unreservedly.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFBd7JxKxa tjOtX+j0RAgEnAJ 9/l+LENz6NiIpg+ga 6QPmsYAcidgCcDm kN
Qr45VSpY+7rmlJ8 dhAKoHhw=
=hawe
-----END PGP SIGNATURE-----
Nov 14 '05 #6
Chris Barts wrote:
Again, maybe, maybe not. If a function is always inlined, it's not going
to exist through the linking or execution phases.


An interesting point; does that mean that a source debugger will not be able
to trace calls to that function ?
Nov 14 '05 #7
Chris Barts wrote:
john blackburn wrote:
|
| free() is the companion function to malloc() and is definitely a
function.

Maybe, maybe not. It's up to the implementation.

| Look up the library documentation; malloc is used to request memory space
| and initialize a pointer to it and free() is used to release that memory
| space.

free() could be a wrapper around a function that programmers aren't
supposed to call directly, or it might not use a function at all, just
odd compiler magic.


free() is always a function, in any conforming
implementation. free() may *also* be provided as a
macro, at the implementation' s discretion, but it must
in any case exist as a function. The same is true of
all the other Standard library functions (other than
those "functions" that are specifically described as
macros, of course).

The following program must compile and run:

#include <stdio.h>
#include <stdlib.h>

static void do_it( void (*func)(void*), void *arg) {
func(arg);
}

static void not_free(void *arg) {
printf ("not_free: arg = %p\n", arg);
}

int main(void) {
void *ptr = malloc(42);
do_it (not_free, ptr);
do_it (free, ptr);
return 0;
}

This example is both contrived and pointless, but
it's not too hard to come up with a scenario in which
using a pointer to free() makes sense. At the risk of
straying from topicality, imagine a suite of programs
that share a single region of memory. The ordinary
malloc() and free() functions won't manipulate the
shared region, so you might well write shared_malloc()
and shared_free() with identical signatures and similar
semantics. Now, let's suppose you want to write a handy
function to release a linked list which might reside in
either shared or ordinary memory:

void liberate_list(N ode *list, void (*liberator)(vo id*)) {
Node *head;
while ((head = list) != NULL) {
list = head->next;
liberator (list);
}
}

Assuming that you know (somehow) whether the list is in
ordinary or in shared memory, you could write

if (in_ordinary_me mory)
liberate_list (list, free);
else
liberate_list (list, shared_free);

.... and the C Standard requires that the pointer to the
function free() work as expected.

--
Er*********@sun .com

Nov 14 '05 #8
john blackburn <jo************ *********@linto nhealy.co.uk> wrote in message news:<41******* *************** *@news.zen.co.u k>...
Chris Barts wrote:
Again, maybe, maybe not. If a function is always inlined, it's not going
to exist through the linking or execution phases.


An interesting point; does that mean that a source debugger will not be able
to trace calls to that function ?

In practice it depends on how clever the compiler and debugger are
with regards to inline functions. I've seen debuggers that can step
through an inlined function as well as any other, and I've seen
debuggers that can't see anything but a single statement.
Nov 14 '05 #9

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

Similar topics

6
25497
by: Alexander Malkis | last post by:
Why do programmers like to use NDEBUG instead of DEBUG? -- Best regards, Alex. PS. To email me, remove "loeschedies" from the email address given.
2
5726
by: Aakash Jain | last post by:
Can someone explain me the difference between the following C++ macro and the function MAX: #define MAX(a, b) (((a) > (b)) ? (a) : (b)) template <class T> const T& MAX(const T& a, const T& b) { return (a > b ? a : b); }
7
1963
by: sachin_mzn | last post by:
Hi, It may be a silly question but I want to know the difference between #define macro and inline functions Is there any performance issue related to it. -Sachin
7
23536
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead. Execution is faster. Where will it be stotred?(Is it in bss/stack/?) FUNCTION:
23
1903
by: yezi | last post by:
Hi, all: The 1st sendtence: int main(){ char string={""}; string = {" connected "}; ..... }
11
22345
by: San | last post by:
hi there, I am new to c++ and tryig to learn the basics of the c++ concepts. While I was reading the templates, I realize that the templates are a syntax that the compilar expands pased upon the type specified. This is much similar like a macro expansion in C. can anyone please explain advantages of one over the other ? Thanks in advance -
21
2304
by: MAx | last post by:
Hi, Could any one list possible number of diferences between a function and a macro? which one will be faster and why?? ex : function and a macro ti find max of two nums #define MAX(a,b) (a>b) ? a:b and int max(int a,int b)
6
2300
by: jason | last post by:
Hi, I learned my lesson about passing pointers, but now I have a question about macros. Why does the function work and the MACRO which is doing the same thing on the surface, does not work in the following small example ? #include <stdio.h>
11
5130
by: sunnyalways4u2000 | last post by:
hello sir, Sir will please tell me the exact difference between C and advanced C...what are the extra features or funcions...etc added in this advanced one.
0
8637
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8364
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5574
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4197
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2625
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.