|
|
View previous topic :: View next topic |
Author |
Message |
z3ngew
Joined: 20 Apr 2013 Posts: 50
|
classes memory cost |
Posted: Fri Jun 07, 2013 5:29 am |
|
|
Hello everyone,
I have a little question, if i intended to use classes in writing a program, i would like to know, if the memory cost is being paid for the overall class(it is stored in memory typedef) or only the methods and variables being used are paid for.
Thanks in advance,
z3ngew |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Fri Jun 07, 2013 5:41 am |
|
|
quick,simple answer
cut some code and read the listings.
you'ld have to supply us with a good test program to play with....
Honestly, ask 2 programmers to cut code and you'll get 3 ways to do it!
Without a baseline test program,who can really say? And..if the 'cost' is just a few bytes, who cares? Just buy a bigger PIC ! That's been the PC program philosophy for decades.memory is cheap,cut lousy code, crank up the clock speed, it 'works'....
jay |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
Re: classes memory cost |
Posted: Fri Jun 07, 2013 6:28 am |
|
|
z3ngew wrote: | Hello everyone,
I have a little question, if i intended to use classes in writing a program, |
"Classes" are objects. That means you're using C++ or some other object orientated language. Whatever one you're using, its NOT C. CCS C is only C and has no support for anything that could really be called a class.
Most object orientated compilers targeting embedded platforms will only produce code for the functions that the application is using. If there are functions in the class which are not used, then code space is saved. Remember that some features of most object orientated languages will mean that it works the other way. For example overloading and metamorphic functions will mean that multiple near copies must be generated to implement what is one function in the source code. Also in languages that have strong typing and other run-time functionality such as strong bounds checking and even garbage collection, will involve unseen, or at least unexpected overhead both in storage and code. This, amongst other things, means that C++ and other object orientated languages have never gained much popularity amongst embedded programmers trying to squeeze the most functionality and performance out of relatively slow and memory-limited hardware.
Some object orientated language implementations, particularly those specifically targeted at embedded targets, may produce code that is close to as compact and efficient to run as non-object orientated C, bit don't bet your career on it!
It is possible to write something approaching class-like code in C. I personally feel that is a reasonable structured approach to dealing with increasing code complexity, As I wrote in a C style guide for a company some ten years ago: "Think C++, then write C."
If you are meaning C source library code, which are NOT "classes", then yes, any unused functions will NOT produce any machine code. Only functions you're actually calling will be compiled. However, unused global variables may still be allocated RAM. |
|
|
z3ngew
Joined: 20 Apr 2013 Posts: 50
|
|
Posted: Fri Jun 07, 2013 6:42 am |
|
|
thanks for the reply temtronic
but, i haven't started with the code just yet, but i was wondering which one to choose, a class structure or a simple c source file and include it.
so class structure will give you a good control of your code but, is it really worth it, or just it will eat all my device memory?
Thank you,
z3ngew |
|
|
z3ngew
Joined: 20 Apr 2013 Posts: 50
|
|
Posted: Fri Jun 07, 2013 6:53 am |
|
|
RF_Developer you are correct, i am writing something approaching class-like code in C it is mainly based on struct data structure.
I want to know if all the approached_class will be counted as memory using or just the used methods and variables,
Thanks in advance,
z3ngew |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1345
|
|
Posted: Fri Jun 07, 2013 2:11 pm |
|
|
The CCS compiler won't generate code for functions that you do not use (if you use function pointers, merely assigning a function to a function pointer does count as "use"). It will take RAM for any variables you create regardless if you use them or not (mind you typedef does not create anything, so merely typdef'ing out a struct for your class won't produce any code).
In terms of extra memory generated by trying to implement class like structs in functions, you will probably get a little more memory generated, but the how much and what type depends on the implemenation. For example, say you try and implement the hidden *this pointer in your functions:
void myClassFunction(class_type *this, int param1, int param2, ...);
In this case, you will at the least generate extra ROM instructions to de-reference the pointer each time you use the struct in the function and lose a little time de-referencing it each time. In most cases you will also use an extra RAM variable for the function call, but only as much as the size of your pointer.
If you use the reference method, it is a little different:
void myClassFunction(class_type &o, int param1, int param2, ...);
This method will most often cause CCS to inline the function. This will mean more ROM instructions overall, but won't take any extra RAM (when passed with &, CCS tries to directly address memory) and you won't lose time in the function de-referencing the pointer since it will be directly using the memory address. I like this method better, especially on a PIC with 32k+ ROM.
There are other ways, but that is just a little off the top of my head. |
|
|
z3ngew
Joined: 20 Apr 2013 Posts: 50
|
|
Posted: Fri Jun 07, 2013 3:37 pm |
|
|
Thanks for the explanation my friend |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|