Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9021215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:12:12+00:00 2026-06-16T05:12:12+00:00

#define FREE1(x) do { free(x); x = NULL; } while (0); #define FREE2(x) {

  • 0
#define FREE1(x) do { free(x); x = NULL; } while (0);
#define FREE2(x) { free(x); x = NULL; }

What is the difference between these macros?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:12:13+00:00Added an answer on June 16, 2026 at 5:12 am

    If your question is about the do/while trick in macros, there’s no difference, because you apparently made an error in the first macro definition, which completely defeated the purpose of the trick. You put the ; after the whole (0). The proper implementation should absolutely not have the ; after the while (0) in the first macro. This is the whole point of the do/while trick.

    Now this code will not compile

    if (condition)
      FREE2(arg);
    else
      /* something else */;
    

    This code will not compile either

    do 
      FREE2(arg);
    while (condition);
    

    The point of the do/while technique is to make this code compile. But it will not compile with your FREE1 macro either because of the aforementioned error.

    However, if you define the first macro correctly

    #define FREE1(x) do { free(x); x = NULL; } while (0)
    // No `;` at the end!!!
    

    then the first macro will work perfectly fine in the above code samples. This is actually the reason people use the do/while technique in multi-statement macros – to make it work correctly and uniformly with ordinary functions in such contexts.

    P.S. As a side note, the purpose of all such techniques is to turn a group of multiple statements into one compound statement. In your specific case the free(x); x = NULL; sequence can be re-implemented as a single expression (free(x), x = NULL), which eliminates the need for any multi-statement macro techniques, i.e. this

    #define FREE3(x) (free(x), x = NULL)
    

    will work as well. But it is a matter of personal preference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I define a 'block' of text as all lines between start of file, newline
Possible Duplicates: Why are there sometimes meaningless do/while and if/else statements in C/C++ macros?
I have these two files: test.h #ifndef TEST_H #define TEST_H typedef struct _vertex_node {
Possible Duplicate: Do-While and if-else statements in C/C++ macros I have the following macros
#define BUF_SIZE 10 char *html = foo:baa\r\nxxx:yyyy:\r\nLocation:........................................\r\Connection:close\r\n\r\n; char *p = (char*)html, *buf, *pbuf, *tbuf;
(define (lcs lst1 lst2) (define (except-last-pair list) (if (pair? (cdr list)) (cons (car list)
#define MAX 100 struct bs{ int ab; int ac; }be; struct s{ be b;
#define HISTORY_SIZE 50 #define INPUT_SIZE 512 /*Max input size*/ char input[INPUT_SIZE]; /*Holding user input
#define __HAVE_ARCH_STRCPY What's the meaning of __HAVE_ARCH ? I'm not a native speaker and
I define a tabs using JQuery <div id=tabs> <ul> <li><a href=#tabs-1>Tab 1</a></li> <li><a href=#tabs-2>Tab

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.