Tag Info

Hot answers tagged

25

In C, a function with an empty parameter list () can take anything for its arguments. Literally anything. This is usually used to implement a function which can take a variable number of arguments, though these days it's considered preferable to use the more explicit ellipsis syntax (...) for these functions. In C, a function with the parameter list (void) ...


16

Sometimes test code contains snippets of code from third parties, both external and internal to your company. This happens as users file bugs; your tests (such as regression tests) then incorporate the code supplied by them to reproduce. Often, the licensing of such code snippets to reproduce bugs is unclear. So, you should be aware of intellectual ...


14

I do not know for sure, but I would be willing to bet that the answer is probably because the inventors of C ran out (or nearly ran out) of characters. The original intention was that the language should use symbols for operators, and also that the language must be expressable within the so-called "lower" ASCII table, which consists of ASCII values 0 ...


10

Shipping tests? Yes. Shipping Unit tests? No. As you say in the comment, problem you may face when running the product on a client computer will include problems such as linking with the wrong dll, generally this is not something a unit test will catch (which will no doubt have mocked the dll out to test the code). Now, shipping an integration test suite, ...


10

You have two separate issues: How to code a C or C++ program which can easily be ported to several operating systems. The easiest way is to use some cross-platform framework library like Qt or POCO (or perhaps libsdl or GTK) which has been ported to several platforms and provides a common set of abstractions. You could also restrict yourself to purely C99 ...


10

As a rule, you should only use macros, when a better alternative does not exist. They should not be used to generate code; you should simply write the code instead (if the code is type-agnostic, write a template instead). They should not be used to define constants; constants should be defined using one of these: (static) constexpr/const variables, ...


7

When are macros idiomatic and when should they be avoided? Macros are idiomatic only when there is no alternative to their use. Examples are include guards (they are the only portable form), embedded domain-specific languages, special compiler support not available through other language features (embedding built-in macros like __FILE__, stringifying ...


5

You have class A and you need to use A with C code: A.h #if !defined(A_H) #define A_H class A { public: void set(int); int get() const; private: int a; }; #endif A.cc (to be compiled with c++ compiler) #include "A.h" void A::set(int i) { a = i; } int A::get() const { return a; } Now to access A from C code: Add a wrapper: ...


4

strtok is not thread-safe because it saves its state in static memory between calls. If two threads make concurrent or interleaved calls to strtok, they will get erroneous results. The strtok_r variant returns a pointer to the caller to use for future invocations on the same string. There are many other C and POSIX functions that are not safe to use from ...


4

You seems to want to make some optimization (e.g. common subexpression extraction, as commented by rwong) in your interpreter. Notice also that a language (even a domain-specific language) is never defined by some internal representation, but a computer language is defined by its syntax and its semantics (so you'll better define them first, at least on ...


4

Simplest method is to pass a callback function to the state machine that gets called when a state changes (or other event happens). Then when it gets called you can update the gui in that function (or forward the message to the gui thread if the state machine runs in a separate thread). The signature would be something like void callback(State oldstate, ...


3

The only time I'd consider macros are for compile time constants, typically ones that affect the compilation (as opposed to constant application values, which are better modelled as const types). However, I have modelled utility routines such as calls to logging functions in a macro just so they will be compiled out, but I think even this is unnecessary ...


3

First of all, there is an important thing to understand in this line: char *s1 = "abc"; The variable s1 does not store any bytes of the string "abc". Instead it is a pointer (as indicated by its char * type) that contains some address in memory, where the actual bytes of the string are stored one after the other, with a zero terminator at the end. This is ...


3

There are always two sides to constness: Being const in the sense of the programming language, and being logically constant. Say a C++ object has a pointer to a std::string object. If the C++ object is const, I can't change the pointer to point to a different std::string object. However, I can change the contents of the std::string object. Which doesn't ...


3

1.Just put all internal prototypes, typedefs, defines etc. to the top of the .c-file. I have done this in the past, but I do not like it. It does not seem consistent to pollute the .c-file with header-material. Function prototypes are not header-material. Public API function prototypes are header-material. If you had a single compilation unit, you ...


3

During learning C++, I have found books recommending to avoid creating containers with pointers. So, should I just accept this case as an exception to the rule ? No, burn the books instead. There's nothing special about T* that makes it unsuitable in a container. How should I optimally design the initialization of the Nodes and the destruction ...


2

I have made a pair of co-ordinates and pair it with an index in another pair. How can I access members of these vector pairs...?? You access elements of an std::vector via std::vector::operator[]. You access elements of a std::pair via std::pair::first and std::pair::second. Suppose vec is a std::vector< std::pair<std::pair<int,int>,int> ...


2

StackOverflow would be a somewhat better place for asking programming/algorithm related questions. In any case, the implementation you must have read would be based on "tables". Here is how such implementation works: Initialize vector with size n, say n = 16 Address: 0xAAA0 to 0xAAB0 Memory reserved Insert 17 elements. First 16 inserted fine. Next ...


2

The accepted answer mentions only the pros of converting C to idiomatic C++, as if C++ code would be in some absolute sense better than C code. I agree with other answers that it's probably unnecessary to do any drastic changes to the mixed code if it isn't broken. Whether mixing C and C++ is sustainable depends on how the mixing is done. Subtle bugs can be ...


2

C and C++ are different in this respect. C 2011 Online Standard 6.7.6.3 Function declarators (including prototypes) ... 10 The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters. ... 14 An identifier list declares only the identifiers of the parameters of the function. An empty ...


2

I want the new subtypes to inherit the way they are handled from their base type The classic GOF visitor pattern behaves exactly like that. For example, look the Wikipedia CarElement example (it is a Java example, but the important part is similar in C++). Lets assume you add new Wheel types like SquareWheel, RoundWheel by inheriting them from Wheel. ...


2

Is all the functionality known at compile time? If so, template metaprogramming might work. See a real example here, but something like this --- template<class T> ExtraFunctionality { //do something with instances of class T } template<class T> OtherExtraFunctionality { //do something else with instances of class T } template< ...


2

You can try using std::function to pass function pointers around, or just raw function pointers if not (I have used the latter for lookup tables of routines to call based on some data input)


2

If the logic for choosing the next state is a part of the Algorithm classes, then clearly they must be able to communicate the specific state to change to to the Context class. If the concept of states to change to is useless without the 'context' object (hard to tell), passing a reference to the context object should be fine - the algorithms and the ...


2

Services, not Superclasses These things are not related objects, they are behavioral aspects of game objects that may come and go as the state of the game objects change; think about them as services, not superclasses. They're not well-suited to purely compositional elements either, as the in-game behavior will likely be strongly dependent on the state of ...


2

GUI libraries (like for example Qt) are using some kind of subscriber-publisher design pattern, which is event based. That means, on an event, all subscribers are going to be notified. So, on a state change, publish the event. Every object that subscribed to the event, is going to execute a callback.


1

Would this model benefit from shared pointers? It looks like you hold the pointers and take ownership of them. It also seemsn that client code doesn't share ownership. At this point (heh punny!), you would benefit from implementing this in terms of a std::unique_ptr, and exposing either a custom safe wrapper around a pointer, or a Blob* const to client ...


1

I'm no C++-Expert, but in the last days I did some research around "smart" pointers, so maybe I can help you: Shared pointers may help you in this situation, but they may be just a too easy way out of a situation where it is not too clear in the first place who the data really belongs to. Accessing a shared pointer is cheap, but copying it has a small ...


1

It's certainly do-able, but it's not small. An incomplete minimal example looks something like: template <typename BaseIterator, typename FieldType, FieldType SensorReading::*FieldPtr> class FilteredIterator { BaseIterator base_; typedef FilteredIterator<BaseIterator, FieldType, FieldPtr> self_type; public: // iterator type traits ...


1

Boost may be cutting edge when it is first released. But it (boost) should also be considered a testing ground for new ideas. The thread ideas introduced in boost were a good idea at the time and the lessons learned helped the standardization committee when they added threads to the standard. But now that threads are part of the standard you should be ...



Only top voted, non community-wiki answers of a minimum length are eligible