Anonymous
You can redirect stderr as well
i know, but by default it will not be
Anonymous
unless you explicitly use 2>&1 to redirect stderr to stdout (depending on the shell)
Anonymous
i know, but by default it will not be
You mean you dont have to specify 1 when you redirect stdout but you have to specifically mention the descriptor Id 2 when you redirect stderr. So that is the difference you are highlighting?
Anonymous
i find it usefull to prog > /dev/null # causes only stderr to be outputted
Anonymous
https://mywiki.wooledge.org/BashFAQ/055 this may help
Anonymous
https://mywiki.wooledge.org/BashFAQ/055 this may help
I know already what is mentioned on that page. I was just curious if you think mentioning the descriptor id for stderr when you want to redirect it is a difference worth mentioning when discussing about cerr and cout. I just dont see the connection.
Anonymous
well, when all output is in stdout, it is difficult to filter error messages since those are typically expected to be printed on stderr and not stdout, UNLESS all error messages are explicitly PREFIXED in the program eg ERROR: some_message
Anonymous
https://gist.github.com/289f2e4c1741042f2d7af34234885509 would this be exception safe?
It still is not. Say while copying, if the element in the middle throws an exception while copying, what will be the state of your array?
Anonymous
i am not sure 🙁
Anonymous
You would have copied half the elements and the remaining half will be the old elements. So basically you have changed the state of the array and left it in an invalidated state. In other words your code is not exception safe. Try this as an example. Create a class and maintain a static counter in it that keeps track of the number of copy assignment calls. Throw an exception on every 100th call. Create a std::vector and store elements of this class in it. Do a lot of copying and push_backs. Check the status of your vector after the exception is thrown. It will be the same as before.
Anonymous
if an array copies itself, can it still throw an exception?
Anonymous
Anonymous
like makes a backup of itself
Anonymous
eg Array<T> tmp = *this;
Anonymous
like makes a backup of itself
If the copying of the underlying element can throw then the array copying will throw as well unless it can catch it and handle it somehow
Anonymous
ooo what if i copy it INTO a new array?
Anonymous
ooo what if i copy it INTO a new array?
Now you are getting the right ideas. Yes you should do that. Copy into a new array and only if the copy is successful make the member point to the new array. If the copy failed then leave the old data pointer as it is. That will be exception safe code.
Anonymous
eg template <typename U> void copyFrom(Array<U> & array) { static_assert(std::is_copy_constructible<T>::value && std::is_convertible<U &&, T>::value, "T must be copy constructible and convertible from U"); Array<T> tmp; tmp.resize(array.capacity); if (std::is_same<U, T>::value) { memcpy(tmp.data, array.data, sizeof(T) * capacity); } else { for (int i = 0; i < capacity; ++i) { tmp.data[i] = static_cast<T>(array.data[i]); } } delete[] data; data = tmp.data; tmp.data = nullptr; capacity = tmp.capacity; tmp.capacity = 0; }
Anonymous
should i change my move constructor to this? Array(Array<T> && other) { delete[] data; data = other.data; other.data = nullptr; capacity = other.capacity; other.capacity = 0; }
Anonymous
should i change my move constructor to this? Array(Array<T> && other) { delete[] data; data = other.data; other.data = nullptr; capacity = other.capacity; other.capacity = 0; }
No. You shouldnt do this. What if delete failed and threw an exception (destructors shouldnt throw but you should account for them as well)? You should instead swap the data pointers and capacity of *this and other. other being a rvalue reference will automatically call delete on the old data pointer after it goes out of scope and thus delete your old data
Anonymous
If I do s.erase(s.begin()) Where s is a set of pair of (int, T), will ONLY the first pair element, of the type (int, T) be erased?
Anonymous
how do reallocate a pointer without calling the destructor on its elements? T tmp[capacity]; memcpy(tmp, data, sizeof(T) * capacity); delete[] data; data = new T[newCapacity]; memset(data, 0, sizeof(T) * newCapacity); memcpy(data, tmp, sizeof(T) * capacity);
Ammar
how do reallocate a pointer without calling the destructor on its elements? T tmp[capacity]; memcpy(tmp, data, sizeof(T) * capacity); delete[] data; data = new T[newCapacity]; memset(data, 0, sizeof(T) * newCapacity); memcpy(data, tmp, sizeof(T) * capacity);
There is no way to avoid destructor being called when you delete[]. The option for this case is using malloc() / realloc() instead of new[] / delete[]. However, malloc() won't call the constructor, so it doesn't really work well with C++ object lifetime and construction.
Anonymous
even if i cast it to void* ?
Ammar
even if i cast it to void* ?
Oh, surely the destructor won't be called for void pointer. But isn't it undefined behavior?
Ammar
Class may not have a constructor.
Ammar
how do reallocate a pointer without calling the destructor on its elements? T tmp[capacity]; memcpy(tmp, data, sizeof(T) * capacity); delete[] data; data = new T[newCapacity]; memset(data, 0, sizeof(T) * newCapacity); memcpy(data, tmp, sizeof(T) * capacity);
How would you avoid the constructor being called for T tmp[capacity];? Do you want to call the constructor capacity times for each reallocation? And that VLA isn't C++ standard, ISO C++ forbids VLA.
Anonymous
how do i detect a private constructor?
Ammar
how do i detect a private constructor?
I don't know, but I think there must be at least one public constructor if you have a private constructor. Otherwise the class can't be initialized.
Anonymous
eg https://coliru.stacked-crooked.com/a/e225d867e2a74fd9
Ammar
eg https://coliru.stacked-crooked.com/a/e225d867e2a74fd9
What's the purpose of detecting private constructor? I don't really understand your goal.
Anonymous
What's the purpose of detecting private constructor? I don't really understand your goal.
to provide a compile-time error stating that the constructor must be public
Anonymous
also to use it as a flag for detecting a non class value such as all C types (there are A LOT of them, some compiler-defined such as __*, and in some arch's, 80-bit float types long double )
Ammar
to provide a compile-time error stating that the constructor must be public
So your goal is to prohibit any private constructor? It is not allowed to have one even if it provides public constructor(s)?
Anonymous
eg T(); will be unable to be called due to it being private
Pavel
also to use it as a flag for detecting a non class value such as all C types (there are A LOT of them, some compiler-defined such as __*, and in some arch's, 80-bit float types long double )
Not sure that checking the constructor would help with this, you probably need to check for is_standard_layout and is_trivial for checking that the class is POD type.
Anonymous
hmm ok
Pavel
yes, since it cannot be default constructed
Why not just check for ability of being default-constructed
Anonymous
cant use is_trivial since it returns true for struct { int m; } which by default has a constructor
Pavel
cant use is_trivial since it returns true for struct { int m; } which by default has a constructor
Ah, ok, I thought you want to discard any c type. What about int, should it pass or fail the check? Can you give some examples of types that should pass your check, and types that should fail?
Anonymous
https://coliru.stacked-crooked.com/a/4bdb8b12ba12ce48
Anonymous
no
Anonymous
for int, and A there should be some other check that can differientiate betwen them but i do not know what
Anonymous
is_pod returns true for both
Anonymous
hmmm is_class seems to work :)
Anonymous
yay https://coliru.stacked-crooked.com/a/c2461e74af70f2fc
Anonymous
is it possible for a class to have a private destructor? eg is_class to return true and is_destructuble to return false?
Pavel
is it possible for a class to have a private destructor? eg is_class to return true and is_destructuble to return false?
I always thought is_destructable only applicable to classes (it's always true for basic types). But I'm not sure, never used it myself
Anonymous
newCapacity < capacity returns true if newCapacity is less than capacity right? eg 5 < 8
Anonymous
eg if (newCapacity < capacity) { for (int i = newCapacity-1; i > newCapacity-1; i--) { data[i].~T(); } } realloc(data, sizeof(T) * newCapacity); if (newCapacity > capacity) { if (std::is_class<T>::value) { for (int i = capacity; i < newCapacity; ++i) { data[i].T(); } } }
Ammar
newCapacity < capacity returns true if newCapacity is less than capacity right? eg 5 < 8
Of course, why doubt about it? Unless they both are object whose < operator gets overloaded, then it may behave something else.
Ammar
Using old realloc()'ed pointer could be Use After Free.
Ammar
Plus null check, because realloc() doesn't throw an exception when it fails.
Ammar
T *tmp = realloc(data, sizeof(T) * newCapacity); if (tmp == nullptr) { // handle error here // NOTE: `data` hasn't been freed at this point. } data = tmp;
Ammar
Something like that.
Ammar
You may need to cast the realloc() return value to T * though.
Kaddy
https://imgur.com/a/w5MFnhu Can someone please help with this problem. I want a sophisticated algorithm for this. What I've tried till now is the brute force algorithm (it works but is very inefficient and gives TLE for larger cases). I iterated over all permutations of numbers from 1 to n in this and found minimum cost permutation which is obviously not a very elegant method. Can someone please suggest a better method for the same?
Kaddy
A hint is sufficient 😅 I want to finish the thing on my own
The Curious Cat
Why is ">c99 -Wall..." returning "invalid argument `all' to -W" in macos terminal ?