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-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.

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