need some constructive critisim on some code I wrote.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
need some constructive critisism on some code I wrote.
Hello. I'm not that advanced in the art of programming. So I just need some evaluation of code I have just written.
Am I doing things efficiently? Do you understand what i'm trying to do? Here is the code
Basically what I'm trying to create is an computer algabra system. It should look something like :
Two things:
1) Why do you have an empty "#define USER_DEF" statement? Your comment above mentions a index into an array, and some other stuff, but there is no clear definition (or commented examples) of what that define is for or how it will be used if it's needed by another file.
2) Why do you declare the "data" member of your structure to be void? I would declare it as an int, long int, or a character pointer.
Without some actual functional code though, it's not really possible to have many suggestions.
Well, your right about the definition of USER_DEF. It's not really needed. I just wanted to make the point that an element having a data type FUNCTION, can be interpreted two way. If the data it holds is smaller then a specified value, it's somthing that has been predefined, like a trigonometric function. Else it's somthing that the user defined.
I have a bit of a problem with the "data" member of my structure. The thing is, most of the time it will hold an integer value, because it will just be a referance to where the data actually is. It can, for example be an index of an array. Somthimes though, it will have to be a floating point value, if the type of data it holds is a number. Now I considder it quite an waste of memory if most of the time I use the presision of a floating point value when it really just holds an integer. So just for now I declared it void till I figure out how to handle this. Any suggestions?
Type casting can solve that problem. Just declare data as a long, a double, or whatever you want. When you reference it, just typecast it to whatever value you need it to be. For example:
Code:
if (data_type == INTEGER)
printf("You gave me an int with a value of %d\n", (int)data );
if (data_type == FLOAT)
printf("You gave me a float with a vallue of %f\n", (double)data);
...
Ye I know I can do that. And I have. It's just that the variable will hold enough "room" for a floating point value, while most of the time it will represent an integer. And that's a waste of memory.
Then make data a pointer. A pointer to an int is the same size as a pointer to a float, a pointer to a string, and a pointer to a function. It will require the extra steps of reserving and freeing memory. More efficient? Well, it depends on whther you'll use strings. If you use it just for ints and doubles, then the addition of four bytes of overhead for each doesn't make sense when compared to using the space for a double as an int or a double; you'd be wasting space roughly half as many times.
To ensure your data field is EXACTLY the right width for your specific data type, you will need to use the pointer method described above, or use strings to hold your data, and convert back and forth when necessary.
That is, unless somebody else out there knows a way that I haven't been exposed to yet.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.