|
i would like to copy the contents of a byte array to a long variable. my code looks like this:
long key = 0;
for (i = 0; i < 8; i++) {
key <<= 8;
key |= (long)(buffer[i]); //<- this line causes the problem
}
however, when i run the program, there is an error message saying "unable to handle exception". what may have cause this problem?
|
|
|
|
|
You could also copy the byte array into your long directly:
long key = 0;
memcpy(buffer,&key,4);
In that case, you need to take care of the byte ordering in your byte array.
modified on Monday, February 11, 2008 4:36 AM
|
|
|
|
|
How is declared buffer?
BTW long data type is only 4 bytes wide.
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
|
|
|
|
|
CPallini wrote: BTW long data type is only 4 bytes wide.
And I didn't even noticed that . This is a slow monday morning...
|
|
|
|
|
Hello everyone,
I think in practical experience, extern is useful only when we have more than one compile unit (cpp file) and making cross-reference between compile units.
In the MSDN extern sample,
http://msdn2.microsoft.com/en-us/library/0603949d.aspx
It only uses one source file (compile unit) to demonstrate the usage of extern, is it correct and practical?
thanks in advance,
George
|
|
|
|
|
It only shows the its usage. U'rs thinking is absolutely correct.
Come online at:-
jubinc@skype
|
|
|
|
|
Hi Don,
I also think it is correct. But do we achieve special benefits if we use extern in the same compile unit?
regards,
George
|
|
|
|
|
No. The reason for extern is to differentiate between a declaration and a definition:
int num; // Is this a definition or a declaration?
In this case the compiler assumes it’s a definition. If you want to share num between two compilation units this will not do the trick and will result in a linker error – there will be two variables with the same name.
extern int num; // This is a declaration.
Now this referrers to a “num” defined elsewhere.
Steve
|
|
|
|
|
Thanks Steve,
In the sample, even if you do not use why we need to write statement, extern int i? i is global and we can use it anywhere in the same compile unit (cpp file).
regards,
George
|
|
|
|
|
As I said, extern is used to indicate that a construct is a declaration and not a definition. With functions no such mechanism is needed: if the function has a body it’s a definition and if not it’s a declaration:
int void FooBar();
int void FooBar()
{
}
If you want to use the “FooBar” function in another compilation unit (from where it’s defined) you need to make sure to include its declaration. This is usually done by including a header file. It’s an error to define the same function twice in two separate compilation units however and attempting to do so will result in a link error.
Since variables don’t have bodies this technique isn’t usable, thus the extern keyword:
int FooBar;
extern int FooBar;
Steve
|
|
|
|
|
Thanks Steve,
How about this,
extern int i = 100;
a definition or declaration, why?
regards,
George
|
|
|
|
|
From section 3.1.2 of the C++ standard:
“A declaration is a definition unless it declares a function without specifying the function’s body (8.4), it contains the extern specifier (7.1.1) or a linkage-specification (7.5) and neither an initializer nor a function-body, it declares a static data member in a class declaration (9.4), it is a class name declaration (9.1), or it is a typedef declaration (7.1.3), a using-declaration (7.3.3), or a using-directive (7.3.4).”
Steve
|
|
|
|
|
Thanks Steve,
1.
So, extern int i = 100;
should be declaration and matches the rule, " it contains the extern specifier (7.1.1)", right?
2.
What is " linkage-specification (7.5)"?
regards,
George
|
|
|
|
|
George_George wrote: 1.
So, extern int i = 100;
should be declaration and matches the rule, " it contains the extern specifier (7.1.1)", right?
No, because of the "and neither an initializer nor a function-body" clause; the example you gave does have an initializer.
extern int i;
extern int i = 100;
George_George wrote: 2.
What is " linkage-specification (7.5)"?
extern "C" is an example.
Steve
|
|
|
|
|
Thanks Stephen,
Any special advantage/restrictions/functions we could have when writing extern int i = 3 other than int i = 3?
(I think you mean extern int i = 3 has the same meaning as int i = 3, which is definition with initialization. right?)
regards,
George
|
|
|
|
|
There is no reason to do so (use extern ) in such cases. Doing so does neither harm nor good; the two constructs are equivalent. I wouldn’t in cases like that however as it could lead to confusion.
Steve
|
|
|
|
|
Thanks Steve,
My question is answered.
regards,
George
|
|
|
|
|
I think this[^] article is helpful for you.
|
|
|
|
|
Thanks Hamid,
But it does not cover the case when using extern to qualify a variable which is defined in the same compile unit.
regards,
George
|
|
|
|
|
Doing so is poinless and possibly misleading, but harmless.
Steve
|
|
|
|
|
How do I disable a toolbar button?
Also I'd like to know how to disable some buttons in my context menu?
Please make an example for me.
Thanks!!!
|
|
|
|
|
If you're using raw toolbars then the TB_ENABLEBUTTON is probably what you're after. If you using MFC I believe the "MFC magic" should take care of this for you.
Steve
|
|
|
|
|
Check out ON_UPDATE_COMMAND_UI() .
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I am not sure if this is the place to ask this question.
I have been working on learning C++ for about 2 years now.
A year ago i got tired of it all , so i made a comprimise and went to visual C++ 6.0 (didnt want to go to that because i wanted the most efficiancy i could get) and recently bought Visual Studio 2005. I have bought so many books and read so many of them. They all leave me with a gap. I learn how to do the simple things , but not teaching me where to go next. I can make text based console applications till the cows come home , I am very good at using most C++ features , Classes , pointers , not the best at them , but i still know enough to get by.
BUT the books , articles , programs , what ever i use never tell me how to take the next step to make "real" program that has a GUI.
I have even went to using the Windows Forms to design GUI , but have discovered even that didnt take me far enough.
None of my books have taught me how to use libraries , or antyhing like that. I was told i had to use these to get somewhere , but no idea where to go.
I want to make programs to help people , remember meds , manage health problems.
I want to make programs to plan gardens , where you have like a graph background and can drag and drop pictures of plants.
I want to make a jigsaw puzzle game.....
This stuff isnt rocket science i know , i just dont know the steps to go to learn the extra stuff so i can make windows programs GUI that i can have all of this stuff in.
Does any one have any ideas where i can go from here. Books i can read , sugestions i can take , articles to read.
I am great at making programs in PHP , because i can use the HTML as the gui.
I want this so bad , to be able to make programs , and am willing to work for it , i just need to have an idea where the path is i need to go down.
Thank you very much for reading this.
And thank you even more if you reply LOL.
Nic
|
|
|
|
|
dont give up - there's a whole host of ways to go from where you are - there's GDI, GDI+, OpenGL, glut and a host of libraries people have written for games etc that use advanced graphics
.. let google be your friend !!!
'g'
|
|
|
|