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

char array read

Name: fucking idiot 2009-06-01 21:07

    c++ (maybe c) question time
    i need to make a string class. i'm not allowed to use the c++ one.
    so what i don't get is how the fuck i read a string and save it into a array. i mean, i just don't see how. i've been looking around but i don't find anything that fits what i need
    all i need is that. reading a string and saving it into an array. if someone can guide me i'd be thankful.

    also it has to be dynamic, so no char arr[1000000000000000]

Name: Anonymous 2009-06-01 21:21


>>1:1:0:
    Couldn't match expected type `[a]' against inferred type `Maybe a1'
    In the first argument of `(++)', namely `c'
    In the expression: c ++ maybe c
    In the definition of `it': it = c ++ maybe c
(0.01 secs, 0 bytes)

Name: Anonymous 2009-06-01 21:22

You should have payed attention in class.

Name: Anonymous 2009-06-01 21:28

>>2
what i mean by this is that maybe you could give me a c example i could implementate in c++. is it so hard to get?
>>3
they really didn't teach me this. i'm supposed to make this out of nowhere.

Name: Anonymous 2009-06-01 21:30

malloc

Name: Anonymous 2009-06-01 21:34

>>4
Yes, they did fucking teach you. Who tells their class to get input without telling them how? Stupid bullshit like that is a staple of CS courses, even though you should be able to figure it out on your own.

Name: Anonymous 2009-06-01 21:38

>>2-
yahibbit

Name: Anonymous 2009-06-01 21:53

>>4
Then the rest of class will fail too and you're professor will throw out / curve the grade.  Problem solved.

Name: Anonymous 2009-06-02 13:54

>>4
This is what second semester CS students at our university believe, too.

``Oh, I haven't heard any of this before, so there's no way I can solve this exercise here, right? Right?''

Name: Anonymous 2009-06-02 14:11

If the assignment is only to make a string class/struct, implement it as a linked list. If you can't implement a dynamic linked list, you can't do shit and should give up programman.

Name: Anonymous 2009-06-02 14:37

>>10
Listen to this man. You need to implement a linked list of char's. Other than that, you're on your own.

Name: Anonymous 2009-06-02 14:55

>>11
why not use a char vector. this is c++ after all. you don't need to reinvent the wheel.

Name: Anonymous 2009-06-02 15:09

>>10,11
EXPERT memory allocators.

While you're at it, why not make it a DOUBLY-LINKED LIST for O(1) insertion and deletion at the front and the back?

Name: Anonymous 2009-06-02 15:12

>>13
I would even consider a doubly-linked-list with tree also. With pointers all over the shop so it can be iterated through as a tree, or a list. This would allow for easy sorting.

Name: Anonymous 2009-06-02 16:13

>>14
Clearly, the best data structure for the OP's assignment is a weighted directed graph, with vertices representing characters and edges representing the index of the character that is obtained by traversing the edge in a given direction.
To iterate the string, you'll need to first build up the string from the graph, then iterate the string itself. Insertion is O(1) (just choose a random vertex and add a new edge and node), with deletion O(n).
Good luck

Name: Anonymous 2009-06-02 19:33

Lol a linked list of chars.
Nice one /prog/ ;-)

Name: Anonymous 2009-06-02 22:21

>>16
Don't help him!

Name: Anonymous 2009-06-03 6:00

>>16 is not a Haskellite

Name: Anonymous 2009-06-03 8:13

>>18
As a matter of fact, I am.

Name: Anonymous 2009-06-03 8:18

>>12
Nice try but a vector of char's is sub-optimal for frequently modified strings. Doubly so when you use STL's implementation of vectors which is too general for a string implementation.

Name: Anonymous 2009-06-03 8:22

stop. what the fuck. just have a class with a method for outputting nicely and a piece of memory which you dynamically allocate based on the size of the parameter to the constructor which is a string.

Name: Anonymous 2009-06-03 8:24

>>21
EXPERT ENTERPRISE TURN-KEY STRING SOLUTION PROGRAMMER

Name: Anonymous 2009-06-03 10:24

>>22
actually, no, it's just the obvious solution to the problem. i'm starting to think that you were all serious when you suggested that >>1 should implement a goddamned linked list for the string. you're all fucking morons.

Name: Anonymous 2009-06-03 10:29

>>23
YHBT

Name: Anonymous 2009-06-03 10:34

>>23
Every one of us!

Name: Anonymous 2009-06-03 11:19

This is a little risky but you could implement the linked list of characters with lower overhead by storing the character in the pointer itself. This is very easy to do on AMD64 architectures since it's guaranteed (on currently released processors) that the usermode addresses you get will at most have 48 useful bits, so you have 16 more to store the data (this enables proper multi-byte character set support). Remember to mask the data bits and/or sing-extend the address when dereferencing the pointer though.

On 32-bit architectures this is trickier. If you're lucky, memory allocations will be aligned to 8 bytes, and clamped at 0x7FFFFFFF. This gives you 4 useful bits per pointer, so a character becomes two linked pointers. However some systems will only give 4 bytes of alignment and allow 3GB of usermode addressing space. In this case you have just 2 useful bits per pointer. If this is unacceptable, consider a kernel-mode implementation - there's much more tricks you can pull over there.

Finally, >>13 proposed a doubly linked list. If you have been following carefully, you'll be gladly surprised to discover that this is not only compatible with my proposed solution, but it comes with no extra space overhead! You just have to XOR both pointers together. To retrieve the next pointer in either direction, just XOR it with the pointer you came from. This doesn't affect the stored data in the pointers.

Name: Anonymous 2009-06-03 11:24

Or just realloc for every operator= call.

Name: Anonymous 2009-06-03 11:25

>>26
♥ If someone turned in a working implementation of this to me, I'd pass them. ♥

Name: Anonymous 2009-06-03 12:53

>>26
This is simply brilliant.

Name: Anonymous 2009-06-03 13:32

How about linked list type of string class. Please forgive my ignorance if this seems memory exhaustive.

Name: Anonymous 2009-06-03 13:33

>>30
Oh wow, I see it was already proposed.

Name: Anonymous 2009-06-03 14:46

It would be better to store the characters in a relational database instead.

Name: Anonymous 2009-06-03 14:53

>>32
But how would design your table to accommodate such an implementation?

How about having one column for each character?  So each row would represent one string, with the first character being stored in column 1, the second column 2, and so forth. Alternatively, I guess one could create a new table for each string, and store each character as a tuple (string index, character) in the table. You could then retrieve the string by selecting all entries from the table and ordering by the index.

This would make reversing the string trivial, too: Simple reverse the sort oder.

Name: Anonymous 2009-06-03 15:38

This thread makes me very sad.

Name: Anonymous 2009-06-03 15:43

>>34
What's the matter, too deep for you? This is EXPERT DISCUSSION MATERIAL.

Name: Anonymous 2009-06-03 15:53

>>26
You just have to XOR both pointers together. To retrieve the next pointer in either direction, just XOR it with the pointer you came from.
I bow to you.

Name: Anonymous 2009-06-03 16:33

>>28
I'm tempted.

Name: Anonymous 2009-06-03 17:10

I'm a terrible programmer, and even I know the answer to this.

Name: Anonymous 2009-06-03 17:38

>>26
This is glorious. Please have my manbabies.
Bonus points for Machiavellian truth.

Name: Anonymous 2009-06-03 22:53

   ____ ∧ ∧   / ̄ ̄ ̄ ̄ ̄ ̄
 ~' ____(,,゚Д゚)< 
OMG OPTIMIZED
   UU    U U    \______

      ∧ ∧  / ̄ ̄ ̄ ̄
    (,,゚Д゚)< 
VROOM VROOM
     ⊂  ⊃ \____
    ~|  |
      し`J

Name: Anonymous 2009-06-04 1:29

How do others pronounce the OMG OPTIMIZED and VROOM VROOM effects? I used to imagine them in deep, announcer-type voices, but just now realized they are quite amusing in the high-pitched voice you would expect such a cat to have.

Name: Anonymous 2009-06-04 1:44

>>41
If cats could talk, I would expect them to sound like Patrick Stewart and Don LaFontaine.

Name: Anonymous 2009-06-04 5:30

>>41
"omg optimzed" to me should be said in a bit of a dumb loud voice, like mongo from heathcliffe.

"vroom vroom", like a stupid kid playing with a car

Name: Anonymous 2011-01-31 21:11

<-- check em dubz

Name: tray 2012-03-14 23:15


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