Click here to Skip to main content
15,901,035 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How can program tell that itself is running as admin? Pin
Brandon-X120001-Oct-11 5:38
Brandon-X120001-Oct-11 5:38 
QuestionDatum & Map projection Pin
rajgaurav_jsr1-Oct-11 3:01
rajgaurav_jsr1-Oct-11 3:01 
AnswerRe: Datum & Map projection Pin
Software_Developer1-Oct-11 21:58
Software_Developer1-Oct-11 21:58 
QuestionHow to update the Document generated by server COleServerDoc Pin
Andraw Tang30-Sep-11 10:01
Andraw Tang30-Sep-11 10:01 
QuestionHow can I Move a memory pointer in FILE? Pin
002comp30-Sep-11 2:52
002comp30-Sep-11 2:52 
AnswerRe: How can I Move a memory pointer in FILE? Pin
Code-o-mat30-Sep-11 3:04
Code-o-mat30-Sep-11 3:04 
AnswerRe: How can I Move a memory pointer in FILE? Pin
Madhu Nair30-Sep-11 3:29
Madhu Nair30-Sep-11 3:29 
AnswerRe: How can I Move a memory pointer in FILE? PinPopular
enhzflep30-Sep-11 3:34
enhzflep30-Sep-11 3:34 
I'd be inclined to load them individually.

I.e

B *obj = new B();
fread(&obj->a, sizeof(obj->a), 1, stream);
fread(&obj->b, sizeof(obj->b), 1, stream);


Please note also: the compiler may pad your structures where it sees fit, unless prevented via a compiler directive. I'd assume it also applied for classes, though do not know.

The implication is that if you declare a struct thusly:

struct simpleStruct{
 char memberA;
 long memberB;
}


You can often find that doing sizeof on each of the members then adding them together will give you a different result to sizeof theWholeStructure.
I.e, in my above example, it's quite possible that - sizeof(char) + sizeof(long) != sizeof(simpleStruct).

You may find that when you expect your structure to be 5 bytes as in the above example, that the compiler will pad it to 6 or 8 bytes. This means that when you do an fwrite using &simpleStuctInstance, you may in fact be adding 6 or 8 bytes to the file, although there are only 5 bytes of useful information. If you then do a fread using &simpleStructInstance, the extra bytes of padding will be magically of no consequence. However, if you fwrite the individual members out individually, then go onto fread the whole structure in one go, you'll be reading 6 or 8 bytes, discarding 1 or 3 of them and ending up with a struct that contains garbage.

This issue often arises when writing roll-your-own code for bitmap handling - failing to direct the compiler not to pad the structs results in all sorts of nasty behaviour.
GeneralRe: How can I Move a memory pointer in FILE? Pin
Chris Meech30-Sep-11 7:13
Chris Meech30-Sep-11 7:13 
AnswerRe: How can I Move a memory pointer in FILE? PinPopular
Richard MacCutchan30-Sep-11 10:10
mveRichard MacCutchan30-Sep-11 10:10 
QuestionSetting font size in popup message Pin
nashhashdash30-Sep-11 1:21
nashhashdash30-Sep-11 1:21 
AnswerRe: Setting font size in popup message Pin
Richard MacCutchan30-Sep-11 2:35
mveRichard MacCutchan30-Sep-11 2:35 
Question'Should I use <code>using namespace std;</code> in my code?' Pin
Stefan_Lang30-Sep-11 0:49
Stefan_Lang30-Sep-11 0:49 
AnswerRe: 'Should I use using namespace std; in my code?' Pin
Erudite_Eric30-Sep-11 0:56
Erudite_Eric30-Sep-11 0:56 
GeneralRe: 'Should I use using namespace std; in my code?' Pin
Stefan_Lang2-Oct-11 22:14
Stefan_Lang2-Oct-11 22:14 
GeneralRe: 'Should I use using namespace std; in my code?' Pin
Erudite_Eric3-Oct-11 22:00
Erudite_Eric3-Oct-11 22:00 
AnswerRe: 'Should I use using namespace std; in my code?' Pin
Niklas L30-Sep-11 1:13
Niklas L30-Sep-11 1:13 
GeneralRe: 'Should I use using namespace std; in my code?' Pin
Stefan_Lang30-Sep-11 1:50
Stefan_Lang30-Sep-11 1:50 
GeneralRe: 'Should I use using namespace std; in my code?' Pin
Niklas L30-Sep-11 2:28
Niklas L30-Sep-11 2:28 
GeneralRe: 'Should I use using namespace std; in my code?' Pin
Albert Holguin30-Sep-11 8:25
professionalAlbert Holguin30-Sep-11 8:25 
GeneralRe: 'Should I use using namespace std; in my code?' Pin
CPallini30-Sep-11 22:13
mveCPallini30-Sep-11 22:13 
GeneralRe: 'Should I use using namespace std; in my code?' Pin
Stefan_Lang2-Oct-11 22:17
Stefan_Lang2-Oct-11 22:17 
GeneralRe: 'Should I use using namespace std; in my code?' Pin
CPallini2-Oct-11 22:35
mveCPallini2-Oct-11 22:35 
GeneralRe: 'Should I use using namespace std; in my code?' Pin
Stefan_Lang3-Oct-11 0:42
Stefan_Lang3-Oct-11 0:42 
AnswerRe: 'Should I use using namespace std; in my code?' Pin
Chris Losinger30-Sep-11 7:47
professionalChris Losinger30-Sep-11 7:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.