« first day (1316 days earlier)      last day (1528 days later) » 

11:37 AM
@Permian my top 3 are structs, functions and RAII ☜(゚ヮ゚☜)
 
 
1 hour later…
12:38 PM
i don't know what to search in google for this: if the user enters "12cm", store that as 0.12m(eters). can somebody help me figuring out a good googlesearch?
 
parsing SI units?
 
nwp
12:58 PM
"Base unit conversion" maybe.
 
i think i found something, the boost-library boost.org/doc/libs/1_53_0/doc/html/…
thanks you two
 
 
2 hours later…
user7659542
2:40 PM
@Mgetz My understanding of volatile is that it tells the compiler to not optimize the variable in question. Meaning the software should read RAM memory every time to obtain the variable's value. And thus not read some cached value.
 
@traducerad not quite, it tells the compiler not to optimize the read/write out and to not reorder. But it can't guarantee any sort of bus lock.
nor does it ensure that the cache value isn't used
only the page tables can ensure that
or a locking mechanism
volatile has no alignment requirements so it could also be cross cacheline
(it won't usually)
 
at the end I used "find" for parsing inputs, it does what it should, altho a wrong input results in 0m(eters). here: pastebin.com/MtybzCKm
 
user7659542
@Mgetz if it is NOT optimizing out read/write ops, it is NOT caching those values, I guess. Which is what I said
 
user7659542
Unless I misinterpreted you
 
user7659542
Why do we care about bus locking here?
 
2:48 PM
@traducerad well AS-IF still applies, and we care about bus locking because people think that volatile can be used to implement atomics. Which it can't. volatile is not guaranteed to be write-through and may only be written to cache.
I'm actually not sure volatile blocks read and write reordering for anything BUT the volatile variable
 
user7659542
@Mgetz That is my point. If you are not using volatile you may be caching. If you are however using volatile, you must be "not caching" otherwise the keyword would be useless
 
@traducerad except that's wrong
cache behavior is controlled via page tables and kernel level mechanisms, volatile doesn't bypass those. It's a compiler not CPU level emit
so volatile does not bypass caching
 
user7659542
does this mean that the volatile keyword may be useless?
 
outside of kernel mode? In most cases
 
user7659542
what about firmware? Where you have interrupts
 
2:53 PM
volatile is basically an indicator to the compiler you're breaking sequential consistency
 
user7659542
the only proper way to communicate between your IRQ and the rest of you app is using volatile
 
@traducerad very useful there insofar as you have either no virtual memory or have that particular location set up as write through
 
nwp
No. The point of volatile is to say "this does not behave like memory". Maybe you have a step motor connected to the bus at some address. 5 means go, 6 means stop, reading means you ask how fast you are going. volatile int *motor = ...; *motor = 5; std::cout << *motor; *motor = 6; makes total sense in that context. But if you think there is an int there in memory then this makes no sense.
 
most modern CPUs allow you to configure cache behavior
 
nwp
If you mix volatile with multithreading you're doing a weird hack that may or may not do what you want. It probably won't.
 
2:55 PM
pretty much that in a nutshell
hence volatile doesn't have much use outside of driver code
because of what @nwp said
regardless even with that you still need to have the CPU configured to allow volatile to work correctly for that address
 
user7659542
@nwp Hah. I recently ran in a very funny situation in that regard. So I used to work at a company where they were using an extremely old version of gcc, ie 3.1
 
user7659542
meaning atomic_t and all its friends were undefined
 
user7659542
our application was multithreaded and a lot of data was shared accross all those threads
 
user7659542
thread 2 would wait untill thread 1 generated the data for thread 2
 
user7659542
so you had no other choice than using volatile
 
user7659542
3:09 PM
almost everything was declared as being volatile
 
nwp
Sounds like not fun. I imagine stuff breaking randomly and nobody really knows why and just throws volatile around until it stops being broken.
 
user7659542
@nwp Hahahaha. Yes, exactly
 
user7659542
you sound like an engineer who has worked there
 
nwp
Currently I'm sending code into the void with a debugger that doesn't give me a correct stack trace and SDKs and technologies I don't understand. I'm having fun of my own.
 
user7659542
What I managed to distilate out of this volatile discussion. Is that that keyword is usually only usefull when writing low level software (firmware, device drivers, ...). It makes sure your read/write operations to (some) memory are not optimized out.
 
user7659542
3:21 PM
And it is useless in userspace because volatile has an inherent flaw, which is that you may actually be accessing cache memory and not actual RAM. Which is precisely why it is useless in a multithreaded environment as thread has its own cache
 
user7659542
Did I get that right?
 
user7659542
not sure though why it suddenly become usefull when you are dealing with firmware or device drivers
 
nwp
Because firmware and device drivers deal with things that are not memory, such as hardware devices.
The reality is complicated of course. Interrupt service routines use volatile because "when used correctly" it will give you a correct enough amount of optimization inhibition.
 
user7659542
ok, but at the end of the day that variable which is modified by an IRQ is still stored in memory
 
nwp
And pre C11/C++11 people used it for threads because they didn't have anything else even though technically threads didn't exist back then.
 
user7659542
3:25 PM
so you care about that memory region being updated properly not the device itself
 
nwp
Yeah, the IRQ is a weird edge case where you can make a hack with volatile mostly work.
 
user7659542
@nwp atomic_t was already a thing before C11
 
user7659542
I am kinda worried now. I don t know what I ll answer on future job interviews
 
nwp
As a non-standard compiler extension maybe. Which did who knows what.
And there actually are situations where volatile is useful in multithreading, but the examples are complicated, mostly because multithreading is involved.
 
user7659542
Every job interview I had, I was asked to explain what it does
 
nwp
3:28 PM
"Assume this points to a device, not to memory and therefore don't do tricks like eliminating dead writes" might be a decent answer.
"An old keyword you should forget until you are making drivers" is another.
Though honestly if they ask you about volatile then they are not a good place to work at. The ideal answer I'd want to hear is "Dunnow, I never use it".
 
user7659542
I write firmware and embedded software for a living. So I think I will never escape from this question
 
user7659542
I ll get it every single time
 
nwp
Oh. Well yeah, in embedded you can't escape it.
 
user7659542
just like "what is typedef?" The number of people who answer "it allows you to create a new type" is horryfying...
 
nwp
Well, you can still ask them what exactly they mean and show them an example like typedef int integer; and ask if it makes a difference if you use int or integer. Sometimes people mean the right thing but it comes out wrong.
 
user7659542
3:36 PM
meh
 
user7659542
Less competition for me. As a freelancer, I feel like that type of answers is good for me. Unless I have to hire them
 
what is the point of a redeclaration in c++? I don't understand why I would want such "feature".
 
nwp
You mean like why void f(); void f(); is legal or something else?
Or something like int i; { int i; }?
 
yeah this
void f(), void f()
 
nwp
Not sure. There probably isn't a good reason to make that an error either and the default is to do nothing.
You could skip header guards if you only declare functions, but that's a really weak argument.
 
3:55 PM
Uhm i've a variable naming question but it's not strictly c++ related, can i still ask here?
I'm working on a class that runs AngelScript scripts in a second thread. The thread only exists while a script is running, so if it's paused the thread stops existing and another one is created when execution resumes.
I have an atomic boolean i'm calling "running" to check if the script is currently running, but i need a name for another atomic bool that would define that the script execution did start, but isn't finished, regardless of it being running or not...
I just can't find the right word, and not being native english doesn't help
 
Does it make sense to make constexpr functions also static?
 
what do you think of the same variable name in different functions like this:
int function1()
{
    double x=2.7;
    //do alot
    return something where x is involved;
}

int function2()
{
    double x= 3.142;
    //do alot
    return something where x is involved;
}
 
@SAJW It's a local variable; as long as x means something in the context of the function, I don't see any issue there.
 
ah ok, I was just wondering.
 
Sure :)
 
4:29 PM
mmh, i wonder this: how could I detect if a function is never called and therefore superfluous? the compiler doesn't warn there.
 
@SAJW You need a static analysis tool such as cppcheck
 
4:42 PM
@Vaillancourt thanks
 
5:18 PM
how can I excercise one function for one purpose? I think I wrote a monster-function actually, but how should I rewrite it? see function "tutorial" here: pastebin.com/YunPyuDQ can you make an example where you split a function sensibly, so i can understand what is meant?
 
5:30 PM
A possibility would be to extract the "convert a string to the numerical value", so you supply the function a string, and it tries to parse it to a numerical value. And so you split the "reading" part from the "converting" part.
 
5:49 PM
@Vaillancourt like this? pastebin.com/bR7VH9cP note the (new) function user_input().
 
@SAJW like this:
bool parseToMeter( const std::string& raw_number, double& parsedNumber )
{
    const double meter=1;
    const double inch=0.0254;
    const double centimeter=0.01;
    const double feet=0.3048;

    parsedNumber=0;
    bool hasFoundAndParsed = false;

    if (raw_number.find("cm")!=std::string::npos)
    {
        parsedNumber=std::stod(raw_number)*centimeter;
        hasFoundAndParsed = true;
    }
    else if(raw_number.find("m")!=std::string::npos)
    {
        parsedNumber=std::stod(raw_number)*meter;
(un-tested so there might be syntax error, but you get the idea)
 
sorry for the silly question but why of type bool?
 
@SAJW You need to know if the parsing succeeded. If you've been able to parse the input, you know you can use the number safely.
 
6:05 PM
strangely enough this compiles, but I don't know why. I can't see where "number" gets a new value, but I see it is written in the vector.
 
@SAJW inside the function it's called parsedNumber
they both point to the same value, since it's passed by reference
 
oh, I didn't knew that!
so actions on a variable inside a function can change the global variabel if done this way?
 
@SAJW Yes, when you pass a variable by reference, it's going to act on the value of the variable passed to the function.
 
@traducerad unless you get a pointer from the OS to the device? yes
 
6:40 PM
Hey guys. Had a quick question so thought I'd just ask here. How many bytes will this allocate? Would it be 16*10 =140?

struct Example
{
    double d;
    char c1;
    float f;
    char c2;
};

Example* example = new Example[10];
 
@AaySquare depends on your platform, it'll be sizeof(Example)*10 most likely
 
Ah okay. I quickly tested by printing cout << sizeof(Example) * 10 and it printed 240. Trying to understand how it got to that number though. Cause looking at the members of struct, the bytes add up to 14, not 24 though.
 
is this behavior a feature of C++ or a bug? if there are 2 "std::cin"s and i type "hello world" in the first cin, the "hello" is used as the first cin, and "world" as the second. But what if i actually want to type in "hello world" in the first cin, how am I able to do that?
 
@SAJW that's what stuff like std::getline is for, by default istream>>(string) seperates words
 
oh, never mind then
 
6:53 PM
@AaySquare padding works in misterious ways, if you want to see what got padded to where you can use the archaic offsetof :P
 
@PeterT Sorry I don't understand what you mean by "use the archaic offsetof." :P
 
if you check offsetof(Example, d), offsetof(Example, c1) , etc. you can see how much padding got added
it's just an old construct from C that's almost deprecated at this point in C++
my guess would be:
DDDD DDDD
XXXC FFFF
XXXC XXXX
where D=double, C=char, F=float, X= padding
the final 4 padding bytes are there to not misalign the double if it's packed into an array and they're one after the other
 
7:13 PM
I see... Thanks for that! :)
 
7:52 PM
Hello guys
I am using ZwOpenKey so I can open the RUN path from registry as for later on i want my coffee program to run with the windows
But unfortunately, I keep receiving an error in the function Access violation writing location 0x00000000.
Anyone there?
 
Just ask and lurkers will answer. Those who are here most likely can't help you, and those lurking that can help you are not here. If anyone can and want to help you they will.
 
you are lovely :))
I am using WinAPI
NTSTATUS OpenKey(PHANDLE hKey, PWCHAR KeyName, ACCESS_MASK MaskType)
{
    UNICODE_STRING strKeyName = { 0 };
    RtlInitUnicodeString(&strKeyName, KeyName);

    POBJECT_ATTRIBUTES KeyAttributes = { 0 };

    (KeyAttributes)->Length = sizeof(OBJECT_ATTRIBUTES);
    (KeyAttributes)->RootDirectory = NULL;
    (KeyAttributes)->Attributes = OBJ_CASE_INSENSITIVE;
    (KeyAttributes)->ObjectName = &strKeyName;
    (KeyAttributes)->SecurityDescriptor = NULL;
    (KeyAttributes)->SecurityQualityOfService = NULL;
And I am calling the function like this:
WCHAR RegistryString[MAX_PATH];
if (wsprintfW(RegistryString, TEXT("\\REGISTRY\\USER\\SOFTWARE\\MICROSOFT\\WINDOWS\\CURRENTVERSION\\RUN")) != NULL)
{
    HANDLE RegKey = NULL;
    if (OpenKey(&RegKey, RegistryString, KEY_WRITE) == STATUS_SUCCESS)
    {
        // show messagebox
    }
{
 
8:08 PM
And who's giving you the access violation exception? Typically, that's because you write to a null value.
(Also, I don't know anything about that function, but maybe others do.)
 
Here is giving me the error:

(KeyAttributes)->RootDirectory = NULL;
 
Looks like POBJECT_ATTRIBUTES is a pointer to an OBJECT_ATTRIBUTES. So KeyAttributes is effectively null and by assigning values to its member, you're accessing 0x000000000.
You might want to initialize KeyAttributes to something (other than NULL/0). I wouldn't know what would be the best way to do it in your context, though.
Ah!
> Pointer to an OBJECT_ATTRIBUTES structure that specifies the object name and other attributes. Use InitializeObjectAttributes to initialize this structure. If the caller is not running in a system thread context, it must set the OBJ_KERNEL_HANDLE attribute when it calls InitializeObjectAttributes.
 
Oh.
Let me check.
 
 
3 hours later…
11:37 PM
@Vaillancourt should "const double inch =0.0254;" and all other conversionvalues be "private"?
 

« first day (1316 days earlier)      last day (1528 days later) »