|
This is mybe a stupid question , but in the following piece of code
#define YOP 4*3+2
int i=YOP+3;
What will be compiled ? int i=4*3+2+3 ? or int i=17; ?
The question is, if I compile this, will 4*3+2 be done every time the part of code containing this is called, or is it done once by the (pre)compiler and coded as 17 ?
~RaGE();
|
|
|
|
|
Um... the macro will be expanded in place, but you'd hope the compiler will work out they are all constants and do the math for you. So maybe you'll get 17, but all you can hope for is 4*3+2+3.
Macros are evil anyhow. If it's a constant, use a const int instead.
Christian
No offense, but I don't really want to encourage the creation of another VB developer.
- Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael
P Butler 05-12-2002
It'd probably be fairly easy to make a bot that'd post random stupid VB questions, and nobody would probably ever notice - benjymous - 21-Jan-2003
|
|
|
|
|
Christian Graus wrote:
Macros are evil anyhow.
No they are not. I love macros.
- Anders
Money talks, but all mine ever says is "Goodbye!"
|
|
|
|
|
Well, you're entitled to your opinion. I'm not even going to mention that Stroustrup spends two pages of his book warning people that macros are generally evidence of a bad design or a bad programmer, because I think that's a little over harsh. But I agree that they should be used very sparingly. You simply cannot read your code and see exactly what is going on, not to mention when you try to debug it.
Christian
No offense, but I don't really want to encourage the creation of another VB developer.
- Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael
P Butler 05-12-2002
It'd probably be fairly easy to make a bot that'd post random stupid VB questions, and nobody would probably ever notice - benjymous - 21-Jan-2003
|
|
|
|
|
I guess it's because I'm more a C than C++ guy
I do use C++, templates, classes and that stuff, but I never use STL.
I have used std::string a bit, but it's just too slow compared to char* :P
So.... What Stroustrup thinks about macros don't make me think much different about it...
- Anders
Money talks, but all mine ever says is "Goodbye!"
|
|
|
|
|
Anders Molin wrote:
I guess it's because I'm more a C than C++ guy
Well, I guess bad habits die hard. If you're used to the pain of macros, then I guess there's no real reason to stop using them.
Christian
No offense, but I don't really want to encourage the creation of another VB developer.
- Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael
P Butler 05-12-2002
It'd probably be fairly easy to make a bot that'd post random stupid VB questions, and nobody would probably ever notice - benjymous - 21-Jan-2003
|
|
|
|
|
So maybe you'll get 17, but all you can hope for is 4*3+2+3.
I think this is not true. These expressions are called constant expressions and it is guaranteed that the compiler will precalculate their value. Example follows:
const int size=4*3+2+3;
char buffer[size]; So, in this case we can hope for the best
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Joaquín M López Muñoz wrote:
So, in this case we can hope for the best
Is it really ? I did not know that the standard *guarenteed* this behaviour.
Christian
No offense, but I don't really want to encourage the creation of another VB developer.
- Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael
P Butler 05-12-2002
It'd probably be fairly easy to make a bot that'd post random stupid VB questions, and nobody would probably ever notice - benjymous - 21-Jan-2003
|
|
|
|
|
Well, if one gets really anal about it, the standard actually does not guarantee that the expression does not get executed at run time. One can always write en evil compiler that does the calculation at run-time just for fun and then throws the value away.
What my example shows is that any real compiler has to calculate the value of constant expressions.
PS: Even this is not entirely true if one imagines a pyschedelic compiler that does not reserve static memory for fixed sized arrays and prefers to do it at run-time. Or also, a conforming compiler can just translate to a high-level language and avoid calculating any constant expression at all.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
OK, so what I said is right - you might reasonably hope that the compiler will evaluate the constant, but there is no guarentee ?
Christian
No offense, but I don't really want to encourage the creation of another VB developer.
- Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael
P Butler 05-12-2002
It'd probably be fairly easy to make a bot that'd post random stupid VB questions, and nobody would probably ever notice - benjymous - 21-Jan-2003
|
|
|
|
|
You are utterly (en vogue word, ain't it) right, my dear.
In my defense I'd say that
int i=10000; reasonably gets calculated at compile-time, but there is no guarantee that the compiler does not generate the equivalent of
i=0;
for(int n=0;n<10000;++n){
Sleep(1000);
++i;
} just for fun.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
lol
Build a man a fire, and he will be warm for a day Light a man on fire, and he will be warm for the rest of his life!
|
|
|
|
|
I'm not 100% sure, but I think I remember reading that all harcoded expression, like yours, are resolved at compilation.
So YOP+3 would equate to (4*3)+2+3 = 17.
This result would then be used thoughout your code.
|
|
|
|
|
The preprocessor (which handles #define 's) simply performs string substitution. It does not do any expression evaluation. The end result is that int i=4*3+2+3 is what is passed to the compiler. The compiler will evaluate the statement as i=17 .
This is why it is so important to parenthesize macro arguments, along with the macro itself. Since the #define is just a string substitution, it can have problems based on operator precedence. For example, suppose you have:
#define macro(a,b) a / b If you use this macro like this:
int x = macro(4,2); the compiler actually parses and generates code for:
int x = 4 / 2; But, if you use it like this:
int y = macro(4,1 + 1); the compiler actually parses it as:
int y = 4 / 1 + 1; which has a different result from what you expect. Similarly, if you use the macro like this:
int z = macro(4,2) * 3; the compiler parse it as
int z = 4 / 2 * 3; Given operator precedence, the compiler will generate code for
int z = 6; Using the macro as follows:
int z = macro(4,1 + 1) * 3; causes the compiler to parse:
int z = 4 / 1 + 1 * 3; which generates code for
int z = 7;
Software Zen: delete this;
|
|
|
|
|
If you have a compiler smart enough to do "constant folding", then yes, you'll end up with int i = 17 . Constant folding is one of those compiler optimizations that is literally so easy, that every compiler you will find out there will do it. Even most interpreters will do it.
The other replies have given a good enough explanation of the preprocessing stuff going on here, so I'll not explain that part of it.
Chris Richardson
C/C++ Include Finder[^]
|
|
|
|
|
:-DHow can I obtain SID -> security Identifier ie.
HKEY_USERS\S-1-5-21-905137394-830476157-270368766-1458
of the current logged in user
|
|
|
|
|
From a bit of spelunking, you should be able to use
LookupAccountName to get a SID of a user, and
ConvertSidToStringSid to convert it to the
text format you are after.
Good luck!
Iain.
ps. You might want GetUserName to get the
current user name.
|
|
|
|
|
Hi,
I am working on windows XP machine, Visual Studio 6.0(SP5),
I am trying to draw caption using the following function :
::DrawCaption(m_hWnd, *pDC,&rcCaption,DC_ACTIVE|DC_TEXT|DC_GRADIENT);
Studio reports the error that DC_GRADIENT is undeclared identifier.
If I go to the source of where this macro is declared, I see a conditional declaration that if WINVER is >= 0x500 only then this could be compiled.
If I look for the source of WINVER, it is 0x400 in some files(winRes.h and Windows.h), and 0x500 in other files(WinRef, WinUsr, WinGDI & WinDef).
Now I could not understand, that why is it defined to 0x400, on my XP machine ?? is it a issue with some Service pack ??
Could some body please throw some light on this ?
Regards,
Abhishek Narula
|
|
|
|
|
The only thing that matters is what you define for WINVER. If you assign a value of 0x500, all that's meant to work under 5.0 and higher will get compiled without errors. In the corresponding files, there're some ifdefs which introduce some identifiers if your version passes the comparison, otherwise not. The standard WINVER is 400 I think.
|
|
|
|
|
The best ist to define this value for yourself and NOT to set WINVER = 0x500 because there may arise problems if the programs isnt running under XP.
Try this @ home. (B&B)
|
|
|
|
|
i need to access zip and jar file directly by c++.
in someway, i am doing an unzip and unjar program.
in the moment, i only can do:
zip file:
read all structures with right info (OK)
problem: for code part, i don't know its format, so i can't translate it to oringinal file.
jar file:
none (only few info from google).
who knows useful links and books about their format - espacially about code part?
thx for any help.
includeh10
|
|
|
|
|
|
Have you tried ZipArchive at
http://www.artpol-software.com/
?
There's a free library there that allows you to read and write ZIP files that's very easy to use, I don't know if that's any use to you.
Cheers,
KB
|
|
|
|
|
Have a look at zziplib. If you want to, you can even replace plain fopen with zzip_fopen and read the file like it was a plan C stream.
As for jar file, it completely depends if you're talking about the .jar extension that Sun "stole", or the more widely spread and later hijacked JAR archiver (the successor to ARJ). If the former, it's nothing more than a renamed .zip-file. For the latter, check the ARJ/JAR site and see if there is a free unpack lib.
++luck;
|
|
|
|
|
hi, thx u 3!
i need to access sun's java jar file first, than more...
what does "stole" mean here? is there any special?
includeh10
|
|
|
|