« first day (1363 days earlier)      last day (1490 days later) » 

1:54 AM
@Ron Its a good way to avoid typos. There are a few widget kits and libraries where you need to cast a lot. Qt is one of them. So in their case they have a wrapper around dynamic_cast, riptutorial.com/qt/example/22798/qobject-cast
Widget kits and games are among the few times OOP makes sense.
 
 
6 hours later…
8:08 AM
Ok so I got a question about virtualization in intel cpus:
When the cpu gets a clock interrupt (assuming on guest execution) it causes the cpu to save the state of the processor to VMCS struct. Got it, now how does the guest while execution gets clock interrupts (like there need to be a clock interrupt for context switching in the guest and I don’t think the hypervisor sets an event and then does vmenter every clock interrupt) does the guest gets Different clock interrupt from the APICv? How does it work...
Or is this something else?
(And btw cpp templates are more difficult the operation systems internals.)
 
 
5 hours later…
12:56 PM
When we erase a string object with .erase function, does all iterators of that object initialized beforehand get moved in that object ?
Get moved meaning referring to another character
 
They get invalided, that just means "you shouldn't use them anymore"t
there's no guarantee about what does happen when you do use them anyway
 
gotcha
@PeterT does the same rule applies to other containers ?
and if there are functions that works with iterators on which we could apply the same rule again ?
 
all containers have their own rules about what does and what doesn't invalidate iterators
for example, inserting an element can invalidate other iterators into a string or vector, but all the iterators in a map or list are fine
 
1:16 PM
woah so it's much more complex than I thought
Would be much more convienient if there was a general rule/s of some kind , at least for me
well than
Is it worth to use iterators instead of indexing string when we have to pay so much attention when using iterators ?
 
user6461957
Iterators are making life arguably easier especially when you consider doing it via pointer-based indexing. I bet anyone who did some pointer arithmetic in their lifetimes would agree with that sentiment.
 
Does pointer based indexing include s[3] ? where s is a string for example
 
user6461957
I would argue yes. It is simply another way of writing it. *(s + 3) -- assuming that s is an int ptr.
 
s[i] to be exact
aham
Well I wouldn't know , but it still seems to me that using iterators require way more attention on things like mentioned here, invalidating iterators.
That practically means that whenever I use a dubious function, I should reinitialize or create new iterators that were defined beforehand.
And as I'm kinda beginner in C++ , I still don't know what could or couldn't be a dubious function.
 
user6461957
What do you mean with "dubious function"?

Though you should generally take my argument with a grain of salt. You should listen to other minds as well. People such as PeterT, Mgetz et al. are way more skilled than I am.
This is just my opinion. There are other well thought out opinions as well.
 
1:29 PM
int sum(vector<int> arr, int n)
{
    int sum = 0; // initialize sum

    // Iterate through all elements
    // and add them to sum
    for (int i = 0; i < n; i++)
    sum += arr[i];

    return sum;
}

// Complete the miniMaxSum function below.
void miniMaxSum(vector<int> arr) {

    int arr_len = sizeof(arr)/sizeof(arr[0]);

    int smallest = arr[0];
    for(int i=0; i<arr_len; i++) {
        if(smallest>=arr[i]) {
            smallest=arr[i];
        }
    }

    int biggest = arr[0];
    for(int i=0; i<arr_len; i++) {
 
Okey , but we can still argue :) with dubious function I mean every function that may invalidate your iterators
 
why is smallest in second function always 0?
 
user6461957
@TheGarrus

A couple of things for consideration:
- I would add some identification right after the for loop.
- you should consider using arr.size() instead. Use the member functions provided by std::vector.
- you could have used a for-each loop since std::vector is a iterable type
 
user6461957
@TheGarrus


int sum(std::vector<int> &v)
{
    int sum = 0;
    for (auto it = v.begin(); it != v.end(); ++it)
        sum += *it;
    return sum;
}
 
user6461957
1:45 PM
Also consider using a debugger. If you are a beginner and you struggle with gdb I would argue that most IDEs such as Visual Studio C++ provide an excellent debugger and are intuitive enough that even a beginner could follow.
 
user6461957
I am not quite sure what miniMaxSum is supposed to compute, but try to incorporate std::vector's member functions and a debugger and you should be fine. :)
 
user6461957
You can also use std::sort (by including <algorithm>). And sort it, then get the biggest or smallest elements that way. ;)
 
user6461957
@domocar1 Sounds reasonable to me. :)
 
So what do you think ?
about this
"That practically means that whenever I use a dubious function, I should reinitialize or create new iterators that were defined beforehand."
 
user6461957
You might as well copy them.
 
user6461957
1:59 PM
(Which is expensive, but still it is safe nevertheless.)
 
user6461957
Most containers have copy ctors implemented.
 
Okay yeah , forgot to mention that. But the point was, what if it is a hefty code with loads of iterators ?
That sounds troublesome to me
Sure with 1 or 2 iterators it shouldn't be a problem
 
Thanks man
 
user6461957
@domocar1
Consider those rules maybe?:


https://stackoverflow.com/questions/6438086/iterator-invalidation-rules
 
user6461957
@TheGarrus Your welcome!
 
2:03 PM
@d03 bingo ! That was exactly what I was looking for. Thanks :)
 
user6461957
:)
 
2:22 PM
Imagine I have an iterator first referring to some element in a string and a index that is strictly larger that what would have been an index of that iterator. For example:
string s="House";
iterator si refers to 'o' while index i = s[3]
What would be the most elegant way to do something like this:
s.erase(si,i);
I clearly cannot write that cuz si is iterator and i is an index
But I'm looking for a neat way to convert si to a number or other way around , or yet there is a turnaround
Tried using std::advance but that requires too much lines of code
 
user6461957
That number that you want to retrieve is surely a number that is dependent on the encoding of a character. (If you want to retrieve a number from a string or sub-string, then you should probably create a hash function and do it that way.)

Why not something like this?:

auto i = s.find_first_of('o');
auto j = s.find_first_of('s');

s.erase(i,j);
 
I believe I found what I was looking for
s.erase(si,si+i);
Couldn't believe it took me so long
 
user6461957
2:38 PM
I think I probably misunderstood your question anyway. xD
 
haha nevermind , thanks anyway
 
 
1 hour later…
3:48 PM
template <typename ValueT>
struct association_visitor{
    ValueT _value;

    association_visitor(ValueT def=ValueT()): _value(def){}
    void operator()(ValueT value){
        _value = value;
    }
    template <typename T>
    void operator()(const typename std::enable_if<!std::is_same<ValueT, T>::value, T>::type& /*value*/){}
    ValueT value() const{
        return _value;
    }
};
I want the operator()() to work for all datatypes
But this is not working
I want something like
udho::detail::association_visitor<int> visitor;
visitor(24);
visitor(std::string("Hello")));
But the no operation operator()() which is supposed to take all arguments except ValueT is giving compilation error.
I suppose it is a very silly problem. Thats why I am asking in the chat.
 
Hello
can anyone help me with a small task i am trying to figure out?
 
user6461957
Go ahead. I'll try. :)
 
yeey, let me try to explain
 
7 messages moved from Lounge<C++>
 
4:05 PM
I am trying to output an image file using this code
but it always outputs 5632 KB
this values comes from ReturnedBytes
HANDLE imageFile = CreateFileA("image.png", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (imageFile != INVALID_HANDLE_VALUE)
{
    DWORD imageSize = GetFileSize(imageFile, 0);
    if (imageSize > 0)
    {
        PBYTE imageBuffer = PBYTE(LocalAlloc(LPTR, imageSize));
        if (imageBuffer != ERROR)
        {
            DWORD ReturnedBytes, Unused, FinalCompressedBufferSize;
            BOOL bRet = ReadFile(imageFile, imageBuffer, imageSize, &ReturnedBytes, NULL);
@d03 can you take a look?
 
user6461957
What should ReturnedBytes' value be?
 
user6461957
imageSize?
 
one second
 
user6461957
(This looks like the Windows API to me.)
 
yes i am trying to use them
so the size should be 71168 kb
in the last line of imageSize
is equal to 71168
but then Unused is equal to that too
however the size wrote is just 5632
 

« first day (1363 days earlier)      last day (1490 days later) »