Getting a string in C

What is the safest way to get a string on C?

Attached: Capture+_2018-10-27-07-49-42.png (720x633, 765K)

Other urls found in this thread:

pastebin.com/YTJXYb5E
github.com/antirez/sds
twitter.com/SFWRedditImages

Call Rust via FFI

>what is string.h

why did they choose to raytrace some busted hag?

Easiest and safest is to call some inline asm as follows:
asm volatile(mov ecx, mystring.len\n\t
mov esi, mystring\n\t
mov edi, dest\n\t
rep stosb\n\t);

gets(3)

Locate the C++ standard library DLL/so and call the string related functions from there.

>doesn't mention where the string is coming from
It would help if you knew how to ask smart questions.

strings are too thin as you're probably obese
buy a rope at your local market and then hang yourself at home
it is the safest way

From user input?
gets is shit because it allows the user to cause buffer overflows at will

fgets lets you pass a parameter for maximum length so no overflows unless you the programmer fuck up. just get it from the "file" named stdin (it's a global variable defined in the header files)

You don't need to worry about writing a safe string. C clears the string out of the memory once its been run.

use a modern language

unsigned int size = 1488;
char *str = malloc(size);

str[size - 1] = '\0';
fgets(str, strlen(str), stdin);
/* strings operations */

>not using a linked list of chars
brainlets, all of you

kek

You could also just implement your own function.

...

scanf, or use the strings library. Or, getchar with your own function, like others have said.

fpbp

>str[size - 1] = '\0';
Fgets adds a terminating NULL byte.

Should I use scanf or fgets?

Implement your own function

Literally anything that does bounds checking.

>not using a min binary heap of chars keyed by the index

Omega lul

better to add your own null terminator in all cases just to be sure. this is best approach because you don't need to read the docs

t. pajeet

To be discriminated?

kek

The fuck are you talking about?

I think it's a reference to nvidia graphics and MGS.

You could do something like this pastebin.com/YTJXYb5E

...

I can't figure out how to tie it in a knot though.

Attached: Vector_Rope_example.svg.png (427x200, 14K)

Just try

I think just using the SIMD instructions for bulk data movement is faster than the legacy string instructions.

No thanks, I'll just stick with Python instead.

>He needs someone else to write his C code

isnt there like a gets_s function or something thats safe because it takes in the size of the string too so it cant overflow?

fgets?

Yeah, I call my software The Inputter and what it does is that it takes your input and displays it back onto your screen. It uses one of the most sophisticated algorithms written in C that only a highly intellectual can understand.

If your program is any serious, it will be using a graphical interface, where you can easily limit the number of characters the box can take.

ez: write a fully functional python interpreter, so you can write a fully functional javascript to python converter so you can write your code in javascript and tear your hair out

Girl is a rendering

github.com/antirez/sds

I thought it was real.

>Using strlen() on uninitialized memory
How did you manage to fuck up so badly?

He's a pajeet. There's no hope for him.

that defeats the entire point of C, good job. Might as well stop learning programming languages.

Not necessarily. Maybe you want to take advantage of an instruction that is native to your CPU. Get woke, bruh.

>Safety in C

Hahahahahahaahha

>t. pajeet

C was invented to avoid having to port entire assembly programs to other processors. You're supposed to implement the ANSI library and that's it, nothing else. Using ASM in C is as retarded as using OS-specific methods in Java, it just kills the entire purpose of the language.

>What is valgrind
>What are unit tests

>Using ASM in C is as retarded as using OS-specific methods in Java, it just kills the entire purpose of the language.
Again, maybe you want to take advantage of an instruction that is native to your CPU. That user didn't say write everything in ASM. There's a reason the C language allows ASM: maybe you want to take advantage of an instruction that is native to your CPU.

>he thinks hes safe

Dont worry your code will manage to fail in amazing ways

Not everyone is brainlet like you.

literally zero reason to use C today

if you don't code like a dirty nigger the only security concern you can have is your libc implementation and hipefully it's not written my incompetent faggots like you.

install gentoo

if you develop in webdev/apps/high-level shit. Had to use C today actually to write some low-level usb drivers for an esc/pos printer.

>Posted on a chinese image site that heavily relies on C
Good one

You can use C++ for drivers, almost all drivers for windows are in C++.
The only reason linux kernel isn't in C++ is because linus irrationally hates it.
how does Jow Forums rely on C?

>Cheap and old microcontroles use own ASM variant and only exist manufacture C compiler

>Cloudflare
>nginx
>php
>Your router probably runs on C
It's okay if you use JS, but don't spout bullshit

Yes I ca use C++ for drivers but in my case OOP was REALLY retarded for a simple down to earth driver. I pretty much only needed defines to translate hex instructions. No need for object nonsense in that case.

I thought C++ was discouraged in driver development because OOP can cause page faults in kernel mode

which one would that be? Even 8 bit avr controllers have C++, every arm too. That doesn't leave much except for ancient trash like z80.
Using software that's written in C doesn't mean it relies on C.
>C++
>OOP
you need to learn C++ user, OOP is a very small part of C++.
The single biggest advantage over C is raii. Every C code is forced to use gotos for resource management, or even worse, emulate gotos with ifs and flags (a sign of incompetent devs).
>because OOP can cause page faults in kernel mode
'OOP' has nothing to do with heap allocations and it's trivial to redefine operator new and delete to use a custom allocation scheme.

>Using software that's written in C doesn't mean it relies on C.
wtf? are you high?

#ifdef __I386__
//ultra fast processor specific asm
#else
//C implementation
#endif

okay pajeet codemonkey

Used C for a fuckton of projects, even a full CSFML rpg with story n shit, never ever used a goto anf label in my entire life. And I wrote a fucking compiler and vm for an imaginary cpu. Also brainlet here, what is raii? Don't know much about cpp but I worked a lot with java and while it's pretty cool for big enterprise clusterfuck projects, I think OOP is utter shit when it comes to very simple tasks. How is OOP a small part of cpp?

yup ok, that's a nice answer.

You are the perfect example of why massive disasters happen, people that feel too smart to commit mistakes, fragile cucks.

You are a perfect example of a massive faggot.

You obviously ignored my post: But you probably never wrote a single line of C in your entire life so when reading my post you must have thought "huh what is valgrind? Nah he's wrong anyway".

void* operator new(unsigned len) __attribute__((alias("emulate_cc_new")));
void* operator new[](unsigned len) __attribute__((alias("emulate_cc_new")));
void operator delete(void* p) __attribute__((alias("emulate_cc_delete")));
void operator delete[](void* p) __attribute__((alias("emulate_cc_delete")));
void* __cxa_pure_virtual = 0;
That's literally it. Anyone claiming C++ can't be used for drivers is incompetent.
>never ever used a goto anf label in my entire life
code monkey confirmed.
FILE *a, *b, *c, *d;
a = fopen(...);
if(!a) goto _exit;
b = fopen(...);
if(!b) goto _exit_a;
c = fopen(...);
if(!c) goto _exit_b;
d = fopen(...);
if(!d) goto _exit_c;
some other code;

fclose(d);
_exit_c:
fclose(c);
_exit_b:
fclose(b);
_exit_a:
fclose(a);
_exit:
something else;

This pattern repeats in C hundreds of times. It can only be replaced by (sorted by retardation level):
- copy pasting resource cleaning code for already open resources in every subsequent if
- adding boolean flags and if at the end to check if cleanup has to be done (in the fclose case it's not that bad, but often not possible)
- emulating goto using a break in a while that only executes once (I have seen it)
The goto solution is the simplest and the best one. It also absolutely fucking sucks compared to RAII.
>And I wrote a fucking compiler and vm for an imaginary cpu
aka. a homework
nigger a wrote 20kloc+ driver in fucking ANSI C, which means all variable declarations before code.
I hated every moment of it.

Literally zero reasons to use C in 2018. Maybe 20 years ago when compilers sucked.

c++ is the default language for win32 api driver development

Based

What do you need a string for?

#include

int main()
{
char myText[] = "Hello, World";
printf("my string is: %s\n", myText);
return 0;
}


If you need something dynamic just make your own.
A string is just a char array with extra bells and whistles.

>a wrote
*I wrote a

How do you counter the problems that cant be solved with valgrind?

OOP has a lot to do with polymorphism though and a lot of other abstraction that you don't really need for a device driver.

Then don't use those features.
I have never seen someone complain a language has too many features, just don't use them..........................................

>code monkey confirmed.
>FILE *a, *b, *c, *d;
>a = fopen(...);
>if(!a) goto _exit;
>b = fopen(...);
>if(!b) goto _exit_a;
>c = fopen(...);
>if(!c) goto _exit_b;
>d = fopen(...);
>if(!d) goto _exit_c;
>some other code;
>fclose(d);
>_exit_c:
>fclose(c);
>_exit_b:
>fclose(b);
>_exit_a:
>fclose(a);
>_exit:
>something else;
I prefer my style:
if ((a = fopen(...)) == -1))
//error handling

Also your driver seems cool but I just told you that C was better for very small down to earth drivers, that was the case for mine, I never said C++ was bad for driver development, it's the best language for windows driver development, objectively I mean. C was just a better pick in my context: every hex instruction had a nice define, all stored in a nice .h file.

yeah and at that point you're just using C...

Except you have std::string
And whatever else you do want to use.....

You must implement a unit test for every possible case for every function you write, that's called test driven development you fucking monkey, and that's not just for C that's for every language you use. If you wrote your unit tests religiously then valgrind should give you every memory leak. A program without any valgrind error, memory leak, byte lost or reachable byte is imo an indestructible program

>I prefer my style:
write an if for the d case that also closes a,b,c.
Remember they all have to be closed when the function exits normally.

no YOU have std

strlen needs a terminating null

oh sorry, using C with a layer of pointless OOP abstraction ontop of it.

Better off just using C functions at that point.

True. I overlooked his pajeet code. You could use calloc. As a rule of thumb, I use calloc for strings, and malloc for abstract data types.

>What are templates
>What is constexpr
>What are RAII types
>What are lambdas
Etc

>A program without any valgrind error, memory leak, byte lost or reachable byte is imo an indestructible program

So.. a Python script?

in my mind, you should only use malloc when you're writing what is effectively a complete constructor
if you've not got all the variables ready for filling out the data, then calloc

It's 12:33 pm, I'm tired and I'm doing an hypoglycemia so strong that I'm not so far from actally passing out so sorry i'm not in the mood rn

Exactly my point, my dude.

Allocate some space, fill it with bytes representing ASCII characters, end it with 0x00.

Why the fuck are you talking about python now?

elaborate

>end it with 0x00.
Don't do this if you want a decent string.