MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/badcode/comments/maj3iw/deleted_by_user/grsumv7/?context=3
r/badcode • u/[deleted] • Mar 22 '21
[removed]
86 comments sorted by
View all comments
28
This C++ solution always uses the least applicable function to do its tasks, leaks tons of memory and uses questionable coding practices.
After compiling, give it a filename as a commandline argument.
#include <iostream> #include <string> #include <stdio.h> #include <deque> #include <algorithm> using namespace std; template <typename T> struct PtrImpl { using type = T*; }; template <typename T> using Ptr = typename PtrImpl<T>::type; #define PtrCast(t) reinterpret_cast<Ptr<t>> Ptr<int> read_date_from_file_object(Ptr<FILE> f) { Ptr<int> result = PtrCast(int)( malloc(6 * sizeof(int)) ); Ptr<char> buf = PtrCast(char)( malloc(256) ); if (feof(f)) return NULL; fgets(buf, 256, f); Ptr<size_t> readPos = PtrCast(size_t)( calloc(1, sizeof(size_t)) ); size_t readPossum = 0; for (int i = 0; i < 6; i++) { *(result + i) ^= *(result + i); *(result + i) ^= stoi(std::string(buf + readPossum), readPos); readPossum += (*readPos) + 1; } free(readPos); free(buf); return result; } struct PtrCpy { Ptr<std::deque<Ptr<int>>> dq = NULL; PtrCpy(Ptr<std::deque<Ptr<int>>> dqp) { intptr_t dqa = (intptr_t)dq; dq = Ptr<std::deque<Ptr<int>>>(dqa ^= (intptr_t)dqp); } Ptr<int> operator()(Ptr<int> p) { Ptr<int> p2 = PtrCast(int)( malloc(6 * sizeof(int)) ); for (int i = 0; i < 6; i++) { memcpy(p2 + i, p + i, sizeof(int)); } dq->push_back(p2); return p; } }; struct PtrCmp { bool operator()(Ptr<int> p1, Ptr<int> p2) { for (int i = 0; i < 6; i++) { if (p1[i] > p2[i]) return true; if (p1[i] < p2[i]) return false; } return false; } bool operator()(Ptr<int> p1, Ptr<int> p2, nullptr_t dummy) { for (int i = 0; i < 6; i++) { if (p2[i] < p1[i]) return true; if (p2[i] > p1[i]) return false; } return false; } }; int main(int argc, char* *argv) { Ptr<FILE> f = fopen(argv[1], "r"); Ptr<int> r; std::deque<Ptr<int>> rd; while ((r = read_date_from_file_object(f))) { rd.push_front(r); } std::deque<Ptr<int>> rd2; std::transform(rd.begin(), rd.end(), rd.begin(), *new PtrCpy(&rd2)); std::sort(rd2.begin(), rd2.end(), *new PtrCmp); for (std::deque<Ptr<int>>::iterator i1 = rd.begin(), i2 = rd2.begin(); i1 != rd.end(); i1++, i2++) { bool x = (!((*new PtrCmp)(*i1, *i2))) && (!((*new PtrCmp)(*i2, *i1, nullptr))); if (!x) { std::cout << "false"; return 0; } } std::cout << "true"; fclose(f); }
16 u/Mabi19_ Mar 22 '21 edited Mar 22 '21 Here are the features, in case you can't read the pure embodiment of evil above. (Okay, maybe I'm exaggerating a little.)No-one remembers that * makes a pointer, so I use Ptr<type> everywhereI just used a #define for the PtrCast because I felt like it (definitely not because I forgot how to make it using template metaprogramming)The using namespace std; is only for 1 function callReferences don't existI don't know what the new operator actually doesMixed old style and new style of I/OThe braces are just everywhereBrackets too, though not as muchOne overly descriptive function nameThe pointer compare is made into less-than-or-equal instead of just less-than when an extra null pointer argument is passed inPointer arithmetic everywhere, except when I learned that you can just use array indexing but I didn't want to change all of the existing codeA possum (readPossum: corrected case readPosSum) counts the characters readIt leaks all of the world's memoryI always choose the function that is the worst possible choice for the task.
16
Here are the features, in case you can't read the pure embodiment of evil above. (Okay, maybe I'm exaggerating a little.)
Ptr<type>
#define
PtrCast
using namespace std;
new
readPossum
readPosSum
View all comments
28
u/Mabi19_ Mar 22 '21
Welcome to code hell.
This C++ solution always uses the least applicable function to do its tasks, leaks tons of memory and uses questionable coding practices.
After compiling, give it a filename as a commandline argument.