|
Can please tell me that what really internally happens when we use a macro.
I want to ask you that what is the difference in using macro and not using it
...................
|
|
|
|
|
A preprocessor directive (some folks refer to it as macro but I feel that is incorrect) is removed by the preprocessor thus the compiler does not even see it. You can see this for yourself by using the /P or /E compiler switch.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
The pre-processor (NOT the compiler) will parse the "#" and remove code that does not match the values...
for example :
#ifdef DEBUG
DoSomething();
#else
DoSomethingElse();
#endif
if that code is pre-processed and the value DEBUG (see pre-processor definitions in the project settings) is defined, the preprocessor will generate the following code :
DoSomething();
if pre-processed in RELEASE, (DEBUG is not defined) :
DoSomethingElse();
and only that code will be compiled.
if you have
if (1)
{
DoSomething();
}
else
{
DoSomethingElse();
}
There is no macro, so the pre-processor will do nothing the that whole code will compiled by the compiler; as someone else wrote, in a simple compiler, generated assembly will be generated for both the if and the else ; so there will be code bloat in the final executable, but in modern compilers, if code will never be called, it will be removed, and in that case, only the DoSomething(); will be effectively compiled into the executable.
This signature was proudly tested on animals.
|
|
|
|
|
Hi,
#Ifdef 1
cout<<"Hello World"<<endl;
#else
cout<<"Have A Nice Day"<<endl;
#endif
The compiler preprocessor will explicitly skip the code within the #else and no code will be generated because the previous condition was true.
if(1)
cout<<"Hello World"<<endl;
else
cout<<"Have A Nice Day"<<endl;
A non-optimizing compiler may generate code for both conditions including the impossible condition. An optimizing compiler will detect the second condition is unreachable and skip code generation.
Essentially you will probably get exactly the same result in both cases in a modern optimizing compiler in a Release build.
#if defined RCAD
#else
#endif
deadlyabbas wrote: So what is the impact on our code and the size of it we doing the same thing.
The outcome will probably be exactly the same. By defining RCAD condition as true... the compiler ignores the other condition completely.
Best Wishes,
-David Delaune
|
|
|
|
|
deadlyabbas wrote: Example:
#Ifdef 1
cout<<"Hello World"<<endl;
#else
cout<<"have a="" nice="" day"<<endl;
#endif<="" blockquote="">
That's conditional compilation (compiler doesn't even 'see' the "Nice Day" statement, the preprocessor removes it).
deadlyabbas wrote: if(1)
cout<<"Hello World"<<endl;
else
cout<<"have a="" nice="" day"<<endl;<="" blockquote="">
That's runtime evaluation of statements.
Hint: compile with /E flag to see the preprocessor in action.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Hello Sir,
How to read Byte array From image ?
in C# i can read perfect but in VC++ i dont know how to do it ?
Thanks !
|
|
|
|
|
raju_Code wrote: How to read Byte array From image ?
Do you mean from a GDI+ Image[^]?
Best Wishes,
-David Delaune
|
|
|
|
|
hi.
i want to write an image processing program and i don't need to preview the image so i decided to use win32 console application to avoid any unwanted delays. i want to use GDI+ classes but there is a runtime error.
my program :
#include "stdafx.h"
#include <windows.h>;
#include <gdiplus.h>;
using namespace Gdiplus;
int main()
{
Bitmap *bmp;
bmp = new Bitmap(L"my image path");
unsigned int w = bmp->GetWidth();
return 0;
}
the error :
Unhandled exception at 0x00411761 in GdiPlusTest.exe: 0xC0000005: Access violation reading location 0x00000004.
any help? thanks.
|
|
|
|
|
__erfan__ wrote: i want to write an image processing program and i don't need to preview the image so i decided to use win32 console application to avoid any unwanted delays.
Hey, you're in the un-managed world here.
Probably the bitmap object it is incorrectly built because you've not initialized, as required, the GDI+ library (MSDN [^]):
You must call GdiplusStartup before you create any GDI+ objects, and you must delete all of your GDI+ objects (or have them go out of scope) before you call GdiplusShutdown.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
ops... A photo finish. and you won
|
|
|
|
|
I'm pretty sure he has borrowed some of Rajesh's monkeys. He only slept 4 hours last night. He is training them to play chess.
Best Wishes,
-David Delaune
|
|
|
|
|
|
Randor wrote: I'm pretty sure he has borrowed some of Rajesh's monkeys
Randor wrote: He is training them to play chess.
I actually was persuading one of them (the most appealing female of the group) to have some ...
(well I suppose it's not sister kid safe)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
|
__erfan__ wrote: Unhandled exception at 0x00411761 in GdiPlusTest.exe: 0xC0000005: Access violation reading location
You have to initialize GDI+ before you make any GDI+ calls. check GdiplusStartup()[^] function
|
|
|
|
|
|
|
Looks like the missing initialization didn't pass unnoticed...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Hi,
Please mark the correct answers(s) that helped you by clicking "Good Answer". I've done it for you now.
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Hi,
i would like to know the best way to convert a char (e.g 'c') to its hex string representation (i.e 63) in c++ and save it to a file as such.
|
|
|
|
|
fprintf(fp, "%02X ", 'c');
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Thank you, but is there also a way to do it with ofstream or ostringstream?
So many streams are available, so confusing
|
|
|
|
|
Naturellement!
#include <fstream>
void main()
{
std::ofstream os;
os.open("foo.txt");
os.unsetf(std::ios_base::dec);
os.setf(std::ios_base::hex);
os << (int) 'c' << std::endl;
os.close();
}
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Thanks again, another problem came up though. The character has reinterpret_cast<char*> in front of it and then the address of the vector element. The reinterpret_cast should to my opininon put the char in correct format, but it wont come out right
|
|
|
|
|
Could you please post sample code?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|