« first day (1424 days earlier)      last day (1434 days later) » 

6:14 AM
ideone.com/1g9WDj wondering why it is crashing on my system...
 
6:44 AM
valgrind is complaining ideone.com/VmwHcN with the fixed code.
 
7:06 AM
you're still allocating with "::operator new" but deleting with delete[]
 
 
5 hours later…
11:37 AM
Will realloc ever return NULL if the memory specified is being shrunk and not expanded?
=============================================

Is it better to use `abort()` or `exit(EXIT_FAILURE /* -1? */)` to terminate the program when an "error" occurs?

The user defines the case for the "error" and throws either one of these two functions.
 
nwp
@Lapys It looks like the standard doesn't say, so implementations that do that are allowed but you will probably never see that in practice.
 
Which makes it non-standard to rely on realloc not returning NULL when shrinking memory?
 
nwp
It can cause undefined behavior, but it's not ill-formed. In my opinion it's equivalent to if (argc == 3) { cause_ub(); }. It may still be useful and the standard doesn't say you can't do that.
@Lapys Looking at the documentation for exit and abort it looks like exit does cleanup like flushing output buffers while abort does not, so I'd prefer exit.
If you want to just kill the program immediately without any further actions like trying to avoid getting stuck in an atexit function then abort would be appropriate. I'm not sure why you'd want that, but there probably is a use-case for that.
 
When an std::exception is thrown, does the error object invoke abort or exit?
 
nwp
I remember it invoking std::terminate which ... I forgot what that does besides exiting the program.
It calls abort unless you set your own handler.
 
11:52 AM
Alright, thanks @nwp
 
 
2 hours later…
1:48 PM
Hi, i started to convert a C# project to C++ some time ago. Dont know why, but i had to.
I was wondering if there is some kind of equivalent to C# public bool equals(Object obj)
 
operator== overloading
or spacehip operator overloading if you want to be fancy
or do you mean comparing the address?
 
i have this C# code:

bool equals(Object obj)
		{
			if ((obj == nullptr))
			{
				return false;
			}

			Coords coords = ((Coords)(obj));
			return ((this->x == coords.getX())
				&& (this->y == coords.getY()));
		}
 
yeah looks like a case for overloading ==
 
new to me
 
bool Coords::operator ==(const Coords& other){return x==other.x && y==other.y;}
or I guess better as a free function
bool operator==(const Coords& a,const Coords& b){return a.x==b.x && a.y==b.y;}
 
2:00 PM
thanks, it looks to be working
 
2:59 PM
if i have a number x what is the best way to find the minimum number of 1s 2s and 3s which sum to x?
 
nwp
Divide by 3 and modulo 3 should get you most of the way there.
 
omg i had 3 while loops in a while loop lol
 
3:19 PM
does anyone here do codeforces?
 
 
2 hours later…
4:50 PM
Quick question, what is the correct way in c++11 to do this

const std::vector<std::string> A{"elem1", "elem2"};
std::vector<char*> B = A.copy();

I want to get rid of the const and need c strings
 
nwp
std::transform or similar.
"Getting rid of the const" can backfire.
 
yes, I'd like to get clean copies of all objects, not use const_cast if possible
so best way to do it is just loop over the const strings, get copies on the heap and store the pointers in the vector?
 
nwp
Why would you do that? Why not just auto B = A;?
 
because I have a C library in a C++ project, and the C library expects a non-const-char**. Not much I can do about that. I want to call it like this :

libraryfunction(B.data());

for that I need B to be a non const vector of char*
 
nwp
5:05 PM
Are you sure it expects an array of strings and not just a pointer to a string that it sets?
Also will libraryfunction write to the strings?
 
yes it will only read from the strings as of now, they are just lables to be added onto images. however I'd rather respect const correctness than use const_cast, so I'd like to give it a full copy
 
nwp
That'll be a huge pain. You'll have to dynamically allocate and then free the memory again.
I'd almost recommend just copying the vector and making a std::vector<char *> via std::transform that points to the copied strings. That way at least you don't need to screw around with memory management.
 
this is what I got, but couldn't get it to work yet:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstring>

int main()
{
const std::vector<std::string> strings{"one","two"};
std::vector<char*> B;
B.reserve(strings.size());
std::transform(strings.begin(), strings.end(), [](auto s){char* tmp = new char[s.length()+1]; strcpy(tmp, s.data()); return tmp;});

return 0;
}
i think it'd be easier in a for loop
 
nwp
There is an iterator missing. There needs to be std::back_inserter(B) somewhere in there.
 
cause I need to transform B but want to iterate over A
 
nwp
5:14 PM
The first version of std::transform. You just need to insert the back_inserter before the lambda and it should be good.
Alternatively you can use std::begin(B) and replace the B.reserve with B.resize.
 
hmm, still getting the same error. can you show me the correct transform call?
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstring>

int main()
{
const std::vector<std::string> strings{"one","two"};
std::vector<char*> B;
B.reserve(strings.size());
std::transform(strings.begin(), strings.end(),std::back_inserter(B), [](auto s){char* tmp = new char[s.length()+1]; strcpy(tmp, s.data()); return tmp;});

for(auto s : B) std::cout << s << std::endl;
return 0;
}

got it forget the pointer to end
thx for the help!
 
 
5 hours later…
Sho
10:28 PM
hum ^^ i got a problem that mixes inclusion problems and polymorphism ones
in my case, I want collisions between Circle object and Square objects, childs of Shape class
that collision is dealt with by a member function
here's it's declaration in the Circle class:
void Circle::resolveStatic(Shape &other);
well, at least that's what I would like it to be, because this way we have no way of knowing what shape Shape truly is
so, I thought of calling
other.resolveStatic(*this);
of course, if we didn't overload resolveStatic to take other types, the problem wouldn't change
however... I can't overload it, for a very dumb reason
other (in the first call) is a shape
so I would need to declare the overloaded functions in the Shape class
but it take it's childs as parameters, which are defined after
*takes
 
Sho
sorry for the trouble, I changed my organisation, got Shape to be purely virtual, so now it accepts to take undefined (but declared) classes as parameter
 

« first day (1424 days earlier)      last day (1434 days later) »