c++

When should I allocate on the heap? (C++)

Hi, I don't really understand when I should be allocating memory on the heap and when I should allocate on the stack. All I really know is that allocating on the stack is faster, but since the stack is smaller I shouldn't use it to allocate large data structures; what other things should I take into account when deciding where to alloc...

Difference between angle bracket < > and double quotes " " while including header files in C++?

Possible Duplicate: what is the difference between #include <filename> and #include filename What is the difference between angle bracket < > and double quotes " " while including header files in C++? I mean which files are supposed to be included using eg: #include and which files are to be included using eg: #include "MyFi...

A better way to split a string into an array of strings in C/C++ using whitespace as a delimiter.

Sorry, my C/C++ is not that good, but the following existing code looks like garbage even to me. It also has a bug - fails when str = "07/02/2010" terminated by '\0' - . I think that instead of fixing a bug, it might as well be rewritten. In Python it is just 'kas\nhjkfh kjsdjkasf'.split(). I know this is C-ish code, but it cannot be tha...

Assuring proper maintenance of Clone() in C++

In C++, I find it very useful to add a "Clone()" method to classes that are part of hierarchies requiring (especially polymorphic) duplication with a signature like this: class Foo { public: virtual Foo* Clone() const; }; This may look pretty, but it is very prone to bugs introduced by class maintenance. Whenever anyone adds a data m...

Would making 'this' a reference rather than a pointer be better in retrospect?

Possible Duplicate: Why this is a pointer and not a reference? Is there any reason for this in C++ to be a pointer rather than a reference other than historical language decision? It feels a tad strange given that e.g. copy constructor or assignment operators both accept reference to "that", not a pointer. [I honestly couldn...

Link lists in C++ (pt. 2)

How come void del_begin() crashes when there's only one node left (I have other functions to add nodes)? #include <iostream> using namesspace std; node *start_ptr = NULL; node *current; int option = 0; void del_end() { node *temp, *temp2; temp = start_ptr; if (start_ptr == NULL) cout << "There are no nod...

Mediator pattern vs Publish/Subscribe

Can someone point out the main differences between the two? It seems that, at least conceptually, the two are very closely related. If I were to hazard a guess, I would say that the publish/subscribe method is a subset of the mediator pattern (since the mediator need not necessarily be used in the publish/subscribe manner, but the latt...

iads.h / VS2005 / W2003 SP2 - which SDK and what are the side-effects ?

I'm trying to compile someone elses C++ program using VS2005 on Windows 2003 (SP2). The compile fails because it can't find iads.h Which SDK should I install to get this header ? When I install the SDK is there a danger I might break something already in use ? As far as I can tell the W2003 has no SDK's installed but if it did would i...

Is realloc guaranteed to be in-place when the buffer is shrinking??

Are there any guarantees that realloc() will always shrink a buffer in-place?? So that the following: new_ptr = (data_type *) realloc(old_ptr, new_size * sizeof(data_type)); will always give new_ptr == old_ptr if new_size < old_size (except of course when new_size == 0). It seems sensible (to me) that it would work this way, but was c...

C++: How to declare an array at the top of the method?

I'd like to declare an array at the top of my method, but it's not compiling: Foo Bar() { int arr[]; // C2133 // … // C2059, C2143, C2143 arr[] = {1, 2, 3}; } What am I doing wrong here? UPDATE I know that C++ doesn't force me to do it this way, but the project's convention wants all variables declared at the top o...

How to make GCC search for headers in a directory before the current source file's directory?

I am using GCC precompiled headers in my project with multi-architecture build, but things break down when I try to place it in a directory different from current source's directory. The file is included with double quotes, and it works if I change it to angle brackets, but the problem is that I have a lot of other projects that use th...

Do g++ thread-local variables follow default initialization rules?

Using g++ to declare function-static thread-local storage: void f() { static __thread somePodStruct thing; ... } can I assume that thing will get zero-initialized? ...

using BOOST_TYPEOF in expression.

Hello. Can the following be done in a single line? typedef BOOST_TYPEOF(generator) G; typename G::value_type next; typename BOOST_TYPEOF(generator)::value_type next; //does not compile thank you ...

Proper Trigonometry For Rotating A Point Around The Origin

Do either of the below approaches use the correct mathematics for rotating a point? If so, which one is correct? POINT rotate_point(float cx,float cy,float angle,POINT p) { float s = sin(angle); float c = cos(angle); // translate point back to origin: p.x -= cx; p.y -= cy; // Which One Is Correct: // This? float xnew ...

Convert a quadratic bezier to a cubic?

What is the algorithm to convert a quadratic bezier (with 3 points) to a cubic one (with 4 points) Thanks ...

Does GetPath() return cubic or quadratic bezier control points?

Microsoft's docs say: Specifies that the corresponding point in lpPoints is a control point or ending point for a Bèzier curve. PT_BEZIERTO values always occur in sets of three. The point in the path immediately preceding them defines the starting point for the Bèzier curve. The first two PT_BEZIERTO points are...

Fastest timing resolution system

What is the fastest timing system a C/C++ programmer can use? For example: time() will give the seconds since Jan 01 1970 00:00. GetTickCount() on Windows will give the time, in milliseconds, since the system's start-up time, but is limited to 49.7 days (after that it simply wraps back to zero). I want to get the current time, or ticks...

Can't catch atan2 domain error

I am using a thirdparty library eg. Lib::ValueType value. I then do a call to a member function, value.theta() which performs some mathematical operations including a call to atan2 from <cmath>. Sometimes the theta component is empty and an "atan2: domain error" is thrown. However, I can't catch the exception even by wrapping try{}catch(...

Intentional compiler warnings for Visual C++ that appear in Error List?

How can you create a compiler warning (in the model of #error, except as a warning) on purpose in Visual C++ that will show up on the Error List with the correct file and line number? GCC and other compilers offer #warning, but the MSVC compiler does not. The "solution" at http://support.microsoft.com/kb/155196 does not parse in the Vi...

TestFixtureSetUp-like method in CppUnit

In NUnit, the TestFixtureSetup attribute can be used to mark a method which should be executed once before any tests in the fixture. Is there an analogous concept in CppUnit? If not, is there a C++ unit testing framework that supports this concept? Based on the answer below, here is an example which accomplishes this (following the ad...