C++ define preprocessor

valbu17

I am learning C++ and right we are covering preprocessors but I am trying to solve a question from a quiz which I has confused me a bit or a lot.. I tried to worked out by my own before running the program.. and my output was..

System started...
Data at 2 is: 27 28 29 30
Data at 1 is: 23 24 25 26
The data is: 19

I checked the program in Xcode to see if my output is right but the right output is the next one:

System started...
Data at 1 is: 0 0 0 19
Data at 0 is: 7 0 0 0
The data is: 19 0 0 0

This is the code...

#include <iostream>

namespace test{
#define COMPILE_FAST
#define PRINT_SPLIT(v) std::cout << (int)*((char*)(v)) << ' ' << \
(int)*((char*)(v) + 1) << ' ' << (int)*((char*)(v) +2) << ' ' << \
(int)*((char*)(v) + 3) << std::endl

    typedef unsigned long long uint;
    namespace er{
        typedef unsigned int uint;
    }
    void debug(void* data, int size = 0){
        if(size==0){
            std::cout << "The data is: ";
            PRINT_SPLIT(data);
        } else {
             while(size--){
                std::cout << "Data at " << size << " is: ";
                char* a = (char*)data;
                PRINT_SPLIT((a + (4+size)));
            }
        }
    }
}// End of Test namespace...

int main(){
    test::uint a = 19;
    test::er::uint b[] = {256,7};
    std::cout << "System started..." << std::endl;
    test::debug(b,2);
    test::debug(&a);
    std::cout << "Test complete";
    return 0;
}

My big doubt or what I actually don't understand is whats going on here in this preprocessor because clearly for what I did its totally wrong...

#define PRINT_SPLIT(v) std::cout << (int)*((char*)(v)) << ' ' << \
(int)*((char*)(v) + 1) << ' ' << (int)*((char*)(v) +2) << ' ' << \
(int)*((char*)(v) + 3) << std::endl

if someone can be so nice and give me a brief explanation I will extremely appreciate it.

Adam Burry

The macro prints the values (as ints) of 4 consecutive bytes. It allows you to see how a 4 byte int is layed out in memory.

Memory contents, by byte, look like this (base10):

0x22abf0:       0       1       0       0       7       0       0       0
0x22abf8:       19      0       0       0       0       0       0       0
  • 0 1 0 0 is 256, i.e. b[0]
  • 7 0 0 0 is 7, i.e b[1]
  • 19 0 0 0 0 0 0 0 is 19, i.e. a

The sizeof(a) is different than the sizeof(b[0]) because there are 2 different typedefs for uint. Namely, test:uint and test::er::uint.

The address of a is greater than the address of b[] even though b is declared after a because the stack is growing downwards in memory.

Finally, I would say the output represents a defective program because the output would more reasonably be:

System started...
Data at 1 is: 7 0 0 0
Data at 0 is: 0 1 0 0
The data is: 19 0 0 0

To get that output the program needs to be changed as follows:

         while(size--){
            std::cout << "Data at " << size << " is: ";
            int* a = (int*)data;
            PRINT_SPLIT((a + (size)));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C recursive preprocessor define

From Dev

C preprocessor concatenation outside of #define

From Dev

C preprocessor: include based on define

From Dev

C Preprocessor: Dynamic #Define Creation

From Dev

universal preprocessor define for C or C++?

From Dev

How to define value for C# preprocessor symbol?

From Dev

When to use the preprocessor to define functions in C?

From Dev

C Preprocessor #define What type is the declared variable?

From Dev

How to define Preprocessor Directive Global in c#

From Dev

When to use the preprocessor to define functions in C?

From Dev

How to define value for C# preprocessor symbol?

From Dev

Overriding "endl" in C++ by #define preprocessor directive

From Dev

How #define preprocessor really works in C

From Dev

Define C# preprocessor from MsBuild

From Dev

Define multiple similar variables with C++ preprocessor

From Dev

When to use preprocessor directives to define functions in C++?

From Dev

C preprocessor: #define a macro that can be called without parentheses

From Dev

C-preprocessor #define SQR(x) ( x * x )

From Dev

How to define a set of configuration in any preprocessor directive form in C

From Dev

When to use preprocessor directives to define functions in C++?

From Dev

#define a global preprocessor directive in a separate file in Objective-C

From Dev

include file with preprocessor define

From Dev

if else define for preprocessor

From Dev

Preprocessor directive Impossible define or cancel defining the preprocessor

From Dev

Using Parentheses in Define Preprocessor Statements

From Dev

Preprocessor define functions expected an expression

From Dev

Preprocessor directive (define) not found by compiler

From Dev

How do I remove a C-style cast from a #define so I can use it in a preprocessor #if?

From Dev

What preprocessor define does -fopenmp provide?

Related Related

  1. 1

    C recursive preprocessor define

  2. 2

    C preprocessor concatenation outside of #define

  3. 3

    C preprocessor: include based on define

  4. 4

    C Preprocessor: Dynamic #Define Creation

  5. 5

    universal preprocessor define for C or C++?

  6. 6

    How to define value for C# preprocessor symbol?

  7. 7

    When to use the preprocessor to define functions in C?

  8. 8

    C Preprocessor #define What type is the declared variable?

  9. 9

    How to define Preprocessor Directive Global in c#

  10. 10

    When to use the preprocessor to define functions in C?

  11. 11

    How to define value for C# preprocessor symbol?

  12. 12

    Overriding "endl" in C++ by #define preprocessor directive

  13. 13

    How #define preprocessor really works in C

  14. 14

    Define C# preprocessor from MsBuild

  15. 15

    Define multiple similar variables with C++ preprocessor

  16. 16

    When to use preprocessor directives to define functions in C++?

  17. 17

    C preprocessor: #define a macro that can be called without parentheses

  18. 18

    C-preprocessor #define SQR(x) ( x * x )

  19. 19

    How to define a set of configuration in any preprocessor directive form in C

  20. 20

    When to use preprocessor directives to define functions in C++?

  21. 21

    #define a global preprocessor directive in a separate file in Objective-C

  22. 22

    include file with preprocessor define

  23. 23

    if else define for preprocessor

  24. 24

    Preprocessor directive Impossible define or cancel defining the preprocessor

  25. 25

    Using Parentheses in Define Preprocessor Statements

  26. 26

    Preprocessor define functions expected an expression

  27. 27

    Preprocessor directive (define) not found by compiler

  28. 28

    How do I remove a C-style cast from a #define so I can use it in a preprocessor #if?

  29. 29

    What preprocessor define does -fopenmp provide?

HotTag

Archive