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 support@ccsinfo.com

data encapsulation

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



Joined: 22 Aug 2006
Posts: 18

View user's profile Send private message

data encapsulation
PostPosted: Sun Jan 21, 2007 4:30 pm     Reply with quote

Hello,

I would like to use Data Encapsulation in order to avoid using too many global variable. Can this be achieved with CCS compiler? In ANSI C, I would add a "static" keyword in front of a variable declaration to make the variable private but this is not possible in CCS compiler?

Thanks,

ch_dupre
jma_1



Joined: 08 Feb 2005
Posts: 147
Location: Wisconsin

View user's profile Send private message

PostPosted: Sun Jan 21, 2007 11:07 pm     Reply with quote

Greetings,

This seems like a homework assignment question...

A 'static' variable is usually defined inside a function. This by it's definition means it is a local variable and only accessible inside the function.

In the object orientated work, not 'C', you can use standard 'setter' and 'getter' methods for accessing simple data. In this manner, you strictly control how the data is accessed. If your data is more complex (or some other class/object), it also hides the implementation of your structure.

Generally, object orientated practices are great, but on an 8-bit processor, some of the approaches are over-kill and pointless. Heck, use assembly language!! Also, there is a clear distinction between C and C++ (as well as java, ADA, C#, etc). C++ allows the creation of objects/classes which encapsulate both the data and related operations as well as supplying 'public' and 'private' identifiers. This a huge difference. C++ does have drawbacks though...check out the classic multiple inheritance problem and the associated 'code bloat' with o.o practices.

a quick reference:
http://www.cs.mun.ca/~donald/bsc/node13.html

Cheers,
JMA
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Mon Jan 22, 2007 12:00 am     Reply with quote

jma_1 wrote:
This seems like a homework assignment question...

It could be not a homework question. The early versions of the CCS compile help said that the word 'static' is for future use. The current help that I have (v 3.240) says ("Data Types" article):
Quote:
Variable is globally active and initialized to 0
It doesn't say anything about a local static variable declared inside the function. I never used the keyword static and I can't make a quick test right now, because I'm away from my lab.

Last edited by kender on Mon Jan 22, 2007 3:38 am; edited 1 time in total
ferrumvir



Joined: 01 Feb 2006
Posts: 64
Location: England

View user's profile Send private message Visit poster's website

PostPosted: Mon Jan 22, 2007 2:44 am     Reply with quote

Hi,

The best/only way to do encapsulation in C is to create other source files. These other source files have variables local to each file, then each file has a header file declares the functions used to access/update these variables.

I started to try and do this, but the encapsulation is a significant overhead (program size and execution time). The PIC programs I've created have never required more than one person to work on at a one time and therefore the power of encapsulation is never really used.

In the end I settled for a "kind of encapsulation" naming grouped variables/functions with prefixes and then using them correctly/as intended.

Cheers Scott
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Mon Jan 22, 2007 3:03 am     Reply with quote

ferrumvir wrote:
In the end I settled for a "kind of encapsulation" naming grouped variables/functions with prefixes and then using them correctly/as intended.


Scott,

Alternatively, you could use structs.
ferrumvir



Joined: 01 Feb 2006
Posts: 64
Location: England

View user's profile Send private message Visit poster's website

PostPosted: Mon Jan 22, 2007 3:09 am     Reply with quote

Hi Kender,

For the data, I completely agree (and I might change my code), but I'm not sure how you'd do that for functions, I've only previoulsy done that sort of thing on a PC, using a structure with function pointers, then changing the functions those pointers pointed to. Could you give a quick example.

Thanks Scott
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Mon Jan 22, 2007 3:31 am     Reply with quote

ferrumvir wrote:
...with function pointers, then changing the functions those pointers pointed to.

Alas, CCS v. 3.xxx doesn't support function pointers (FP). There is a number of threads on this forum, where people lament about it. FP functionality can, probably, be acheived with some assembly coding and manual positioning of functions in ROM. There's a chance that some day v.4 will support FP. There's a discusson about it in the "Version 4 comments" sticky (on page 9).
ch_dupre



Joined: 22 Aug 2006
Posts: 18

View user's profile Send private message

PostPosted: Mon Jan 22, 2007 4:03 am     Reply with quote

Quote:

The best/only way to do encapsulation in C is to create other source files. These other source files have variables local to each file, then each file has a header file declares the functions used to access/update these variables.

I've tried the following example, and to my surprise the function "ModifyValue" can change the variable called "value".

main.c file
Code:

#include "main.h"

#include "private.h"
#include "private.c"
#include "file.c"
void main(void)
{
   value = 10;
   IncrementValue();
   ShowValue();
   IncrementValue();
   ShowValue();
   ModifyValue();
   ShowValue();
   
}   

private.h file
Code:

#ifndef PRIVATE_H
#define PRIVATE_H

void IncrementValue();
void ShowValue();

#endif //PRIVATE_H


private.h file
Code:

#include "private.c"
int value;

void IncrementValue()
{
   value++;
}
void ShowValue()
{
   printf("Value: %d\n", value);
}

file.c file
Code:

void ModifyValue()
{
   value = 3;
}

The output on the RS232 is: 11, 12, 3. I don't understand why the compiler let the ModifyValue function work.

Cheers,
ch_dupre
ferrumvir



Joined: 01 Feb 2006
Posts: 64
Location: England

View user's profile Send private message Visit poster's website

PostPosted: Mon Jan 22, 2007 4:40 am     Reply with quote

Kender,

Thanks for the info.

ch_dupre,

The reason you can modify "value" is because you have included private.c into the main program, you should only include the interface function definitions, private.h. You need to compile private.c separately and then use the linker to link the object files together.

Cheers Scott
ch_dupre



Joined: 22 Aug 2006
Posts: 18

View user's profile Send private message

PostPosted: Mon Jan 22, 2007 5:24 am     Reply with quote

Scott,

My program won't compile if I don't include "private.c".

The linker is not working as far as I know.
That brings me back to the original question: How to avoid global variable in CCS knowing that there is no linker.

Cheers,

ch_dupre.
jma_1



Joined: 08 Feb 2005
Posts: 147
Location: Wisconsin

View user's profile Send private message

PostPosted: Mon Jan 22, 2007 7:34 am     Reply with quote

Greetings Kender,

I did not comment on the CCS implementation of 'static'. The question specifically asked about ANSI C. The question seemed more of a theoretical question, hence my comment about a homework assignment.

One approach for creating variables with limited access:
Code:

static int bMyValue
void vSetValue(int value)
{
   bMyValue = value;
}

int bGetValue(void)
{
   return bMyValue;
}

This is still has the problem of bMyValue being visible outside of the functions. If you only call the functions, no problem.

Alternatively:
Code:

int bGetSetValue(int bOperation, int bValue)
{
    static bMyValue = 0;
 
    // decode operation to perform
    if (bOperation == SET)
    {
        bMyValue = bValue;
    }

    // if you choose the 'set' operation, ignore the return value
    // 'get' operation by default

    return bMyValue;

}


You would change this to use pointers, but this illustrates the basic idea. This implementation has the advantage of completely encapsulating the data with the function.

Cheers,
JMA
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