« first day (1043 days earlier)      last day (1813 days later) » 

3:10 AM
BYTE &vector[0] return access violation because the first value is 0. i know that byte of 0 is null terminator, is there a solution to this beside using an array byte?
 
3:41 AM
nvm restarting my pc fixed it!
 
 
2 hours later…
5:31 AM
can someone explain this line of code, i know what the code does, i need to know the name of this operation. MENUITEMTEMPLATE* mitem = (MENUITEMTEMPLATE*)MenuTemplate;
MENUITEMTEMPLATE is struct pointer and we use it to fill the data of MenuTemplate(array of bytes)
this way we can fill an array with multiple different structs with different datatypes. but what do we call this operation?
so when we instantiate mitem->mtOption with a value, this value is automatically inserted into the array. I havent seen a code like this
 
 
4 hours later…
nwp
9:08 AM
@ma1169 That's a C cast.
@ma1169 Unless there is conversion operator trickery involved this is simply undefined behavior.
 
nwp
That is straightup undefined behavior. You are not allowed to access objects that do not exist. There is no MENUITEMTEMPLATEHEADER anywhere in the code, so you cannot access it.
 
i implemented it and it worked
 
nwp
Undefined behavior is undefined. Doing exactly what you want is one manifestation of undefined behavior.
If it still works when you enable optimizations and use a different compiler is a gamble you should not make.
 
oh, I have no idea, i am using visual studio 2019, i didnt play with the settings
 
nwp
Also it's not that hard to make it defined behavior. Replacing MENUITEMTEMPLATEHEADER* mheader = (MENUITEMTEMPLATEHEADER*) milist; with MENUITEMTEMPLATEHEADER* mheader = new (milist) MENUITEMTEMPLATEHEADER; should make it guaranteed to work instead of just accidentally working.
 
10:11 AM
but why when i execute mheader->versionNumber =0; it write that in the array
it write only the value 0 in the array
 
nwp
Because thats what you wrote. You point to the array with mheader and set a value to 0 which it writes into the array. What else did you expect to happen?
 
how it knows how to map
so when i do this
mheader->versionNumber =0;
mheader->offset =0;
it write 4 bytes
how does it know where it should write these values
the iteration i mean
 
nwp
What iteration?
Iteration usually implies a loop of some kind, but there doesn't seem to be one.
 
when we do new (milist), it points to the first address of the array right? then when I run this code if I run this code mheader->versionNumber =0;mheader->offset =1; the first 4 bytes ARE 0 0 1 0, the rest are not defined
how did it now that it should write the 1 at the 3rd address of the array
 
nwp
The compiler knows what MENUITEMTEMPLATEHEADER looks like, what data members are inside of it and what the offset and size of each data member is relative to the start of the object.
The start of the object is known via the pointer mheader, so it knows the location and size of versionNumber and offset.
 
10:23 AM
aha, I see, thanks
now it make more sense to me a bit, it was really confusing to understand what was going on when i debugged part of their code, the value of were written then overwritten, thanks again
 
nwp
You can ask the compiler about the size with sizeof and if the type is a simple struct also about the offset with offsetof.
 
wow <3, i never knew offsetof exists thanks
 
10:49 AM
one last thing should I disable optimisation?
 
nwp
In debug builds usually yes, in release builds usually no.
2
Optimizations tend to hinder how much the debugger can help you, which is the only proper reason to not enable optimizations.
2
Some compilers support -Og which enables all optimizations that don't hinder debugging.
3
 
11:50 AM
@nwp there are certain items within GDI that should be UB... but are implementation defined
 
 
1 hour later…
nwp
1:23 PM
There is a way to make it work with std::tuple, but it's ugly.
 
nwp
1:35 PM
Seems to be a duplicate.
At least they fixed it in C++20. Thanks for rubber ducking.
 
 
4 hours later…
5:21 PM
i want to read follwing input in c++

3
1 2 5
4 6
7 8 9
i am sorry i know it c chat room
but i dont find room for c++
basically i want to create vector<vector<int>> vec
please can anybody help
 
you can do that with getline if you want to and then split it using any number of methods
 
nwp
int n;
while (std::cin >> n) {
    //do something with n
}
That's the normal way to read numbers in C++.
 
5:46 PM
@Mgetz i tried this pastebin.com/7DnKxSL2
but does not work
 
6:07 PM
"does not work" isn't a problem description
Also, don't include bits/stdc++
 
6:17 PM
ok thanks for reply
actually it read only first two rows
if n=3
ok i got it
i missed cin.ignore()
i need it after reading n
thanks lot for your help :)
 
7:10 PM
can you rely on end behaving like a pointer or is that undefined behavior
also back is suppose to give you a reference, is there a way to get its address?
 
@Rick end as in std::end?
because that just gives you an interator, which has a defined interface
 
correct, but if you subtract 1 from it will give you the last element
as an address
 
7:26 PM
@Rick depends on what type of iterator you get back?
 
so I have to wait until C++17 to rely on this behavior
 
@Rick no you have to ensure what you get back is a random access iterator
iterators may not be pointers
 
but the spec now guarantees vectors store their elements contiguously
This makes me quite happy as I would rather not have to write that deficit inducing iterator logic.
Make pointers great again! :-)
 
7:52 PM
If I need to store a callable object in a class Foo, is there any difference between these two?
class Foo
{
    template<class T> Foo(T&& onDestroy) : onDestroy{std::forward<T>(onDestroy)} {}
    std::function<void(int)> onDestroy;
};

class Foo
{
    template<class T> Foo(std::function<void(int)> onDestroy) : onDestroy{std::move(onDestroy)} {}
    std::function<void(int)> onDestroy;
};
 
@Rick I suspect you're probably over-complicating things and trying to over optimize
@LanYi no but if you need a finally there is already a lightweight implementation available
 
@Mgetz actually that handler should be better named onTimeout since it won't be called from destructor, but from a boost::asio::steady_timer, but thanks for the finally implementation anyway, need to use it in other places ^ ^
 
@LanYi I'd suggest just writing small RAII wrappers for most of those things
particularly if you're going to use them more than once
 
(:-O After all this time you still don't trust me not to try to tweak things.
):-P Unfortunately, in this case, it wouldn't help anyway.
 
@Rick in most release builds iterators are pointers... so yeah I don't
 
 
2 hours later…
10:00 PM
Are strands in boost::asio cheap to construct? Could I just make a bunch of different strands for each connection / socket I have?
 

« first day (1043 days earlier)      last day (1813 days later) »