« first day (682 days earlier)      last day (2154 days later) » 

3:12 AM
Why can't I memset a unique pointer with 0's without the program crashing when I try to access that memory again?
 
 
4 hours later…
nwp
7:12 AM
@nobism Technically because you broke the object by overwriting its data, practically because dereferencing a nullptr usually ends in a crash.
You should avoid memset, memcpy and so on on C++ objects because they only handle bytes while C++ objects also have invariants that C functions don't care about, leading to bad behavior of those objects. Also it's usually UB.
 
 
1 hour later…
8:20 AM
@nwp Gotcha, thanks.
 
Sam
8:35 AM
Assume I have a class Car which inside do everything which build a car e.g. Engine, Gearbox. If I wanted to abstract these inner components into a their own classes, would I initialise those objects within the Car classes constructor OR initialise them before creating a Car instance and inject them into the Car object instantiation?
 
@Sam In real life a car doesn't build itself, unless maybe it's a transformer, so I would go with your second option.
 
nwp
That should be defined by your domain. Your domain should answer the question of "What do I need to make a car?". If it is an engine and a gearbox then you pass those. If they are implementation details you don't pass them and build them in the constructor instead.
 
Sam
So I would have some factory which instantiates the relevant parts of the car and injects them into the Car when creating the object. OK I think I got it
 
nwp
You can also go half way and do Car::Car(Engine e = default_engine{}, Gearbox gb = default_gearbox{}).
 
Sam
Am I right in saying this is dependency injection?
 
nwp
8:41 AM
I don't think so.
Usually dependency injection is for things that the class doesn't actually depend on.
@Sam Are you sure you need a factory function and can't just use the constructor?
 
Sam
I was running with the analogy of car building :P
 
nwp
Ah, right, different thing.
 
Sam
I'll just create the Car function and use the constructor (as your code example above)
Yeh, factory probably isn't the best word to use when talking design patterns
 
Yes, using the constructor would be a good idea.
You could then initialise the engine etc. as private member variables in the initialiser list.
 
Sam
@nobism Would this mean they are fully encapsulated within the Car classes methods?
 
8:50 AM
@Sam I wouldn't use the word encapsulation to describe their relationship, and it has nothing to do with the Car's methods.
 
Sam
What does it mean to be a private member variable in the initialiser list?
Sorry, I'm completely new to c++ and still wrapping my head around OOP
 
What I mean is that Engine and Gearbox are private member variables of Car. And you initialise them in the initlaiser list using the constructor.
 
Sam
Ohh I see what you mean.
 
Check out the UML 'has a' relationship.
Car 'has an' Engine, Car 'has a' gearbox etc.
 
Sam
Ohhh OK. Google Images has given me some nice explanations. Association is something I need to look into
I have a patient structure in which a bunch of medical measurements are stored about them, I was hoping to maybe abstract them out into more modular structures
At present, the constructor has 232 lines of class assignments
 
8:58 AM
wow
The constructor is 232 lines long?
 
Sam
yup
 
That's nuts
 
Sam
Yeah its a bit depressing
Plus i'm not even a c++ programmer so it makes this project even more fun
 
You'll get used to it, once you learn the syntax, you'll start thinking in relationships and higher level stuff like that which is easy.
Car 'has an' engine then becomes obvious.
 
nwp
For patients in medical environments you should probably do whatever DICOM does.
Although it may not apply because that's somewhat specific for imaging. But I suppose patient stuff is still usable.
 
Sam
9:04 AM
I think I can also see use for polymorphism. We have 2 files which contain equations to apply (for some transitions). At current, there is an if statement which checks the state of some variable and applies the corresponding equation set
I think I could maybe have an equation interface and branch of the sets that way.
 
Good idea
 
Sam
@nwp How do I look into there structures?
 
nwp
@Sam I wouldn't do that. Polymorphism to avoid a branch is like chopping off someones head to cure a headache.
Maybe you can make an Equation object instead that you just set once depending on the variable and then use it without caring which one it is.
 
Sam
I thought it was common practice to program to interfaces?
 
nwp
Depends on what you consider an interface. If you think of abstract base classes with no data members as interfaces then no, those are far less common in C++ than in, say, Java.
@Sam This I guess.
Polymorphism gives you pointer semantics, dynamic memory allocation and the reoccurring problem of trying to figure out what type stuff is.
Sometimes you can hide it well like in std::function or it is super worth it like in std::ostream, but generally you want to stick to values as long as possible.
 
Sam
9:12 AM
So I should keep it static and just perform the check?
@nwp When the titles say 'modules', am I to infer classes in this context?
 
@Sam It is common to program to interfaces, but the word 'interface' is used more broadly, rather than the concept of an interface in the programming language.
 
Sam
Maybe I should look into that concept further then
 
What you could have is an interface with virtual pure functions, say class A. Have a class B implement those functions. And then you pass B to another class, C, who only sees the interface A.
 
nwp
@Sam I guess that's the closest equivalent.
 
Then B uses the functions, not knowing (or caring) which class those functions have been implemented by.
 
Sam
9:17 AM
What happens in the case of a class implements A's functions but has additional methods to implement?
 
nwp
What you also have in C++ is templates where the interface is implicitly defined by the usage of the templated things. It has different properties than Java style interfaces and is more common in C++.
 
C won't be able to use them.
C would only see A's functions.
 
nwp
But generally removing type information by not passing the type that stuff actually is gives you a huge disadvantage. Only use it when you are getting an appropriately huge advantage out of it.
 
@nwp If you're passing in an interface, then you'll be alright.
 
Sam
@nwp That DICOM looks interesting. Question though, assume you were to implement all those modules, each of which hold certain characteristics for a given patient... If all of these "modules" were to be made inside the constructor of a Patient class, how would I pass in any parameters to each of these individual modules
I feel it could get quite messy creating an object whilst passing in an object with a bunch of parameters
patient::Patient(Mod 1 = Mod 1 {param1, param2}, Mod 2 = Mod 2 {param1, param2}
 
9:25 AM
You can call constructors in constructors, the outer constructor would need to first get the parameters for the inner ones.
 
Sam
Example (minus the syntax)
 
Patient(Param1 param_for_mod) { Mod my_mod = Mod(param_for_mod); }
 
nwp
You should really look into laws and standards in that field. I believe in the UK it is illegal for a doctor to enter patient data at all. The patient has an insurance card with the patient data and they have to use exactly that, so your application might be forced to just use the given dataset as is.
 
Sam
So the program in question is for a DLL who's front end is written in VBA (Excel). It passes us a lengthy vector in which I pull all the data I need via indexes
 
nwp
There are also DICOM libraries that do most of the stuff for you and you should consider using one instead of re-implementing it. The DICOM standard is huge.
 
9:30 AM
Is there are reason why you need to use C++?
 
Sam
Purely for speed. The program performs monte carlo simulations which are too slow when programmed in VBA
 
9:56 AM
Are you writing the monte carlo algorithm as well?
 
@Justin hm i get error on vs2013
 
Sam
The code base has already been written and works fine. I'm just trying to slowly resturcture it
 
Ok, gotcha
 
Sam
It will also help me learn the code if I try refactoring it
 
10:15 AM
yeh exactly
 
what do people need to know about memory models???
(in both c++ and java)
 
Sam
Memory
 
?
 
Sam
Making a failed joke, sorry.
 
lol
 
Sam
10:28 AM
I have no idea what memory models are. Googling them now.
 
nwp
@Permian From what I understand memory models are used in the context of multithreading. They define when changes to memory done by one thread become visible to another thread and what happens on concurrent access.
 
it seems like to cpu caching as well
which ups the level of difficulty
link**
 
nwp
@Permian Well, kinda sorta. In theory the memory model is independent of the CPU. In practice you kinda have to make your memory model CPU-friendly, otherwise you lose massive performance.
 
how much do you need to know about this sort of stuff?
in both c++ and java dev (to a good level)
 
nwp
Well, you need to know the model. In C++ you must know that a data race is undefined behavior. That's about it for regular application development.
Java doesn't have undefined behavior so the rules are a bit different, but I don't remember them exactly.
I'm fairly sure C++ essentially uses sequential consistency.
It is always a trade-off between performance and understandability. Sequential consistency seems to be near the sweet spot of barely understandable while delivering decent performance.
Python uses a much easier model but leaves performance on the table, which is also a reasonable trade-off.
@nwp (*in the absence of data races)
 
11:32 AM
right
is this just a matter of googling around or reading some standard text?
this sort of thing is never covered in standard java/c++ programming books
 
nwp
I expect that race conditions come up in every book that covers threads.
 
no but with respect to memory model
s
 
nwp
There is a talk by Herb Sutter where he says that developing C++ programs does not require you to know about memory models. Knowing about data races is good enough.
 
oh cool
 
nwp
Herb Sutters talk titled "atomic weapons" 1&2 has all the details you don't need to know about.
Well, some of the more interesting details at least.
 
11:38 AM
do you happen to know why there is c++ q&a and not a java q&a???
 
nwp
You do need to know that when people tell you that reading and writing ints on x86 is atomic they should be yelled at for spreading dangerous half-knowledge and ignore them.
@Permian I don't know. This room isn't that old either. There probably is some Java room somewhere answering questions.
 
i asked in normal java now
@nwp those guys get aggressive
its only for more formal questions
 
nwp
30
Q: Java Memory Model

Swaranga SarmaThe Java memory model is a huge topic in Java. I do not expect anyone to answer or explain the Java memory model in Stack-overflow. But can you please share links to good references that explain this. It will help if the references are not too technical but targeted for intermediate Java programm...

They still manage to produce decent results.
 
closed as off topic! lol
 
nwp
Wait, they are talking about something different.
Well, kinda. The comment clarifies it.
 
11:43 AM
physics se is quite aggressive as well
math se completely isnt though
 
nwp
12:05 PM
This is not legal right? Clang doesn't like it.
 
 
1 hour later…
Sam
1:28 PM
@nwp Following our discussion earlier about DICOM structure.. If I'm only storing patient data in a more abstract and modular way... would using a structure suffice as opposed to making classes
 
nwp
struct and class do the same thing in C++ besides default visibility.
 
Sam
I assumed a struct would only have attributes, no methods
 
functionally they are the exact same except the default visibility
 
Sam
Default visibility? Are you able to expand
 
structs default to public as the default access modifier
classes default to private
 
nwp
1:32 PM
Basically class{ is the same as struct{private: and struct{ is the same as class{public:.
Except you also need to add it when inheriting.
 
Sam
AH OK I'm with you both. If they are both the same, would one favour implementing a class over a struct in any given instance?
 
the difference is handy for documentation purposes but that's about it
 
Sam
Seems odd to have two almost identical types
 
nwp
Since C++ doesn't care some people use conventions such as "structs are a collection of stuff while classes have invariants", but it doesn't tent to be well-followed.
@Sam Historic reasons. Bjarne thought you can't make classes with C's struct, you need a new keyword. Turns out he was wrong.
But too many people were already using the class keyword to take it away.
 
he wouldn't have been wrong if he disallowed inheritance on structs
 
Sam
1:36 PM
Meh OK. I'll stick with my classes :P
 
@ratchetfreak except they found that structs are perfect for making interfaces (pure virtual objects)
 
nwp
Then people kinda repeated that mistake with typename.
template<struct T> should be the only thing, but now that isn't even legal.
 
if any of you is familiar with opencv... Given an image, and a cv::Rect, how can I extract a subimage with wrap around?
in case the borders of rect don't fit the image
is there any ad hoc function?
 
Sam
2:10 PM
This doesn't fit in with my current intuition on pointers..
DoublePtr createArrayDouble(int size)
{
	DoublePtr dbPtr;
	try
	{
	    dbPtr = new double[size];
    }
	catch(std::bad_alloc&)
	{
	   throw;
    }
	for(int i = 0; i < size; i++)
	{
		dbPtr[i] = 0;
	}
	return dbPtr;
}
Why would you try and fill a vector which is a pointer? Shouldn't we make a vector and then declare a pointer to that vector
 
a pointer can point to the start of an array of multiple objects
but yeah you should be using a vector there to avoid leaks
 
Sam
So in its current implementation this is still right
 
nwp
That function unnecessarily leaks by default. Not good code.
 
Sam
What do you mean by leaks
 
nwp
If you call createArrayDouble(42); you leak memory.
 
2:14 PM
the new[] allocates a block of memory that needs to be delete[] down the line
if you discard the return value it will never be freed
 
Sam
OK, alternatively, I would just create a vector of a given length and return that
 
nwp
As a quick fix you should replace DoublePtr with std::unique_ptr<double[]>. As a proper fix replace all occurrences of createArrayDouble(X); with std::vector<double>(X);.
 
Hi everyone.!
 
Sam
OK. I think i need to do more reading on pointers. It's still not intuitive.
 
Hi Sam.
 
Sam
2:18 PM
Hi.
 
Would you like to help me about something.?
I want to use "SetWindowsHookEx()" as service.
 
Sam
It's OK thanks. I think I need to do some more reading into this pointer paradigm.
OH I thought you were offering help to me, sorry xD
 
But it doesn't work well.
 
Sam
@nwp Is 42 a magical number?
 
nwp
Maybe search for "ownership" when reading about pointers.
 
2:20 PM
@Sam it's the answer to life, universe and everything
 
Could anyone help me.?
 
nwp
@Sam Yes.
 
0
Q: C++ Q: SetWindowsHookEx callback function does not work on SYSTEM authority

A.GodnovEnvironment : I am using VS2010 on Win10 x64 [16299.431]. I want to log some keys on my PC using SetWindowsHookEx function. Ultimate goal : If someone type correct password in order, my PC will unlock automatically. Problem : It doesn't work on SYSTEM authority. Implementation: #define L...

SetWindowsHookEx function does not work on SYSTEM authority.
How can I solve this problem.?
 
@A.Godnov Please don't question dump because your question has not had enough visibility
 
Sorry, I am new to "chat".
Please help me a lot.
 
2:22 PM
@A.Godnov Also this is blocked for security reasons, the only way to unlock a machine locally is via the secure desktop
or via exploits of course
 
Yeah.? So, it means, you can't help me.?
 
@A.Godnov it means you're trying to do something the system is explicitly prohibiting, consider another approach. Alternatively why doesn't the normal login work?
 
Okay, but anyway, SetWindowsHookEx() doesn't work on SYSTEM authority.
I've never thought like you, Mr. Mgetz.
Is it impossible.?
 
@A.Godnov SYSTEM doesn't have a windowstation deliberately
 
Hm... then, is there anyway to control Keyboard on SYSTEM authority.?
 
Sam
2:30 PM
Why can I find no docs on DoublePtr :S
 
@A.Godnov potentially, but why? You shouldn't do anything interactive at SYSTEM level
@Sam because it's not a thing?
 
Sam
Oh really
 
nwp
@Sam Try CTRL-clicking it in your IDE.
 
@Sam could also be using DoublePtr = std::unique_ptr<double[]>;
 
Sam
typedef double * DoublePtr;
Is this a custom name for a pointer to double?
 
2:32 PM
@Mgetz I am just developing a security system for my company.
Please help me.
How can I control the KEYBOARD on SYSTEM authority.?
Another function in there.?
 
@A.Godnov why not just use AD?
or SAMBA?
generally rule number 1 of security is don't write your own
 
We don't use Active Directory - AD, our infra is not available.
 
nwp
@Sam yes
 
@A.Godnov it's not clear what you're trying to achieve, but the question has a lot of "This looks like someone trying to do something bad"
 
Oh, my lord.!
@Mgetz Do not yell at me.!
Why do you think so.? Huh~!
 
2:38 PM
@A.Godnov I'm not? I'm just saying that the question seems like you're trying to do something either the wrong way or harmfully.
 
Sam
How do you infer he/she is yelling
@nwp Thanks
 
@A.Godnov Generally the only valid thing to do at SYSTEM level is launch a process at a lower level. In general using SYSTEM should always be avoided under all circumstances.
 
@Mgetz Thank you for your kindness.
Hm... it was a serious problem....
 
@A.Godnov if you let us know the problem you're trying to solve that would help
 
What's that.? Please....
I think no one want to help me.
Anyway, I will solve it myself.
 
2:46 PM
@A.Godnov what is this window hook designed to solve. What's the larger problem?
 
I edited most part of code, because it contains some secrets.
I will ask you understand me.

I intend to prevent PC on SYSTEM level.
But, "SetWindowsHookEx" function doesn't work on SYSTEM lv well...
This is so urgent, also I have no time.
 
@A.Godnov Prevent access at SYSTEM level? That should be taken care of already by the OS.
 
@Mgetz Thanks for your editing.
 
@A.Godnov better tags mean that people more familiar with the matter can look at it
 
3:03 PM
@Mgetz A interface for input password, and this tool have to control USB injection, CD-ROM injection, also other memorable devices.
Then have to control keyboard to prevent special actions.
 
@A.Godnov Login UI provides the former, replacing that requires unsupported modifications to the Operating system. Use Group Policy for the rest
 
@Mgetz A bargain is a bargain, I have to develop in range of requirements.
 
@A.Godnov This might be a bit forward but do you mind if I ask your first language?
 
static constexpr std::ios::openmode decode(openmode_t pmode)
		{
			std::ios::openmode mode(0);
			if (pmode & openmodes::app)
				mode |= std::ios::app;
			if (pmode & openmodes::binary)
				mode |= std::ios::binary;
			if (pmode & openmodes::trunc)
				mode |= std::ios::trunc;
			if (pmode & openmodes::ate)
				mode |= std::ios::ate;
			if (pmode & openmodes::in)
				mode |= std::ios::in;
			if (pmode & openmodes::out)
				mode |= std::ios::out;
			return mode;
		}
`std::ios::openmode mode(0);` works in MSVC but G++ gives ` error: invalid conversion from ‘int’ to ‘std::ios_base::openmode {aka std::_Ios_Openmode}’ [-fpermissive]
std::ios::openmode mode(0);
`
std::ios::openmode mode; works in MSVC if the function is not constexpr
 
@Mgetz Africans. Do you want to know more about me.?
 
3:12 PM
what are my options?
 
nwp
@Yashas Well, gcc is right. 0 is not a valid std::ios::openmode. Try if std::ios::openmode mode{}; gets rid of the warning.
 
@A.Godnov On the off chance there was a language specific stackoverflow site that might be more useful to you
 
@nwp It worked. What does {} do? I thought it would default construct object after deducing the type.
What's it called actually? I don't know what to google.
 
nwp
Value initialization.
Apparently there are issues with that, but I'm too lazy to read it right now.
They don't actually cover value initialization in the answer -.-
 
3:29 PM
warning: declaration ‘std::chrono::nanoseconds’ does not declare anything using ns_duration = typename std::chrono::nanoseconds;
I started porting code written using MSVC to G++ and I have been spammed with warnings and errors -.-
 
nwp
That is normal. VS tends to compile a lot that isn't legal.
Just have to fight through it, one error message at a time.
If you look closely the warnings usually have a point, so you are actually improving the code and not just wasting time.
 
I can't see what's wrong with: using ns_duration = typename std::chrono::nanoseconds;
removing typename fixes it
shouldn't I be using typename because there's a namespace prefix?
 
3:46 PM
@Yashas no typename is when you're literally looking for the name of the type for compiler to disambiguate from an instantiation.
because the compiler is supposed to treat anything that looks like an instantiation as an instantiation
 
doesn't std::chrono::seconds not look like one?
I think using Graph = typename std::vector<std::vector<Edge>>; needs typename for g++ IIRC.
but I can't see the difference between the two
 
@Yashas MSVC ignores typename in a lot of places it shouldn't and doesn't error sometimes when it should
 
nwp
Probably because one is a class and the other is a namespace, but I don't really know.
 
@Yashas it shouldn't it's unambiguous
AFAIK you should only ever need typename when dealing with template arguments
 
oh my, it worked without typename too
 
@Yashas now if that was in a template typename would make sense potentially but only if std was the template parameter
 
g++ doesn't give a warning for the Graph example with typename and it works both styles
 
see the link to cppref
@Yashas probably because people sprinkle typename like sugar on code even though it's completely unnecessary
the only valid use of typename is to say that the most scoped derived intentifier directly to its right is a type, not a variable
as with all things in C++ I'm probably wrong due to cornercases
 
why does the -lm flag exist?
 
nwp
4:08 PM
To be fair the X in using name = X; must always be a type, otherwise it cannot compile, so there is not really a point to having to add typename there. People are trying to get rid of it.
 
4:23 PM
Evening
 
@WilliamMariager still morning here
 
Getting eveningy here, 17:29 currently.
I had a quick question. I think it's simply, but my google skills are failing me.
I'm trying to run an external command line tool.
I've been using system("") for this, but I'm not sure it's the correct approach.
Looking for the topic on Google just gives me a bunch of results about how to make a command line tool, not how to launch one.
 
@WilliamMariager do you want the command to block further execution?
 
nwp
Depends on what you want. If all you want is to launch some tool and you are sure that tool exists in that form on all platforms that the code will run on system is fine.
 
Yeah, I need to wait for it to finish up. I don't need stdout or anything, since it has a visual interface.
This is purely for Windows.
 
nwp
4:33 PM
If you are using Qt already QProcess is a pretty easy option.
 
I don't think we are. It's a 16 year old code base.
 
nwp
If you are doing winapi stuff you have CreateProcess. If you are using boost try boost::process.
 
and if just for windows then the native CreateProcess is an option
 
How could I forget about CreateProcess
I'll give it a try.
Thanks.
 
4:47 PM
@nwp fun fact.. system and _wsystem are both wrappers around CreateProcess (with a lot of command line munging and safety code)
 
5:15 PM
@FerencRozsa Something about it is C++14 only coliru.stacked-crooked.com/a/477e4f1213c954e7
 
6:11 PM
Guys, I'm trying to write constexpr auto list = {1, 2, 3} inside a function block, and gcc says that "std::initializer_list is not a constant expression", but when I write static constexpr auto list = {1, 2, 3} it doesn't complain anymore, why I must add a "static" to my constexpr variable?
 
@LanYi better question is what is the type of list?
 
@Mgetz I think it should be std::initializer_list<int>
Hmmm, to be precise, const std::initializer_list<const int>
 
@LanYi yeah just tested that
would seem to make more sense to me to just make it a std::array
 
6:34 PM
@Mgetz Actually I don't care too much about its type, I just find very nice that I'm able to iterate over it, and another good thing is `constexpr` variables can be implicitly captured by lambda, so I could write something like
`constexpr auto list = {windowId1, windowId2, buttonIdA, buttonIdB, buttonIdC};
...
functionTable[ON_CREATE] = [](...) {
for(auto id : list) {
createWindow(id, getText(id), getRectangle(id), ...);
}
};`
but after upgrading my compiler, it won't let me write constexpr auto list = {...} anymore, I must write static constexpr to pass compilation
I'm pretty curious on why I have to do this xD
 
@LanYi it actually makes sense if you think about it, what's the main requirement of constexpr?
 
well, it must be evaluated at compile time, and it implies const
(iirc)
 
@LanYi which means it can't have automatic duration
 
nwp
It doesn't imply const anymore.
 
I believe it still does for variables, just not functions
 
nwp
6:39 PM
It's so that member functions can modify their data members even though the whole thing is constexpr.
 
Yep just verified, the implication of const always applies to declarations of objects, just not functions
 
7:33 PM
guys what topics would you say a core c++ developer needs?
 
@Permian RAII
 
does a c++ programmer just need core c++ and some knowledge of STL (contrainers and algos)
@Mgetz ok got that bit
 

« first day (682 days earlier)      last day (2154 days later) »