Codepad
How does coderpad work? Are there similar open source projects?
The REPL uses Pry to provide a REPL with history, highlighting, and autocomplete. Additionally, whenever you run scripts... Strangely enough, there are descriptions of how all their runtime environments work in the source code of the page, even though they aren't visible. Weird. So anyway, if you got that far and have the developer tab open with the console, source code, networking and and more, I suggest you poke around in there and see what you find. Happy hacking!
More on reddit.comC++: Problems understanding Destructors (code in CodePad)
The problem occurs here
return(temp);
You are creating a temporary object that has allocated memory. When you return it, it's copied, and the value of the pointer is copied to the new Vector. But then the temporary goes out of scope and the destructor calls delete[] on that pointer. So now the new Vector has a pointer to invalid memory which it frees when it too goes out of scope. The same happens with the parameter to operator^ because its passed by value.
* Actually, the compiler can optimize away the copy due to the return. It's most likely due to the parameter being passed by value instead of by reference, though both are possibilities if the optimization is not done.
Because you're dynamically allocating memory, you should also define a copy constructor and assignment operator to correctly handle the memory transfer.
I suggest an alternate implementation where you use a std::map<int, double> instead of an array to store the coefficients where they are keyed by the term to which they apply. This will allow you to store sparse vectors much more efficiently without adding much complexity to your multiplication.
v = new double;
You don't need to do this since you set it to zero right after. It causes a memory leak.
** I added some extra print statements so you can see what's happening: http://codepad.org/TlvkvE99
The output indicates the temporary is being copied when it returns, then the one declared inside the function goes out of scope, then the copy is assigned to Z, and the copy goes out of scope, which is the second free of the same memory.
You won't always see the same output, however, as it will be compiler and system dependent. For example, on my system, it does the optimization and happily takes no action for the second free.
Constructing xMore on reddit.com
Constructing y
Constructing temp
-0.5
-0.5
-0.375
// no second "Constructing y" message because it uses the compiler-generated copy constructor for the parameter
Destructing temp : 0xa0380b8 // temporary
Destructing y : 0xa038058 // parameter
-0.5, -0.5, -0.375
Destructing temp : 0xa0380b8 // Z - it was assign the value of temp
Destructing y : 0xa038058
Destructing x : 0xa038020
What's the deal with technical interviews that use CoderPad?
Videos
This service
https://codepad.th-luebeck.dev
could be of interest to anyone who wants to share small code snippets. We developed this service during the COVID-19 pandemic for our programming beginners in computer science courses in order to promote the online exchange among students and with supervisors using specific code snippets.
The service is (intentionally) anonymous and primarily developed for our teaching needs at the LΓΌbeck University of Applied Science (programming courses, beginner level). Codepad is intentionally not a full-featured IDE.
But maybe it is of interest to others? That is, why we share it (several of our students asked to do so).
So, here we go. Especially, Java, Python, Go, or C programmers might want to have look.
What you need to know:
-
A codepad is stored only for 180 days (the duration of a semester).
-
The runtime of a codepad is limited to 10 seconds (so it is not useful for interactive programs or for GUI-programs).
-
A compiled and executed codepad is served from cache.
-
You can share links of codepads in whatever messages or social networks you want.
-
You do not even have to register to use codepad.
-
Each codepad is completely anonymous (except, you provide contact data in your source code).
So, code strong and keep healthy, wherever you are!