Hey guys, I would like some pointer (no, not that kind) on removing bloat in code. Here is a fully functional piece of code that I wrote which I'm worried about bloat in. How do I shorten this?
int get_input()
{
int count, paren_count, total_count, c;
char *input, *temp;
>>14
If we are going to allow modifications we could add a lot more to it #define GC getchar
#define C char
#define M malloc
#define SO sizeof
#define RA realloc
#define I int
#define B break
#define F free
#define P printf
#define R return
#define Q if
#define EF else if
#define FR for
and then have I g(){I u,p,t,c;C *i,*m;i=(C*)M(500*SO(C));Q(i==0)R 0;P("%% ");FR(u=p=t=0;(c=GC())!=-1;u++,i++,t++){Q(u==499){i-=t;m=(C*)RA(i,(500+t)*SO(C));Q(m==0)R 0;i=m;i+=t;}Q(c=='\n'){*i='\0';m=i;m-=(u);u=0;FR(;*m!='\0';m++){Q(*m=='(')p++;EF(*m==')')p--;}Q(p==0)B;P(" ");}*i=c;}*m='\0';i-=t;P("%s\n",i);F(i);R 0;}
Thne we should replace everything that is >1 letter long with a single character.
>>16
If we allow a header file we could even OMPTIMIZE even further:
h.h #define GC getchar
#define C char
#define M malloc
#define SO sizeof
#define RA realloc
#define I int
#define B break
#define F free
#define P printf
#define R return
#define Q if
#define EF else if
#define FR for
#define G I g(){I u,p,t,c;C *i,*m;i=(C*)M(500*SO(C));Q(i==0)R 0;P("%% ");FR(u=p=t=0;(c=GC())!=-1;u++,i++,t++){Q(u==499){i-=t;m=(C*)RA(i,(500+t)*SO(C));Q(m==0)R 0;i=m;i+=t;}Q(c=='\n'){*i='\0';m=i;m-=(u);u=0;FR(;*m!='\0';m++){Q(*m=='(')p++;EF(*m==')')p--;}Q(p==0)B;P(" ");}*i=c;}*m='\0';i-=t;P("%s\n",i);F(i);R 0;}
>>8
I'm not, and I already have. The plan was to get this to work first and then clean it up. >>5
Input = realloc (input,...)
Realloc isn't gauranteed to work fool.
- pulls out increment as a constant
- returns -1 on failure (you were returning 0 everywhere for some reason)
- checks for end of file (ctrl+d or closed pipe)
- checks for too many closing parens
- adds CODE COMMENTS, so you can actually see what's going on
Name:
Anonymous2010-02-24 12:44
>>27
- also, doesn't fuck around with your input pointer, so you can use it with garbage collectors and other analysis tools. It's a little scary that OP doesn't keep a fixed pointer to the start of the buffer.
>>22
You think I give a shit if realloc works or not? You think his program is going to be able to gracefully degrade if it runs out of memory? Get the fuck out with your useless pedantry.
Name:
Anonymous2010-02-24 18:24
>>35
My program? Frankly, I like protection.
Anyway today I fixed the bloat issue. I'll post it later for show.
Name:
Anonymous2010-02-24 18:42
>>36 Those who give up efficiency for security deserve neither.
Name:
Anonymous2010-02-24 19:04
>>37
Okay here's the thing: I am writing an interpreted programming language, this function is part of the shell.
God damn, I need to free all of the memory in the linked list before it exits, or else bad things will happen, and you know it.
>>38
Why don't you just write it in a higher level language? Until you have it working, worrying about efficiency is a waste.
Name:
Anonymous2010-02-24 19:50
>>39
I hate higher level languages in general (save for languages like scheme and perl), plus the linked lists are so much easier in C. And you're right, worrying about efficiency in the beginning is stupid, I was just saying how safety is necessary for what I'm doing.
Any way, here's my fixed version. Sorry if I don't get the bbcode right
int get_input()
{
int count, post_count;
char *input = NULL, *temp, c;