Questions about C++, a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language.

learn more… | top users | synonyms

25
votes
10answers
5k views

Isn't the use of pointer variables a memory overhead?

In languages like C and C++, while using pointers to variables we need one more memory location to store that address. So isn't this a memory overhead? How is this compensated? Are pointers used in ...
6
votes
2answers
128 views

Does Time Complexity analysis factor for cache performance of an algorithm?

If I have an algorithm A. and it has fewer instructions than algorithm B. but performs worse on a CPU due to poor memory coalescing (and hence, poor CPU cache performance), does that factor into the ...
1
vote
2answers
129 views

Acceptable design to use public member objects in composition?

Say I have a large class called Root, which has a lot of members & functions: class Root { public: void func1(); // operates on a and b void func2(); ... private: A a; ...
1
vote
2answers
148 views

Should I use a jump table or a switch case statement for similar functions but may have some parameters that are not relevant [closed]

I had a HUGE switch/case statement, which I converted to use a function dispatch table using an enum list for the index. My colleague (who is doing a code review on this change) agreed that it should ...
1
vote
1answer
532 views

Number of parallel lines in a given set of lines [closed]

I am given N lines, i.e, I am given a,b,c for N lines. And each line is of the form ax + by + c = 0. I need to find out the maximum number of lines that are parallel to each other. For this, first I ...
10
votes
5answers
559 views

What is the conceptual difference between finally and a destructor?

First, I am well aware of Why is there no 'finally' construct in C++? but a lengthy-growing comment discussion on another question seems to warrant a separate question. Apart from the issue ...
1
vote
1answer
237 views

Refusing to define 2 or more classes in one .cpp file [closed]

In my company there is implicit rule not to define multiple classes inside one .cpp file. The argumentation is that it looks like a mess and hard to read. But in my opinion it will make sense if the ...
0
votes
1answer
136 views

Implement Stack for Toy VM C++

Recently for fun I have decided to build a toy programming, compiler and a low-level register based interpreted vm. While starting to implement the virtual machine I got stuck. The stack which holds ...
-2
votes
1answer
240 views

Bresenham Vs Midpoint Circle drawing algorithm

Can somebody explain the difference between Bresenham and Midpoint Circle drawing algorithms? All I am able to understand is that in both algorithms we compute coordinates of an octant instead of ...
6
votes
2answers
278 views

Merits of copy-on-write semantics

I am wondering what possible merits does copy-on-write have? Naturally, I don't expect personal opinions, but real-world practical scenarios where it can be technically and practically beneficial in a ...
1
vote
0answers
86 views

Running simulations in parallel, managing each separately [closed]

Let me preface this by admitting that I am a novice - this is the first real project I am doing outside of a class. I am running a single-threaded C++ simulation several hundred times (i.e. I have ...
8
votes
2answers
354 views

Is the strong exception safety guarantee with a pass-by-value argument which can throw on destruction possible?

Suppose you have a type with a throwing destructor, and a function receiving it by value. Can that operation ever provide anything better than the basic exception guarantee? Or formulated differently, ...
2
votes
2answers
136 views

Private variables and the old FORTRAN common blocks [closed]

This is a question that perplexes me about object oriented programming. In some OOP languages (e.g. C++) a member function can access private variables of the class without restriction. That means ...
3
votes
2answers
324 views

What alternatives to a singleton are there for a class which only can have one instance?

I need to represent an abstraction over various parts of the hardware for a game. I'm trying to decouple the code that does things like manage the logic of the game from the code that is API/platform ...
4
votes
4answers
1k views

Is measuring the binary size of a c++ program a good indication of code complexity? Or something else?

In my company we have this obligatory practice before a review to be send the binary size impact of the code change to be measured and provided. We must use -Os for this metric to avoid ...
0
votes
0answers
47 views

Do I have to put copyright info in every file of my project? [duplicate]

Do I have to put a copyright phrase on every file that is used in my visual studio projects? does it even make any sense? example: file.cpp // Copyright Company Name 2000-2015 code code code ...
5
votes
2answers
239 views

How to optimize/parallelize the following clustering/joining algorithm:

I have relatively-small algorithm that takes up ~60% of the total run-time of my scientific code (57 lines of 3600), so I would like to find a way to optimize what I'm doing and make the code ...
1
vote
0answers
61 views

Function behavior dependent on the calling class [closed]

I have a base class B and several children classes of it. Every children B1, B2, B3... has a method that calls a method M of another class C. Class C has a component D (implementing an interface, ...
1
vote
0answers
152 views

What is the “async desaster” Detlef Vollmann was talking about at Meeting C++ 2014?

I found the slides of his talk there: https://meetingcpp.com/tl_files/2014/talks/executors-meetingcpp.pdf In one of the last pages, he is talking about the "async desaster" (which I, as a non-native ...
1
vote
1answer
179 views

Merging Two Binary Search Trees

What time in terms of Big O will it take to merge two BST's in One? Each having no of nodes n and height O(log n) with no common element. Resultant should be also a BST
3
votes
2answers
185 views

Should I add a layer to keep object always valid and immutable?

I am developing COM. A factory need to be initialized first by providing a ID. I can not pass arguments to IFactory instance via CoCreateInstance. So IFactory may need add a init method to be called ...
12
votes
6answers
986 views

In general, is it worth using virtual functions to avoid branching?

There seems to be rough equivalents of instructions to equate to the cost of a branch miss virtual functions have a similar tradeoff: instruction vs. data cache miss optimization barrier If ...
1
vote
1answer
129 views

undirected c++ graph with a direction [closed]

I want to create a Graph in C++ that has directions, with directions I don't mean the direction of an edge but what I want is an undirected graph where the edges have a direction to the next vertex, ...
2
votes
1answer
225 views

Model-View-Controller design question (One vs. multiple models for multiple view+controller)

I have an issue with shared code in a MVC application. Application controllers/views ABC are different classes but use a common framework. It is a single class reused multiple times. Let me introduce ...
6
votes
2answers
284 views

Automatic namespace resolution in C++

My question is focused on design, and the code included in this question is meant to clarify the problem I'm experiencing. I'm interested in conceptual level answers and not code in order to answer ...
0
votes
1answer
154 views

Where is the dynamic memory associated with std::bind?

The problem is that since you can use std:bind in a loop with an arbitrary number of iterations, the storage cannot be inline. It doesn't appear to be on the heap, since it doesn't appear to generate ...
1
vote
1answer
137 views

State Machine Memory Management Problems

My state machine handles requests and returns the next state. In the below simplification, I've got two states here, CreatedState and RunningState. Running state is the end state, and just returns ...
0
votes
1answer
421 views

How to store various sized values in a vector?

I want to separate the communication protocol from the communication medium in a project. For example, I want to have a serial class and whatever other medium class and separately have a checksum ...
4
votes
3answers
216 views

C++ returning persistent objects

I'm currently trying to learn best practices in C++ after coming from a C# background. I understand that there are three ways of handling objects: By value (objects are copied or moved when passed ...
2
votes
3answers
251 views

Is there any merit for using beginning iterator instead of reference to a std::vector?

I'm working on our company's lib. I see a lot of code like: std::vector<int>::iterator it = market.vec.begin(); for (size_t i = 0; i < market.vec.size(); ++i) it[i] = i + 1; I think a ...
2
votes
1answer
118 views

C++ HTTP API Design

I have a massive library written in C++ that handles a lot of mathematical operations, using C++ makes it fast and in all honesty, I don't want to re-write the whole functionality. I am thinking ...
0
votes
1answer
89 views

Function or class design in library API for efficient object reuse

I am currently building a library in C++11, where I spent a lot of time trying to design a good interface. After some small redesigns along the way, I've ended up with a design that I am happy with. ...
-3
votes
3answers
70 views

Link to ISO doc? Complexity of unordered_map operations in C++::stl [closed]

In different forums, I have found the statement that in C++::stl, insert and find for unordered_map have guaranteed amortized time complexity O(1). This puzzles me, and I would like to see the ...
1
vote
2answers
88 views

Fully Specifying #includes

I'm very fastidious about code. When I remove functionality from a file I remove the cooresponding #includes. I've hit several errors where I would remove a #include from a header, and another file ...
0
votes
0answers
87 views

How to improve the design pattern of this Screen Window system?

I have a Screen Window system, a Screen (IScreen as interface), which holds a window, either WindowA or WindowB. System can switch window by calling stateChanged: class IScreen{ public: virtual ...
0
votes
0answers
145 views

C++, header files and using namespace std

So I understand that one must not use using namespace std; in header files. Well for my assignment we have to overload the >> operator. I got my program to run and all, but I had to include the ...
2
votes
1answer
167 views

Feeding std::streambuf for Non-Blocking std::istream Read

I want to write small library to guide a child process for a specific needs (not trying to reinvent bicycle here). My needs includes reading from child stdout and stderr. I want to implement this as ...
2
votes
3answers
175 views

Call by value for classes c++ design decision [closed]

I wonder why C++ uses 'call by value' for classes as default? I imagine this could have advantages in the sense of performance (speed). I mean C# and Java pass classes as references as default. What ...
4
votes
3answers
285 views

What are R-value references used for?

I'm trying to dig deep into C++ and really learn the nuances of the language but one thing I've found to be really confusing is the R-Value reference. The whole double ampersand thing to be clear (in ...
3
votes
0answers
131 views

Is it okay to write C code that must be compiled with -fno-strict-aliasing? [closed]

Some major C projects violate the strict aliasing rules in the C standard and must be compiled with -fno-strict-aliasing. These include Python, OCaml and the Linux kernel. Obviously this is not ...
1
vote
1answer
102 views

Data structure to use for time-optimized line-vs-grid check?

I am asking here because I feel that this question can be filed under the "algorithm and data structure concepts" category. Background: I have been recently assigned the task to design and develop a ...
4
votes
1answer
159 views

Optimal fixed-size sequential sorting algorithms

I have been working on sorting algorithms for a few weeks now, but one of my question still hasn't an answer: are there optimal sequential comparison sorts for fixed-size – and random-access – ...
3
votes
3answers
253 views

non-optional pointers vs. non-const references in C++

In Other C++ Features, Reference Arguments of the Google C++ Style Guide, I read that non-const references must not be used. All parameters passed by reference must be labeled const. It is clear ...
-5
votes
1answer
148 views

Using delete this to free memory in class

Whenever we are writing destructors in C++ (though this concept could apply in other languages) could we just not write delete this; and it would delete whatever data is inside of the class? Is ...
2
votes
1answer
91 views

How to generate learning material from your work [closed]

Catalyst: Recently my team has been tasked with bringing in house a large software project that has been worked on for over 20 years. Some of the old development team is still around, and originally ...
5
votes
8answers
299 views

Designing around shallow constness with inheritance

Background I'm writing an image handling class. For this question two requirements of the class are of interest: Must have "deep" const correctness. Must allow sub-image aliases, a.k.a. sections or ...
133
votes
11answers
19k views

Should I stop using the term C/C++?

I understand C and C++ are different languages but when I was learning C++ I was always told that C is a subset of C++ or C++ is C with classes. And that was quite true until the appearance of C++x0, ...
3
votes
3answers
204 views

C++ Extensible namespaces - how to force declarations back into global namespace

It is good programming style to include all necessary dependencies in a header that references them. Often this includes declarations that are placed in the STD & global namespaces (like cstdio). ...
1
vote
1answer
55 views

Threads Inside Full Duplex Device Library

This is the second project I'm working on which will use a design that I'm not 100% confident about. I'd like some feedback, maybe recommendations for a better design, or verification that this would ...
4
votes
2answers
130 views

Does returning pointer to composed objects violate encapsulation

When I want to create an object which aggregates other objects, I find myself wanting to give access to the internal objects instead of revealing the interface to the internal objects with passthrough ...