[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

char* astrcat(char* prefix, char* suffix) { char* str = NULL;

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 35
Thread images: 1

File: helpme.jpg (92KB, 656x773px) Image search: [Google]
helpme.jpg
92KB, 656x773px
char* astrcat(char* prefix, char* suffix)
{
char* str = NULL;
asprintf(&str, "%s%s", prefix, suffix);
return str;
}

char* getstr()
{
char* str = "";
char character;
while((character = getchar()) != '\n')
str = astrcat(str, &character);
return str;
}

I usually just write code for small personal utillities so I dont have to deal with much user input,
but I made a function to get a string using getchar, for some reason it inserts a one after each character.
It worked fine with getche but getchar is easier to use. Can you help me find the reason /g/?
>>
I don't know C
>>
>>57137908
Try it sometime, its neat.
>>
>>57137862
Don't use a single char as a string

Also, it appears to leak memory.

Shit code/10
>>
>>57138004
It does, just not enough for me to care. Its not going into some large project I just need it to work, optimization is an afterthought in this case. Also when you say don't use a single char as a string should I initialize it to null? I'm learning.
>>
>>57138087
>optimization is an afterthought
>I just need it to work

Then why use C?
>>
>>57138087
character after getchar() is a char, and you passing it to astrcat() and treating it as a string
>>
>>57137862
Returning a locally scoped variable/10
>>
>>57138153
>not knowing how pointers work
>>
Undefined behaviour you dumb retard.
>>
>>57138087
If you are learning to do shit, writing a small efficient subset of the libc is a given, and if you did that, you'd know exactly why your shit doesn't work.
>>
>>57137862
How can you use str as buffer, in astrcat? It isn't pointing to anything.
>>
>>57138314
astrcat allocates or reallocates its own buffer if needed.
>>
>>57138314
asprintf automatically allocates space.
>>
>>57138347
You mean asprintf?
>>
>>57138355
oops, yeah
>>
>>57137862
I wouldn't feel too good about passing a non-terminated char pointer as a string, my dude
>>
Is this any better?
char* getstr()
{
char* str = malloc(sizeof(char*));
char character;
while((character = getchar()) != '\n')
asprintf(&str, "%s%c", str, character);
return str;
}
>>
void getstr(char* buffer)
{
char character;
while((character = getchar()) != '\n')
{
sprintf(buffer, "%s%c", buffer, character);
}
}

int main()
{
char buffer[256];
getstr(buffer);
printf(buffer);

return 0;
}


Shouldn't you just do this?
>>
>>57138427
Doesn't automatically resize the array when needed.
>>
Ok I think im getting it (not really)
char* getstr()
{
char* str = malloc(sizeof(char));
char character;
while((character = getchar()) != '\n')
asprintf(&str, "%s%c", str, character);
return str;
}

It works, but is it okay?
>>
>>57138415
sure, but
>char* str = malloc(sizeof(char*));
you probably want space for a single char in the beginning, not a char pointer. having your buffer start out with size char* is perfectly alright functionally, though.

and after that, if you call malloc, you're likely to get garbage data, so you probably want to force it to start as an empty string:
>str[0] = 0;
>>
>>57138523
I don't get the &str, as the first argument of asprintf, how can that even compile?
Is it really taking a char**, and if so, why?
Shouldn't you just send str?
>>
>>57138542
Okay, that makes sense. Will my current solution leak memory or is it ok?
>>
>>57138559
asprintf needs a pointer to a (string) so that when it reallocates your buffer to a newer, bigger space, it can change your (string) pointer to point to that new buffer.
>>
>>57138559
Its taking a char** don't ask me why. It works just fine.
>>
>>57138584
Ah, yeah that makes sense.
>>
>>57138570
asprintf will free your old buffer when it reallocates, but you still need to handle the fact that you have a string on the heap. Make sure to free the returned string afterwards and you'll be fine.
>>
>>57138604
I appreciate the help anon.
>>
>>57137862

>char* str = "";
>...
> str = astrcat(str, &character);

Why are you even dicking around in C if you can't be bothered to learn the most basic memory management? I mean, that's just the most *basic* part. It should be:

> char *str = NULL;

Now for the absolutely stunning horror:

>while((character = getchar()) != '\n')
> str = astrcat(str, &character);

OH MY FUCKING GOD I CANNOT LOOK AT THIS.
>>
>>57138644
I lost my k&r book. Its not going into anything important calm down sperg.
>>
>>57138644
And because I used astrcat instead of asprintf it would have made a null before any text made its way into the string.
>>
>>57138662
>Its not going into anything important
This is the programmer equivalent of "I'll do it tomorrow, promise". The rules are there for a good reason: so that you don't end up picking up bad habits. They should be second nature to you, not just something you sort of do when you have to because that's how you end up fucking things up.
>>
>>57138644
>It should be:
> char *str = NULL;

sounds like you have something to prove, anon. Go ahead and show us what happens when you asprintf(&ret, "%s%c", NULL, 'a');
>>
>>57138733
I am gonna do it tomorrow though its late.
Thread posts: 35
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.