« first day (889 days earlier)      last day (1964 days later) » 

Zoe
1:55 PM
So I have some (horrible) code: paste.ofcode.org/iewJR8G2V3WEM95gB5uCpV - it seems to stop printing after the second call to the dump method. Using GDB, it's suddenly not reproducible (it prints as expected), but that's with breakpoints. I'm assuming I'm messing up some logic somewhere. Using debugging mode also shows a warning for accessing a pointer that's not in the allocated memory. Probably missing something obvious, but I can't find what
std::cout.flush() makes no difference
 
@Zoe smells like undefined behavior. But I haven't had a chance to look yet
 
Zoe
No rush ^^ But probably. The code isn't exactly the best
newlines or explicitly flushing should fix any buffering-related issues, so it's most likely not that
 
2:28 PM
note avoid including cpp files like headers
it's really really bad practice in general
    void addX(int add){
		(*this->x) += add;
    }
is undefined behavior
the compiler has no requirements beyond that point
just do this->x += add;
heck I'm surprised that even compiled
I'd turn up warnings and errors
 
@Zoe The code contains undefined behaviour because you're creating pointers to temporaries in the constructor. You should make MyClass not use pointers and instead just make copies.
 
Zoe
Had that originally, still resulted in the same problem. I'll change it back though
@Mgetz yeah, I know. I haven't started with headers yet though (gonna start doing that when I have a relatively basic understanding of the language itself)
 
2:44 PM
for what it's worth this is undefined behavior too:
~MyClass() {
		// This is a destructor.
		// C++ doesn't have a garbage collector, so you do it yourself.
		//
		// delete &pointer;
		if(x) delete x;
		if(y) delete y;
		if(z) delete z;
	}
@Zoe use RAII instead of deleteing manually
you don't need delete at all in that code. Having it is actually creating issues.
int *x;
	bool *y;
	std::string *z;
why are those pointers? you don't need them to be
remove the pointers and your code should be fine after you remove the destructor
Assuming you did dynamically allocate x or any of your other member variables... you don't need to check for nullptr before deleting. It's a noop. That said if you do need dynamic allocation use std::unique_ptr instead of writing your own destructor.
 
@Zoe MyClass is utterly broken
firstly because you store an address to a local variable by doing this->x = &x;
secondly because you use delete on something you didn't new yourself
(though: I'm not telling you to use new. I'm telling you to not use delete)
thirdly because you don't conform to Rule of Three
if(x) delete x; is also redundant because delete does nothing if x is null
The proper solution is to not store pointers here
"C++ doesn't have a garbage collector, so you do it yourself." is only true if you subject yourself to manual memory management, like most universities which can't be bothered actually teaching C++
 
Zoe
3:05 PM
@milleniumbug Doesn't surprise me
@milleniumbug I'm mostly just reading online, the basics apparently mainly use horrible practice (or I'm just looking on the wrong websites)
 
People seem to forget you can write extremely performant and safe C++ without ever using pointers
@Zoe I'm going to guess the latter, try getting a copy of Bjarne's book
 
or try C++ Primer
 
@MiroslavCetojevic eh.... I'd just use Bjarne's book, C++ Primer is questionable in my opinion
 
it's not perfect, true, but arguably better than most stuff online
 
Zoe
@Mgetz yep xd Removed the pointers and the deletion, now it's working fine.
@Mgetz Got one of those atm. Should probably use more of those though.
 
3:19 PM
@Zoe not if you don't need to
 
Zoe
@Mgetz I'm used to pass by reference. That's also the only option in some of the other languages I know. The code I'm currently writing is pretty much only to test what I can do with the language, what I can't, and what I shouldn't.
Wouldn't the lack of smart pointers require manual cleanup?
 
@Zoe Nope, Scope based Resource Management AKA RAII
Basically C++ will automatically run the destructors of any 'automatic' variable
 
Manually managed resources need to be managed manually. You can distinguish them if the docs say "after you're done with using this, call this function"
 
so insofar as all dynamically allocated resources are owned by smart pointers you're fine
 
That said: you do not want to have manually managed resources. Always have a RAII wrapper. A std::unique_ptr<T> is one of such wrappers, but there are a lot more.
 
3:23 PM
Use raw pointers only to hold optional references to dynamically allocated resources in a non-owning way.
 
std::string and std::vector<T> are already wrapping a manually managed resource btw, and are not a manually managed resource by themselves.
 
Zoe
So there's actually a lot that's handled automatically? That's kinda nice. Also just found the guidelines (wish I had earlier xd), it at least clears up some stuff on local variables. (And mentions not to use raw pointers earlier)
 
@Zoe Oh yeah, general rule of thumb is you shouldn't need to use new or delete directly unless you're doing library level stuff. Even then you should avoid it.
 
Zoe
That's probably gonna make this a lot easier :D Got some more cleanup to do then
 
Yeah there was an entire CPPCon video where someone basically says to teach C++ intuitively without the confusion that is C
most people get tied up in the C legacy and then go ranting off into Rust
 
Zoe
3:37 PM
Yeah. Stumbled over some guides that mentioned the C legacy stuff, but it never actually said it was bad to use it (probably has its uses though, but not common ones)
But is memory management entirely automatic (assuming RAII)? Wouldn't regular instances (with raw pointers that aren't accessed directly) risk never being used? Probably assumes a thread that doesn't block the UI thread in a way that makes the OS think the program is inresponsive
 
@Zoe not using something is fine, using something that is dead or doesn't exist is a problem
 
not sure what are you referring to
 
RAII is not garbage collection it doesn't have a thread.
 
Zoe
No, but it does still clean up some stuff
 
RAII follows the observation that 99.99% of the time when you create a thing it either 1.) should live until the function finishes execution or 2.) it should live as long as the other thing that follows the assumption number 1.
 
Zoe
3:47 PM
although that actually does answer the question, and the cases are probably really rare. I'm assuming anything initialized in i.e. the main method would be destroyed automatically, which means the only thing left are unused instances of stuff, but they're extremely unlikely to amount to anything big since it doesn't require explicit deletion
 
@Zoe Yes but it doesn't stop the world to do it like a GC does, it's real time and happens immediately after the variable leaves scope.
@Zoe not sure what you mean by "unused instances" if the compiler doesn't detect a write to them or visible side effects it's free to optimize it out (and will aggressively).
 
Zoe
@Mgetz mainly talking about at runtime. It's a chained concern that pretty much only existed while I didn't know RAII existed. The problem would be if anything created in methods weren't deleted automatically, but instead stacked up
 
@Zoe if you allocate something using raw new or any other method that requires manual cleanup, you are responsible for it. Generally speaking you write a wrapper or just use std::unique_ptr potentially with a custom deleter
 
Zoe
yeah, But for most cases, that won't happen. (Assuming regular class initialization without new falls under that category)
 
Correct, and even in those cases you can just use RAII wrappers to ensure they fall under the same automatic cleanup rules. There are some complicated corner cases involving graphs and shared resources but I wouldn't worry about that now.
 
Zoe
3:55 PM
Are there any other wrappers in the stdlib aside those listed in the page you linked earlier?
 
@Zoe no there are proposals for potential ones to support graphs, but they haven't gotten far. Those basically are garbage collection schemes with some automatic management (basically when an owning pointer goes out of scope the graph is traversed and checked).
 
Zoe
Oki. I'll have some more look at RAII wrappers after I get into headers. Thanks for the help btw ^^
 
If we've saved you from C with classes... then I'll consider this a success.
 
Zoe
I'm probably not gonna touch new or delete in a long time (unless I absolutely have to, but with vectors apparently replacing manual arrays, I hopefully won't have to ^^")
 
 
6 hours later…
9:42 PM
Yo, im brain storming some "relation ships" storage thini, say I have 500 objects, these objects can point to each other and have a network of "connections", now how can I effectively store/restore that data set? I was thinking if its possible to save current pointer value/string to std::string, and when - loding data, create new() object in that pointer string value, but I'm not sure... what do you guys think ?
feels to me that the address in memory has to be available
else ill get error
as pointer will be poiting to some random data in memory
and there has to be the same ammount of ram available?@
no idea...
just tryint o understand how a data set of 500 objects with connetions of 20k can be quickly saved/restored
 
nwp
What else do you need to do with it?
If saving and restoring is your main concern represent your graphs in a char array. But that probably conflicts with another requirement.
 
10:01 PM
what a coincidence, I'm learning about graphs right now - not an easy task
 
 
2 hours later…
11:33 PM
@Dariusz Basically you use node indexes rather than pointers. So, instead of Node* next_node, you can use int next_node. Then do lookups on whatever object holds the underlying memory.
Aka nodes[next_node] rather than using a pointer
An astute observer will note that this strategy involved an extra addition, compared with just using the raw pointer
 

« first day (889 days earlier)      last day (1964 days later) »