New answers tagged

0

In C++11 (and other dialects of C++, and in C) you can have several translation units for a given program. So your program could be made of foo.cc, bar.cc, gee.cc C++ source files compiled separately and linked together. If using GCC (e.g. on Linux), you would compile your program with e.g. g++ -Wall -g -c foo.cc g++ -Wall -g -c bar.cc g++ -Wall -g -c ...


3

It's largely a matter of personal preference, although some places may make it a formal coding standard. C itself doesn't care. For my part, if I'm defining multiple functions in a single source file, I will define the called functions before the caller: void foo( void ) { ... } void bar( void ) { ... foo(); ... } int main( void ) { ... ...


1

The best place is somewhere that people can find it. In the middle of a very large file is bad. At the top is good, but others might prefer right at the bottom. Forward declaring main() doesn't help, and is often pointless anyway - because it's very unusual to explicitly call main() from within your code.


0

There are a few things you should ask for: Source code (obviously). This should include any unit tests and integration tests. Complete documentation on the build process, all the way through to delivery of the binaries to the app stores. This should be demonstrated and possibly video'd Any and all keys or accounts necessary to upload the code to the ...


0

I understand the following requirements: it's game code, so it has to be reactive changes are monitored every 100ms. this processing speed of the chose option should cope with this frequency despite network and database latency. On the client side: Option 1 would require 10 times per second: the preparation of POST (conversion to strings and ...


4

What you're talking about will not be directly possible in C++ until we get some form of both reflection introspection and reflection-based generation of types. So you're going to have to wait for a while. The closest you can get right now is to employ inheritance: template<typename T1, typename T2> struct joined : public T1, public T2 { }; Of ...


3

Why nested classes ? Bjarne Stroustrup explains in "The design and evolution of C++", the origin and rationale behind nested classes: original C++ in 1984 had a single name space (page 5 and 102). the use of nested classes was a compromise between the the concept of a class as a scope and the need of compatibility with C (page 102) it was further ...


0

Out Of The Tar Pit (PDF), published in 2006 describes functional relational programming. Think about your classes as tables in a relational database. How would you construct this relationship? Boss and Worker would be “Employees”. The relationship between the boss and the worker is a one to many relationship: each employee should have one boss. This ...


0

There is nothing wrong with this design. Java doesn't care about what order to compile things in, it usually just magically works. Well, I've found a few horrendously complicated cases involving static initialization for public static variables and class-loading order where I ran into problems, but that's extremely rare. If you have an issue, it will be ...


2

I will try to refine a point raised in gnasher729's answer. We should make a careful distinction between: Ownership - Boss owns a Worker. If Boss is destroyed, the Worker it owns must be destroyed simultaneously. Independent existence (entity) A Boss and a Worker can be in existence with individual lifetime. If one is destroyed, the other is not ...


-1

In some languages (Objective-C, Swift, I think C#, and C++ with the right pointer class, you have owning references which keep an object from being deallocated, and circular owning references are usually frowned upon because the keep objects from being deallocated. In the case of Boss and Worker that's nonsense. If the person represented by the Boss object ...


0

For one, that's not the kind of "wasted CPU cycles" that you need to worry about. Even if the second condition was checked, that's a cycle or two. Beginners worry about nanoseconds. Experienced programmers worry about microseconds (I used to say they worry about milliseconds, but times are changing). It's called micro-optimisations and is the kind of thing ...


1

Possible compiler optimization aside, C and C++ language specs explicitly says that expression such as if(i == 2 || i == 4) will be evaluated left to right and second part after || will not be evaluated if first part is true. This is particularly important when right part after || is an expression that has side effects. For example, if it is a function call ...


18

There is nothing which is fundamentally flawed about this idea. What you have is two relationships. Boss owns one or more Workers. And Worker has a non-owning reference to a Boss. The use of a raw pointer suggests that Worker does not own the pointer it stores; it's merely using it. This means that it does not control that object's lifetime. There is ...


4

C++ doesn't have a manual approach to memory management at all. If you're calling free in C++, you're using it wrong. That's the C way that we're proud to be disowning. Really, the predominant models are non-deterministic (GC), deterministic (C++, D), and manual (C).


3

The compiler operates under the as-if rule that allows any and all code transformations that don't change the observable behavior of the program. [C++14: 1.5/8] The least requirements on a conforming implementation are: Access to volatile objects are evaluated strictly according to the rules of the abstract machine. At program termination, all ...


1

Not enough information is given. The first version assumes that b is invariant over foo() and bar() for all parameters a. The second version assumes that b is not necessarily so invariant and programs defensively. The compiler probably has no way of knowing that b is in fact invariant over foo() and bar(), and hence cannot translate the second version ...


2

I am wondering if there are good examples or ideas on simplyfing the process of converting documentation of the file format into code with goal being to load data into a class that can be loaded/saved/processed. This can be solved at multiple levels: you can use boost::spirit parsing, or a custom serializer/deserializer (as suggested in the comments) you ...


2

A possible way to help portability could be to rely only on declarations and features provided by the C++11 standard, and by using cross-platform libraries and frameworks like POCO & Qt. But even this is not fail-proof. Remember the aphorism there is no such thing as a portable program, there are only programs which have been successfully ported (to ...


11

Creating portable code can be very challenging. First some obvious language related advices: use standard C++ and avoid carefully any undefined behavior rely primarily on standard library (and portable libraries such as boost) always include all expected headers. Do not assume that you don't need a header because it's included in another one (i.e.on ...


3

It depends on the "some compilation errors" you mention. Without knowing what they were, it's impossible to be specific. I've got cross-platform code for Windows / Linux / iOS / Android / Mac. Each new platform brought some extra errors and warnings when it was first added. You will quickly learn which constructs bring problems. Either avoid them, or ...


7

If you are asking for "development processes" and you primary development platform is Windows with Visual Studio then I would suggest to try building your project without "windows.h" included. You will get a lot of compilation errors that will point you to many places where you'll need to refactor your code. For example, 'DWORD' won't be #defined and you'll ...


11

There is nothing that can guarantee that the code is compatible with a platform other than building it, running it, and testing it there. Therefore, the approach of all sane people is to build, run and test their application on every platform that they project it will need to be built, run, and test on. Continuous Integration (CI) can ease this burden a ...


5

If the language supports it, I'd opt for: f = b ? foo : bar; for (auto a : as) { f(a); } This approach avoids worrying about whether the if is repeatedly evaluated and avoids code repetition.


2

Joker if (b){ f = foo; } else { f = bar; } for (auto a : as) { f(a); }


0

Sounds like a state machine to me. Create a set of events and have the end of one start the beginning of the next one. The line growing would be a state (with state of its own representing the current length), then the full length line (maybe pulsating or something) would be another state. Then, maybe an event would then trigger a "close" even where the ...


2

Something that I don't think has been mentioned here is that there are efficiencies that come from garbage collection. In the most commonly used Java collectors, the main place that objects are allocated is an area reserved for a copying collector. When things start, this space is empty. As objects are created, they are allocated next to each other in the ...


2

The big difference that garbage collection makes isn't that you don't have to explicitly delete objects. The much bigger difference is that you don't have to copy objects. This has effects that become pervasive in designing programs and interfaces in general. Let me give just one tiny example to show how far-reaching this is. In Java, when you pop ...


1

You have to have the same amount of code, regardless whether it all lives in a single large class or several smaller classes! An ORM can help you write this code, but if we're talking about organisation of higher level DB repository for your business objects, you don't have much choice. Its easy to create many classes to handle DB transactions between ...


2

(I am not sure to understand what you want; I'm trying to guess; asynchronous IO could mean aio(7) but then you won't speak of threads to do them; however I am supposing you are on Linux - adapt my answer to other OSes) You might consider using condition variables using std::condition_variable with std::mutex (probably some other thread would signal the ...


2

C is used basically in embedded systems where using C++ is most of the time overkill and sometimes not possible. C++ is better suited for desktop applications and video games development. Most desktop apps are written in C++ (Chrome, Mozilla etc...) and the Windows OS is written in C++. For video games it is much better to use C++ since the OOP aspect of ...


1

Is returning a private pointer is a bad idea ? By making a member private, you express that this is the class internal business, and that you want to ave the freedom to manage such interals as you want. The problem when returning a private pointer, you give your control away, breaking your own design intention: What happens if the caller deletes the ...


3

If you write correct C++ code with RAII you usually don't write any new or delete. The only "new" you write are inside shared pointers so you really never have to use "delete".


4

Making programmers' lives easier and preventing memory leaks is an important advantage of garbage collection but it's not an only one. Another is preventing memory fragmentation. In C++, once you allocate an object using the new keyword, it stays in a fixed position in memory. This means that, as the application runs, you end up having gaps of free memory in ...


21

It should be noted that it is, in the case of C++, a common misconception that "you need to do manual memory management". In fact, you don't usually do any memory management in your code. Fixed-size objects (with scope lifetime) In the vast majority of cases when you need an object, the object will have a defined lifetime in your program and is created on ...


12

The C++ object lifecycle If you create local objects, you don't need to delete them: the compiler generates code to delete them automatically when the object goes out of scope If you use object pointers and create objects on the free store, then you have to take care of deleting the object when it's no longer needed (as you have described). Unfortunately,...


2

I've learned to classify memory issues into a number of different categories. One time drips. Suppose a program leaks 100 bytes at startup time, only never to leak again. Chasing down and eliminating those one-time leaks is nice (I do like having a clean report by a leak detection capability) but is not essential. Sometimes there are bigger problems that ...


7

With respect to C specifically, the language gives you no tools to manage dynamically-allocated memory. You are absolutely responsible for making sure every *alloc has a corresponding free somewhere. Where things get really nasty is when a resource allocation fails midway through; do you try again, do you roll back and start over from the beginning, do ...


4

This delete seems like a very low price to pay for non-freezing environment. Placing all relevant delete keywords is a mechanical task. One can write a script that would travel through the code and place delete once no new branches use a given object. If you can write a script like that, congratulations. You're a better developer than I am. By far. The ...


2

Java's main promises were Understandable C like syntax Write one run everywhere We make your work easier - we even take care of garbage. Seems like Java guarantees you that garbage will be disposed (not necessarily in an efficient way). If you use C/C++ you have both freedom and responsibility. You can do it better than Java's GC, or you can ...


-5

It is up to the C++ programmer to implement his/her own form of garbage collection where necessary. Failure to do so will result in what is called a 'memory leak'. It is pretty common for 'high level' languages (such as Java) to have built in garbage collection, but 'low level' languages such as C and C++ do not.


38

In C, C++ and other systems without a Garbage Collector, the developer is offered facilities by the language and its libraries to indicate when memory can be reclaimed. The most basic facility is automatic storage. Many times, the language itself ensures that items are disposed of: int global = 0; // automatic storage int foo(int a, int b) { static ...


22

C++ has this thing called RAII. Basically it means garbage gets cleaned up as you go rather than leave it in a pile and let the cleaner tidy up after you. (imagine me in my room watching the football - as I drink cans of beer and need new ones, the C++ way is to take the empty can to the bin on the way to the fridge, the C# way is to chuck it on the floor ...


94

The programmer is responsible for ensuring that objects they created via new are deleted via delete. If an object is created, but not destroyed before the last pointer or reference to it goes out of scope, it falls through the cracks and becomes a Memory Leak. Unfortunately for C, C++ and other languages which do not include a GC, this simply piles up over ...


78

C++ does not have garbage collection. C++ applications are required to dispose of their own garbage. C++ applications programmers are required to understand this. When they forget, the result is called a "memory leak".


1

Your first approach look good. Clan/character is a container relashionship and so the ideal approach would be the Clan has a list of members, and the character is unaware of Clan class. That means character's API must be designed so Clan can implement its services.


1

Look at prior art - the Apple IOKit is the C++ framework/API for device drivers in OS X/Darwin kernel. Also, you may even get some inspiration from the Linux kernel - Torvalds is a famous C++ hater, but many drivers (e.g. USB drivers for 4G mobile modems) are using hand-hacked inheritance in C, and the driver code is often quite readable.


6

While you may have 4 CPUs, you have only one hard-drive (unless you don't). The total performance will therefore be limited by your disk drive's read/write rate. Multiple threads isn't going to change that. Having a single separate thread handle all the file IO will allow your application to remain responsive while still getting things done asynchronously. ...


2

This is a very broad topic. Unfortunately there's no general answer, due to the complex matter of patentability. And in view of the existing practice of patent trolling, there is always be some legal risk to be sued in countries in which software patents are accepted. As a first intro, you may be interested in this WIPO article, and especially TIP3, about ...


0

Some compilers may offer its facilities as a library not just a stand-along program. For example TCC offers libtcc, a library that can take C source code and compile it, and optionally run it. It's most of the internals of the tcc compiler stored as a library. However that specific example only compiles from C. There is also libgccjit, which is only ...



Top 50 recent answers are included