Showing tag results for Code

Feb 23, 2004
Post comments count0
Post likes count0

Invalid thread and process IDs

Raymond Chen

Perhaps you want a value to use as a sentinel, which you want to be guaranteed is never a valid thread ID or process ID. What values can you use? Nothing is explicitly written about this topic, but you can put on your logic cap and figure it out. If you need an invalid thread ID, you can use zero. How do you know that zero is an invalid thr...

Code
Feb 17, 2004
Post comments count0
Post likes count2

GetDialogBaseUnits is a crock

Raymond Chen

Each dialog can have different base units, so it's meaningless to have a global converter.

Code
Feb 10, 2004
Post comments count0
Post likes count0

Answer to exercise: Pointer to member function cast

Raymond Chen

Yesterday's exercise asked you to predict and explain the codegen for the following fragment: Well, the codegen might go something like this: Let's use one of our fancy pictures: Just for fun, I swapped the order of Base1 and Base2. There is no requirement in the standard about the order in which storage is allocated for base classes, so ...

Code
Feb 9, 2004
Post comments count0
Post likes count3

Pointers to member functions are very strange animals

Raymond Chen

Pointers to member functions are very strange animals. Warning: The discussion that follows is specific to the way pointers to member functions are implemented by the Microsoft Visual C++ compiler. Other compilers may do things differently. Well, okay, if you only use single inheritance, then pointers to member functions are just a pointer to ...

Code
Feb 4, 2004
Post comments count0
Post likes count0

Answers to exercises – mismatching new/delete

Raymond Chen

Answers to yesterday's exercises: What happens if you allocate with scalar "new" and free with vector "delete[]"? The scalar "new" will allocate a single object with no hidden counter. The vector "delete[]" will look for the hidden counter, which isn't there, so it will either crash (accessing nonexistent memory) or grab a random number and a...

Code
Feb 3, 2004
Post comments count0
Post likes count0

Mismatching scalar and vector new and delete

Raymond Chen

In a previous entry I alluded to the problems that can occur if you mismatch scalar "new" with vector "delete[]" or vice versa. There is a nice description of C++ memory management in C++ Gotchas: Avoiding Common Problems in Coding and Design on www.informit.com, and I encourage you to read at least the section titled Failure to Distinguish Sca...

Code
Jan 29, 2004
Post comments count0
Post likes count1

Integer overflow in the new[] operator

Raymond Chen

Integer overflows are becoming a new security attack vector. Mike Howard's article discusses some of the ways you can protect yourself against integer overflow attacks. One attack vector he neglects to mention is integer overflow in the new[] operator. This operator performs an implicit multiplication that is unchecked: If you study the code g...

Code