CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

classes memory cost

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
z3ngew



Joined: 20 Apr 2013
Posts: 50

View user's profile Send private message

classes memory cost
PostPosted: Fri Jun 07, 2013 5:29 am     Reply with quote

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: 9226
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Jun 07, 2013 5:41 am     Reply with quote

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

View user's profile Send private message

Re: classes memory cost
PostPosted: Fri Jun 07, 2013 6:28 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jun 07, 2013 6:42 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jun 07, 2013 6:53 am     Reply with quote

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: 1349

View user's profile Send private message

PostPosted: Fri Jun 07, 2013 2:11 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jun 07, 2013 3:37 pm     Reply with quote

Thanks for the explanation my friend Smile
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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