[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
What's wrong with my code? Why doesn't this work?
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 57
Thread images: 4
File: b.png (3 KB, 225x225) Image search: [Google]
b.png
3 KB, 225x225
What's wrong with my code? Why doesn't this work?

#define BUFFERSIZE 80

typedef struct {
char* buffer;
int size;
} *Field, _Field;


Field Field_new(void) {
Field f = malloc(sizeof(_Field));
assert(f);
f->size = BUFFERSIZE;
f->buffer = malloc(sizeof(char) * f->size);
assert(f->buffer);
f->buffer[0] = '\0';
return f;
}


I'm getting segmentation faults and don't know why. Halp?
>>
>>54279609
>What's wrong with my code?
it's written in C
>>
>>54279627
How do you know?
>>
>>54279648
I'm a wizard
>>
File: AAAAAAAAAAAAAAAA.jpg (78 KB, 970x771) Image search: [Google]
AAAAAAAAAAAAAAAA.jpg
78 KB, 970x771
>>54279609
>typedefing a pointer

WOW, I have to deal with fucking retards who think they're being cute by typedeffing their types with the pointer included, it's so fucking infuriating.
>>
just do FLA

struct fla {
size_t len;
char data[];
}

struct elem = malloc(sizeof(elem) + 64 * sizeof(elem.data));
elem.len = 64;

elem.data = 'R'
for (int i = 1; i < elem.len; ++i) {
elem.data[i] = 'E'
}
>>
>typedef anonymous struct for no reason
>typedef a pointer
Fuck you, Wangblows' LPKEK bullshit is annoying enough.
>assert instead of graceful exit on ENOMEM
>buffer-size struct with fixed size buffer
>init first char to zero instead of memset

F-, read K&R and come back.
>>
#include <assert.h>
#include <stdlib.h>
>>
>>54279609

Works fine for me.

Ugly as fuck, though.
>>
>>54279719
>
elem.data = 'R'

Look faggot if you don't know C, don't bother pretending you do.
>>
>>54279609
>typedef struct {} *Field
AAAAAAAAAAAAAAAAAAAAAAAAHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
>>
>>54279688

Yeah, everyone knows that if you're typedef'ing a pointer, that you should put a P in front of it. And to make sure we know it's a 32-bit long pointer, it should be LP. 64-bit? LLP.

typedef struct { .. } *LLPField;

Ahhhh.
>>
>>54279760
>>54279688
My professor says it's good style
>>
>>54279776
NO ITS NOT
stop that shit right now
>>
>>54279776
It's absolutely disgusting and only serves to confuse the poor bastard who'll end up maintaining your shitcode.

struct op *faggot
is necessary and sufficient.
>>
>>54279776
leave your uni immediately

that shit makes 99% of C programmers want to jam forks in their eyes
>>
>>54279808
shouldn't that be
struct faggot *op;

since op is a faggot, but faggot is not an op
>>
>>54279830
Indeed you are correct good sir.
>>
>>54279776
It is. Don't listen to the fags who can't read documentation.

>it was hard to write so it should be hard to read
>>
>>54279776
>My professor says it's good style

It would be OKAY style if you called it FieldPtr or something descriptive.

Typedefing a POINTER that's simply named "Field" is goddamn idiotic. I would strangle you to death if I found that in my company codebase.

And _Field as the name of the bare structure type? What the actual fuck. Here, let me help you:

typedef struct _Field {
char* buffer;
int size;
} Field, FieldPtr;


Now, I don't recall the rules of modern C. Last I checked, you do the funky

typedef struct _Field { ... } Field;
[/code/

because I believe C still requires you to use 'struct' in your variable declaration if you use the struct name. i.e.

struct Field f;
f.size = blah blah blah;


whereas people generally like this better:

Field f;
f.size = blah


Correct me if I'm wrong about modern C.
>>
>>54279918
>have to look up god knows where just to see the typedef
>can't just write struct nigger
No.
>>
>>54279918

This guy knows how to keep a job.

>>54279938

Aw FUCK, I screwed up the code tag. :^(
>>
>>54279721
>seriously this my brain hurts
>>
>>54279938
*Field, _Field means that Field is now a typedef'd pointer and _Field is the regular struct pointer. Faggots getting upset in this thread don't know about C.
>>
>>54279980

Dude, we know. We're discussing readability and style.

Faggot getting all condescending because he can't keep up.
>>
>>54280010
>C
>readable
>>
>>54279609
Two mallocs ? Really ?. Pajeet use something else to do your shit. Wait... don't code for a living just fucking kill yourself.

>>54279688
That typedefining pointer retardation is all over the Windows API. Drives me fucking crazy too.
>>
>>54280064

C is extremely readable.

Now Perl, that's a write-only language.
>>
>>54280093
>C is extremely readable.
Except when someone uses evil typedefs amirite?
>>
>>54280114

Well, OP's typedefs are godawful.

"Field" being a pointer to a struct is idiotic.
>>
You should never force a typedef'd struct to be a pointer.
Let the user choose if they want a pointer or a stack-allocated struct.
You should be dynamically allocating the important parts within the struct anyway.
eg.
list_t stack;
stack_init(&stack);
/* or */
list_t *stack = (list_t *) malloc(sizeof(list_t));
stack_init(stack);
>>
>>54279776
Then he is your typical academically challenged fucktard that has not done a days coding in the real world. Ignore the faggot and come to the real professionals here.
>>
>>54279609
Don't you need to cast those mallocs to the correct type, like
Field f = ( Field ) malloc ( sizeof(_Field) );
f->buffer = ( char * ) malloc ( sizeof(char) * f->size );


>>54280072
>Two mallocs ? Really ?
How would you do that? I mean, I can think of ways, but it would look shitty as fuck.
>>
>>54280299
No.
>>
>>54280299
Allocate as one block unsigned char * then set the pointers up accordingly.
>>
>>54280367
And what if the struct was on the stack and the array on the heap?
>>
>>54280359
I never tried that, but I imagine it wouldn't compile if you try to assign a void* to whatever*.
>>
>>54280400
Then allocate just the buffer. Freeing when it goes out of scope.
>>
>>54280299

Since your buffer size is static and you're always allocating that much memory, there's no point in malloc'ing it.

typedef struct _Field
{
char buffer[BUFFERSIZE];
int size;
} Field;


Or you can make it dynamic and self-contained:

typedef struct _Field
{
int size;
char buffer[];
} Field;

int main(void)
{
const char *some_string = "Hello, world!";
int string_len = strlen(some_string);
Field *field_ptr = (Field *)malloc(sizeof(Field) + string_len);
strcpy(field_ptr->buffer, some_string);

return 0;
}
>>
>>54280510
Does the CRT actually do the free() ?.
>>
>>54280555

No. Well, yes, when your process shuts down memory doesn't stay allocated. But yeah, that's leaky code. Correcting that and the other error are left as an exercise for OP.
>>
>>54280510
Curious, what reason is there ever to use _Field when the typedef exists?
>>
>>54280659

No idea. But that's the most common way I've seen it.

I don't know if there is any downside to just using an unnamed struct. Maybe someone else knows.
>>
File: my-brain-is-full-of-fuck.jpg (13 KB, 268x268) Image search: [Google]
my-brain-is-full-of-fuck.jpg
13 KB, 268x268
Op here.

Thanks for all the style recommendations guys, none of your suggestions actually fixed the error. I should have expected as much from /g/.
>>
>>54280802
There isn't enough information for anyone to help you, idiot.
You didn't even state where it segfaults. Learn to debug your own programs.
>>
Try
f->buffer = malloc(sizeof(char) * (f->size));
>>
>>54280427
You may be thinking of C++, theyre more strict with assigning void *.

C compilers consider it a feature of the language and let you assign void * to anything without a cast. Even with -ansi -pedantic -Wall, and other warning messages enabled a c compiler still wont complain.
>>
File: fine.jpg (50 KB, 446x797) Image search: [Google]
fine.jpg
50 KB, 446x797
>>54279609
Can you give us a barebones program to replicate the bug?

Thats the standard way of getting help from online forums.

You post a full compileable short program, with a main function, which can replicate the error.

We will be able to compile it, and run it quickly and diagnose the issue without needing to guess.

Right now I dont see any issues with your code, and it works fine.
>>
>>54279627
>implying that you've programmed in C before
>>
>>54281703
Whoops, made a mistake.

I shouldnt set f->buffer to NULL, fuck my C is rusty.
>>
>>54281706
It's literally the OP pic.
>>
>>54281703
Dude if that image is a shit post of the OP's retarded program, then you are as retarded as he is for saying there is nothing wrong with it.
>>
>>54282044
The ability to identify it is pretty easy, faggot.

But you're only criticising it because that's what /g/ memes have told you to think.
>>
>>54280802

You snotty cunt, I told you it compiles and runs fine. Maybe figure out what's wrong with your machine, libraries, paths, etc.
>>
>>54279609
>Field Field_new(void) { Field f = malloc(sizeof(_Field));
That's legitimately confusing to read aloud.
>>
>>54284028

OP is a retard who writes unreadable, shitty code. Either his teacher is an idiot, or OP is so dumb he doesn't even understand what the teacher is saying (I'm leaning toward the latter). Additionally, his computer is all fucky such that the code segfaults on his machine.
>>
Why would you even typedef a pointer? Moreso without giving it a descriptive name?
Thread replies: 57
Thread images: 4

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

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.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.