CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,773

    Does #define change things that come before it?

    Does #define change things that came before it? I'd always assumed it did because it's a preprocessor directive but I just found this code in a header file:-

    Code:
    typedef struct _stat GStructLarge;
    
    static inline int
    pbd_g_stat(const gchar *filename, GStructLarge *bufLarge)
    {
        GStructSmall bufSmall;
        int ret = g_stat (filename, &bufSmall);
    
        if (0 == ret) {
            // Copy the various fields....
            bufLarge->st_dev   = bufSmall.st_dev;
            bufLarge->st_ino   = bufSmall.st_ino;
            bufLarge->st_mode  = bufSmall.st_mode;
            bufLarge->st_nlink = bufSmall.st_nlink;
            bufLarge->st_uid   = bufSmall.st_uid;
            bufLarge->st_gid   = bufSmall.st_gid;
            bufLarge->st_rdev  = bufSmall.st_rdev;
            bufLarge->st_size  = bufSmall.st_size;
            bufLarge->st_atime = bufSmall.st_atime;
            bufLarge->st_ctime = bufSmall.st_ctime;
            bufLarge->st_mtime = bufSmall.st_mtime;
        }
    
        return ret;
    }
    
    #define GStuctSmall GStructLarge
    Notice that GStructSmall is a predefined type using 'small' fields (e.g. 32-bit time_t) whereas GStructLarge is essentially the same, except that it uses larger fields - e.g. 64-bit time_t - however, the call to g_stat() needs the small version.

    So in the above code, which version will end up getting passed to g_stat()? The Small struct or the Large one?
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,084

    Re: Does #define change things that come before it?

    Quote Originally Posted by John E View Post
    Does #define change things that came before it? I'd always assumed it did because it's a preprocessor directive but I just found this code in a header file:-

    Code:
    typedef struct _stat GStructLarge;
    
    static inline int
    pbd_g_stat(const gchar *filename, GStructLarge *bufLarge)
    {
        GStructSmall bufSmall;
        int ret = g_stat (filename, &bufSmall);
    
        if (0 == ret) {
            // Copy the various fields....
            ...
        }
    
        return ret;
    }
    
    #define GStuctSmall GStructLarge
    ...
    You #defined GStuctSmall , not the GStructSmall
    Or was it just a typo?
    Victor Nijegorodov

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,773

    Re: Does #define change things that come before it?

    Sorry, that's just a typo...
    "A problem well stated is a problem half solved. - Charles F. Kettering

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,084

    Re: Does #define change things that come before it?

    OK, did you try to look at the "preprocessed C++ source code"?
    Sorry, but I never tried it, so can only refer to other similar discussions:
    https://developercommunity.visualstu...ce-code/392127
    https://newbedev.com/how-do-i-see-a-...-visual-studio
    Victor Nijegorodov

  5. #5
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,773

    Re: Does #define change things that come before it?

    Wow, I didn't even know preprocessed output was available. Thanks.

    Although I'd guess it might be different for different compilers!!
    "A problem well stated is a problem half solved. - Charles F. Kettering

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,537

    Re: Does #define change things that come before it?

    Does #define change things that came before it? I'd always assumed it did
    No. The pre-processing is a one pass over the source file to expand macro uses. A macro is only known from its point of definition. It's not a 2-pass with one pass obtaining the definitions and the 2nd to expand.

    which version will end up getting passed to g_stat()? The Small struct or the Large one?
    The small one. The code below the // Copy line is needed as small struct is passed to g_stat and the returned contents are used to initialise the large one. If the large one was passed to g_stat(), then according to " the call to g_stat() needs the small version", passing a large struct a) wouldn't compile and b) the copying from the small to the large wouldn't be needed.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.11.3)

  7. #7
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,773

    Re: Does #define change things that come before it?

    Quote Originally Posted by 2kaud View Post
    which version will end up getting passed to g_stat()? The Small struct or the Large one?
    The small one.
    Hmm... so what prevents it from being re-defined to GStructLarge? (apart from my typo !! )
    "A problem well stated is a problem half solved. - Charles F. Kettering

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,537

    Re: Does #define change things that come before it?

    The scope of the defined buff_Small is only within the function - before the #define
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.11.3)

  9. #9
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,773

    Re: Does #define change things that come before it?

    So presumably... #define isn't (strictly speaking) a preprocessor directive - because definitions can change within the scope of a file (so they'd need to get interpreted at compiler time??)
    "A problem well stated is a problem half solved. - Charles F. Kettering

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,537

    Re: Does #define change things that come before it?

    Definitions can be changed within a compilation unit. You can undefine a definition and re define it. The new definition takes effect from then until the end or until it is undefined or redefined.

    #define is a preprocessor directive. Along with all the others starting #. The preprocessor goes through the source file dealing with these. The expanded result is then passed to the compiler. The compiler knows nothing about these directives. That is the purpose of the preprocessor.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.11.3)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured