if __name__ == "__main__": e = phonebook() e.fname, e.lname, phone = input("Please enter nigger over and over: ").split(" ")
Samuel Ramirez
Not using C.
Oliver Thomas
Python is so fucking ugly
Noah Perez
>declaring size_t variables without initializing them you're passing random data as your buffer size and writing to an uninitialized pointer inside the struct. you need to malloc each pointer inside the struct and pass that size as the second parameter in getline()
Benjamin Sanders
>not initializing anything It's like you actually want things like heartbleed to happen.
Connor Williams
>malloc >The malloc() function allocates size bytes and returns a pointer to the allocated memory. The memory is not initialized. Use calloc instead and set _f, _l, and _p to zero to guarantee that getline will get you a new fresh freeable string
And also check for NULL after calloc and getline, and you can summarise the three consecutive printf's in one call
Jason Flores
>Use calloc instead not really necessary since you will be filling the allocated memory with a null terminated string anyways >set the read buffer size to zero then how will he read any data
Robert Clark
struct phonebook_entry *e = malloc(sizeof(*e));
Lucas Murphy
man7.org/linux/man-pages/man3/getline.3.html > If *lineptr is set to NULL and *n is set 0 before the call, then getline() will allocate a buffer for storing the line. This buffer should be freed by the user program even if getline() failed.
Camden Nguyen
>not reading the manpages
Grayson Hernandez
>Visual Basic C'mon man, if you're gonna use a .NET language, at least use C# or F#. VB is trash.
Luis Allen
OP here, thanks anons for all your answers. With the suggestions here I changed my code for error checking and also initialized size holders to 0 so getline now allocates the buffers in the struct. #include #include #include #include
>If *lineptr is set to NULL and *n is set 0 before the call, then getline() will allocate a buffer for storing the line. Initialize those string pointers to NULLs
Noah Morgan
This, use calloc instead of malloc
Colton Harris
$ valgrind ./a.out ==13215== Memcheck, a memory error detector ==13215== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. ==13215== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info ==13215== Command: ./a.out ==13215== ==13215== Conditional jump or move depends on uninitialised value(s) ==13215== at 0x4E9FC42: getdelim (iogetdelim.c:63) ==13215== by 0x4007EB: main (in /tmp/hellow/a.out)
Chase Diaz
you want the following: e = (phonebook_entry *) calloc(1, sizeof(phonebook_entry));
Christian Adams
I fixed that by assigning the return of getline to a variable and check it against the condition thanks user
Jose Cox
Why?
Michael Sanchez
Still wrong, you need to initialize the pointers to NULL. Treat the causes, not the symptoms!
Elijah Moore
goto ;
Aiden Green
I tried both malloc and calloc, and booth seem to initialize struct's members to NULL
Parker Ramirez
malloc will generally do that but isn't guaranteed to
John Gonzalez
That is undocumented, unguaranteed behaviour on the part of your particular implementation of malloc
Bentley Diaz
Thanks for pointing that out user
Luke Richardson
It's well documented somewhere, just not guaranteed by the standard.
Isaiah Myers
>not guaranteed by the standard For standard purposes, it's undocumented :^)
Jaxon Williams
Take your meds, grandpa #include #include class Contact { private: std::string fName, lName; int number;
Denoted a variable as unused instead of prefixing it with an underscore, which is bad practice except for members that should not be depended on if you provide a public API (which you don't).
Used static initialization to ensure that the entire struct was initialized with zeros
Didn't use calloc/malloc because for a problem this simple, you don't need it. If you were going to store an array of phone entries it might be helpful.
Another difference, If I had been using them, I wouldn't have casted their return values.
Indicated the type of variables in a more readable way
Dropped the implied return
Daniel Smith
>Dropped the implied return Nonstandard, noncompliant. You'd fail if I were to correct that.
Oliver Young
Damn C++ is fucking disgusting.
Adam Scott
Not if he's using C99 or C11.
Wyatt Campbell
What said. Also static initialization of structs was introduced in C99 and I'm using it in my program, so there's really no excuse except not knowing the standard.
James Robinson
Huh, I thought it was an excercise on memory management. Still, implied returns are disgusting in C.
Cameron Edwards
Enjoy your struct phonebook_entry *e = NULL;
e = (struct phonebook_entry *) malloc(sizeof(struct phonebook_entry));
Hudson Jones
> casting a malloc return value in plain C How about you actually learn C before trying to criticize it, you dumb pajeet.
Also, the irony here is that without the cast that actually looks like short, simple and clean code, as opposed to the fucking C++ abomination above.
Lincoln Rodriguez
There is no need to learn C anymore. And if you are talking about "short" programs, C doesn't do a good job here either. Sucks being a C tard, doesn't it?
Easton Campbell
Also, if you use typedef you can leave out the 'struct'. There's also no reason to initialize e to NULL since malloc returns NULL on failure anyway:
Idiot. You cannot insert data from scanf into unallocated memory buffer. You would likely get segfault. You are the reason why people think C is shitty.
Jason Sullivan
>casting malloc in C
Julian Martin
He's using a POSIX extension: 'm'. It automatically allocates a buffer large enough.
Christopher Bailey
If you're saying this looks like anything remotely good then you're out of your god damn mind, it looks like it was spitted out of an compiler.
Ayden Ramirez
>private: class defaults to private senpai
Jace Howard
>using visual bashit Just kys
Levi Fisher
I personally use this neat "trick" >phonebook_entry *e = malloc(sizeof(*e));
Henry Hill
It's not good, but it's more sophisticated, unlike the half-arsed C examples ITT
Kayden Collins
...
Wyatt Edwards
...
Isaac Lopez
e = malloc(sizeof(*e));
Jonathan Sullivan
Everything is in the struct in the original code, so it would have been wiser to put members in public as well. Otherwise I like this C++ code more than C.
Jace Brooks
>casting malloc I want sepples niggers OUT
Zachary Martin
That trick will get you later into trouble when the compiler will refuse to assign void* to a non-void* variable. Been there and it wasn't fun.
I always used to "typdef struct" but stopped doing that when I started to realize that it creates an anonymous structure. It basically declares two things. And working with anonymous structs makes code more sensitive to undesired effects. Yes, it has the effect that I need to add the struct keyword more, but I learnt the hard way.
For the latter, the compiler will try to assign all variables with its initialising value when entering the block, ONCE. You might think the assignment will take always place on the line you put it but the compiler will certainly think different.