Anonymous
Anonymous
Anonymous
Anonymous
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
Anonymous
Anonymous
Anonymous
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
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
Anonymous
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);
Anonymous
Anonymous
Anonymous
Anonymous
Anonymous
even if i cast it to void* ?
Ammar
Class may not have a constructor.
Ammar
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
Pavel
Ammar
Anonymous
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 )
Anonymous
Anonymous
eg T(); will be unable to be called due to it being private
Pavel
Anonymous
hmm ok
Anonymous
cant use is_trivial since it returns true for struct { int m; } which by default has a constructor
Anonymous
https://coliru.stacked-crooked.com/a/4bdb8b12ba12ce48
Pavel
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?
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
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
Anonymous
The Curious Cat
Why is ">c99 -Wall..." returning "invalid argument `all' to -W" in macos terminal ?