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

string

Name: Anonymous 2012-05-23 12:46

What the hell went wrong?  We started with just this:

   char a[100];
   strcpy(a, "a string");
   strcat(a, " more");

And someone said "Hey, it would be better if the programmer didn't have to declare that 100 manually...  Is there some way the string itself could just resize as characters are added?"

Now, you can't really fault whoever it was for thinking that.  It seems like a reasonable idea.  But tell me, how the fuck did we get from that idea to this?

http://www.sgi.com/tech/stl/string

It's just...  horrible.

Name: Anonymous 2012-05-23 12:59

>>1
>sepples

Name: Anonymous 2012-05-23 13:00

Tooo.... mannnny... underscores.... _____________________________________________

Name: Anonymous 2012-05-23 13:02

>>1
Oh, and don't forget, if you want your strings to also have the amazing functionality of sprintf() and sscanf(), you'll also need the ABOMINATIONS known as std::istringstream and std::ostringstream...  But those probably won't do what you want, so you might as well go ahead and download boost::format(), which you can only get by installing the ENTIRE BOOST LIBRARY, which is over TEN THOUSAND separate files.

Your hard drive is now completely full, so you can't build anything, but if you could, you'd be able to do almost exactly the same neat stuff you could do with sprintf() and sscanf().

Name: Anonymous 2012-05-23 13:05

You ain't read no SICP today.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static char *copy(const char *s)
{
    size_t size = strlen(s) + 1;
    char *r;

    if (!(r = malloc(size)))
        return 0;
    return memcpy(r, s, size);
}

static char *cat(const char *s1, const char *s2)
{
    size_t sz1 = strlen(s1);
    size_t sz2 = strlen(s2) + 1;
    char *r;

    if (!(r = malloc(sz1 + sz2)))
        return 0;
    memcpy(r, s1, sz1);
    memcpy(r + sz1, s2, sz2);
    return r;
}

int main(void)
{
    char *s;
    char *p;

    if (!(s = copy("a string")))
        return EXIT_FAILURE;
    p = cat(s, " more");
    free(s);
    if (!p)
        return EXIT_FAILURE;
    puts(p);
    free(p);
    return 0;
}

Name: Anonymous 2012-05-23 13:35

vasprintf()

Name: Anonymous 2012-05-23 13:39

Typo. asprintf(). Does sprintf, allocates storage to hold it, and returns the pointer (pass by reference). Function return is length of string.

Name: Anonymous 2012-05-23 13:39

>>6
It's better to write your own function than relying on a GNU extension.

Name: Anonymous 2012-05-23 13:48

In that case, just drop the C library, and switch to strings with prefixed lengths, instead of null termination. This also makes them binary safe.

Name: Anonymous 2012-05-23 13:51

>>4

Your hard drive is now completely full
After installing a library that is not much more than 100 MB?

Ten thousand files
Did you forget that they're all header files?

Name: Anonymous 2012-05-23 13:56

>>4
Boost is just as bad as Perl. One big chunk of inter- and cyclic dependencies, some components nearly forgotten and/or badly maintained. I wouldn't touch it, not even with a 10-foot pole.

Name: Anonymous 2012-05-23 14:15

C++ turned into this huge bloated monster
so the Boost devs thought of a solution
make an even bigger bloated monster to kill the huge bloated monster
the monsters became friends

Name: Anonymous 2012-05-24 7:24

bampu pantsu

Name: Anonymous 2012-05-24 8:30

>>4
Not really, I can just use a fixed length char array with sprintf() and concatenate that onto my C++ string.

Name: Anonymous 2012-05-24 8:44

holy mother of fuck that shit is ugly

Name: Anonymous 2012-05-24 9:56

bampu pantsu

Name: Anonymous 2012-05-24 10:09

bampu pantsu

Name: Anonymous 2012-05-24 10:18

bampu pantsu

Name: Anonymous 2012-05-24 11:13

bampu pantsu

Name: Anonymous 2012-05-24 13:17

>>7
and returns the pointer (pass by reference).
0/10

Name: Anonymous 2012-05-24 19:01

>>20
In C, some people use ``pass by reference'' as a shorthand for ``stick an & in front of a variable'' as opposed to ``pass an uninitialized pointer''.
Contrast:
struct faggot faggot;
init_faggot(&faggot); /* sometimes called ``pass by reference'' */

And:
struct faggot *faggot_ptr;
init_faggot(faggot_ptr); /* shit, what did I blow up this time? */

Name: Anonymous 2012-05-24 19:10

init_faggot(&faggot); /* sometimes incorrectly called ``pass by reference'' */
FTFY

Name: Anonymous 2012-05-24 20:17

ALGOL 68 is older than C, B, Unix, and null-terminated strings. Not only does ALGOL 68's string change its length when needed, it also has the +, +:=, +=: and * (repeat) string operators found in most modern languages. ALGOL 68 also originated the printf function (which is type-safe and more powerful than C's), the type-safe tagged union and the void type.
string a := "a string";
a +:= " more"

Name: Cudder !MhMRSATORI!FBeUS42x4uM+kgp 2012-05-25 4:32

In many applications you will know the exact (maximum) length of the string a priori, so you can size appropriately.

For the cases where it isn't, exponential resize is the best solution.

The C++ standards guys wanted std::basic_string to do everything everyone wanted, and that's what the result is. You might find it amusing that std::basic_string<char>::operator==() eventually calls memcmp() in a few of the STL implementations.

Name: Anonymous 2012-05-25 4:45

You might find it amusing that std::basic_string<char>::operator==() eventually calls memcmp() in a few of the STL implementations.
Why would that be surprising? Many compilers are able to optimize a call to that function very efficiently. And what do you care about how a method is implemented?

In any case, std::string is awfully bloated. But it was added to C++ before STL, because everybody made an incompatible and buggy string implementation of their own. When the standard containers were later added, the string class was augmented to support iterators and ranges, and what's found in <algorithm>.

Name: Anonymous 2012-05-25 4:54

>>22
There's a difference between ``how'' and ``what''. When you see the function

    int factorial(int n)
    {
        return n == 0 ? 1 : n * factorial(n - 1);
    }

you don't think of a function that takes an integer, passed by value, which depending on it being zero or not returns either a constant or its value multiplied with the result of a recursive call. That's just the ``how''. Instead, when you see the name of the function, and a quick glance of the body you immediately think of what it does, namely the factorial.

Pass-by-reference is not a language feature. It's a semantic property of a procedure.

Name: Anonymous 2012-05-25 9:53

why didnt you just do something like

char * new_string(char *old_str, char *str_to_add)
{
    char * n_str = (char *)malloc(sizeof(old_str) + sizeof(str_to_add) + 1);

    strcpy(n_str, old_str);
    strcat(n_str, str_to_add);

    return n_str;
}

Name: Anonymous 2012-05-25 12:39

>>27
sizeof on a char* ? Not in my /prog/.

Name: Anonymous 2012-05-25 12:50

typedef struct{
    char *s;
    size_t n;
} string;

string new_string(char *source){
    size_t n = strlen(source);
    return (string){strcpy(malloc(n+1), source), n}; }

string scopy(string source){
    return (string){strcpy(malloc(source.n+1), source.s),source.n}; }

string concat{string s1, string s2){
    char *s = malloc(s1.n + s2.n + 1);
    sprintf(s, "%s%s", s1.s, s2.s);
    return (string){s, s1.n+s2.n}; }

void delstring(string s){
    free(s.s); }

Name: Anonymous 2012-05-25 12:53

>>29
new
Back to your AbstractCubicleFactoryFactory, please!

Name: Anonymous 2012-05-25 12:54

>>30
Fuck you, I'm a computer science student.

Name: Anonymous 2012-05-25 13:01

>>27
sorry, meant

sizeof(char) * (strlen(old_str) + strlen(str_to_add))

but u get the gist of what i mean

Name: Anonymous 2012-05-25 16:42

>>27
Don't cast the result of malloc().

Name: Anonymous 2012-05-25 16:42

>>32
sizeof(char) *
Completely useless and cluttering. sizeof(char) equals 1 by definition.

Name: Anonymous 2012-05-25 18:11

>>33
You need the cast in C++. It's the only way Stroustrup could get people to use new.

Name: bampu pantsu 2012-05-29 5:08

bampu pantsu

Name: Anonymous 2012-05-29 5:11

>>34
It makes the code a whole lot easier to read, you're one of the fucking retards who always use 0 instead of NULL aren't you?

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