Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

You have 10 seconds to implement std::vector in plain C

Name: Anonymous 2016-07-20 3:56

Must support:

* Adding elements
* Regrowing

Name: Anonymous 2016-07-20 3:57

Fuck, my finger slipped.

Hard mode:

* Accessing elements directly with array indexing (somevector[0])
* No void * casting.

Name: Anonymous 2016-07-20 17:26

Name: Anonymous 2016-07-20 17:29

Then what is std::vector implemented in? Javascript?

Name: Anonymous 2016-07-20 17:44

>>4
C arrays/pointers are part of the language.

Name: Anonymous 2016-07-20 17:54

>>4
It's implemented in C++ (it pretty much has to be, because it depends on templates and operator overloading). You could get similar functionality in plain C, but you'd have to use a different syntax.

For example, there's really no way to use something like std::vector<int> vec; in plain C, since C doesn't support namespaces or templates. You could implement a struct and appropriate functions to create an automatically resizing array-like data structure, which is really what I think >>1 is asking for. Making it type-generic like std::vector without resorting to making a new version of the struct and associated functions for each data type would be tricky and probably require messing around with pointers and casting, though that would actually be more elegant than what C++ templates actually do (they basically are macros that create multiple type-specific versions of functions and classes at compile time).

Name: Anonymous 2016-07-20 17:59

What is printf implemented in?

Name: Anonymous 2016-07-20 17:59

Making it type-generic like std::vector without resorting to making a new version of the struct
_Generic macro since C11

Name: Anonymous 2016-07-20 18:03

Name: Anonymous 2016-07-20 19:53

>>8
The trouble with _Generic is that the generic macro is defined once with one set of type-to-function associations. You can't use this to define an implementation on some arbitrary type anywhere in your codebase, as C++ vectors are typically used.

Name: Anonymous 2016-07-20 19:59

>>8
C11 is just "C++ without classes"

Name: Anonymous 2016-07-20 20:34

You have 10 seconds to learn a better fucking language than C, where you need to cobble together your own shit from smaller blocks of shit only to end up with a pile of shit that's shitty.

Name: Anonymous 2016-07-20 21:07

not reimplementing the Linux kernel in brainfuck

Name: Anonymous 2016-07-20 21:24

>>2
* No void * casting.
It's a good thing then that C allows type conversion with void* without casting.

>>6
would be tricky and probably require messing around with pointers and casting
It's trivial actually.

Name: Anonymous 2016-07-20 21:27

>>14
It's trivial actually
Only if you're someone that likes working with pointers.

Name: Anonymous 2016-07-21 0:36

>>15
You should do like the scarecrow in Oz and go get yourself a brain.

Name: Anonymous 2016-07-21 0:45

>>6
I like how you explained that as if everyone here were newbies to the language.

Name: Anonymous 2016-07-21 2:09

>>17
he's used to posting on reddit where he'll get upgoats if he ``ELI5''

Name: Anonymous 2016-07-21 4:15

>>18
le georges-louis sage

Name: Anonymous 2016-07-21 6:05

>>12
You have 10 seconds to learn a better fucking language than Common Lisp, where you need to cobble together your own macros from smaller blocks of macros only to end up with a pile of parens that's undecipherable to non-autists.

Name: Anonymous 2016-07-21 6:18

Any C challenge should include:
* No undefined behavior

But that's not really fair, because no actual written C code is free from UB.

Name: Anonymous 2016-07-21 6:18

(this space left intentionally blank)

Name: Anonymous 2016-07-21 7:27

>>15
My wife and daughter are pointers, so I love working with them.

Name: Cudder !cXCudderUE 2016-07-21 11:17

OpenSSL has implemented something like this using macros.

It also has stacks and queues, if I remember correctly.

Name: Anonymous 2016-07-21 11:27

C-dder is all talk and no action!

Name: Anonymous 2016-07-21 11:31

kill yourself >>24 and >>25

Name: Anonymous 2016-07-21 12:39

>>25
Not everyone is a JavaScript ninja.

Name: Anonymous 2016-07-22 10:42

>>24
Your software would be much better if you used tabs instead of a single-space for indentation.

Name: Anonymous 2016-08-01 20:49

the sad thing is that i actually tried to implement this using macros, and I thought it worked but it had a memory related bug that i didn't understand and i realized i suck at C

Name: Anonymous 2016-08-02 7:42

>>29
C itself sucks. You still suck at C, but the language can at least take some of the blame.

Name: Anonymous 2016-08-02 11:51

>>1
struct Cell
{
struct Cell * car;
struct Cell * cdr;
};


Then, just write suitable definitions of std::vector::push_back et al in Lisp.

Done.

Name: Anonymous 2016-08-02 12:44

>>31
how do you store an element in it lol

Name: Anonymous 2016-08-02 16:36

>>32
It's C, so you can cast whatever the fuck you want as a Cell *.

Name: Anonymous 2016-08-02 18:35

>>30
I agree this is true, but still

oh... i just realized it was because my add function uses realloc, so i was reusing an invalid pointer

Name: Anonymous 2016-08-03 8:01

>>33
you can cast anything but it won't be useful. your structure holds pointers to two structures of the smae kind but at no point does it hold anything other than pointers. you made a doubly linked list of nothings

Name: Anonymous 2016-08-03 9:29

>>35
If the semantics of car and cdr are maintained then it's actually a list of lists of lists of lists of lists, ad infinitum.

Name: Anonymous 2016-08-03 9:49

>>36
the problem is the lack of atoms

Name: Anonymous 2016-08-03 16:48

>>35

You can also use it as a singly linked list, with each cell containing a pointer to its data and a pointer to the next cell.

https://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/Cons-cells.svg/350px-Cons-cells.svg.png

You can also use it to implement a limited form of binary tree, where only the leaves store values. For example you could have Cell A containing pointers to Cell B and C, and cells B and C each containing pointers to two data objects.

A "true" binary tree however would require at least three pointers per cell, one to store data and two to point to other cells.

Name: Anonymous 2016-08-03 18:32

I know it's been more than 10 seconds but I think emacs has almost finished starting up.

Name: Anonymous 2016-08-03 18:41

>>38
You could do this

[ node-val . [ left-branch . right-branch ] ]

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List