Re: "data hiding" prototype code



On Apr 11, 1:18 pm, "bluejack" <bluuj...@xxxxxxxxx> wrote:
Your design does not make it clear who "owns" this data. An algorithm
is typically a particular pattern applied to someone else's data,
although it may store some of it's own in the process. Thus, using
"Algorithm" as the generic placeholder for your OO class could be
confusing. Typical algorithms will *take* data and return *results*.

However, assuming that Algorithm really does own this data, I would
define the struct inside the .c file, providing the typedef in the .h
file. *This* hides your data. Algorithm() can return a pointer to this
type, and then you can call accessors with the pointer to obtain
individual elements of the data.

Thank you for your reply.

I realize that the name "Algorithm" might be confusing. Maybe I should
have named it "Object". My intention was that "algorithm.c" is the
owner of the data. I *did* define the struct inside algorithm.c (named
"alg") and provided the typedef in algorithm.h (named
"ALGORITHM_DATA"). "ALGORITHM_DATA" is a typedef, not a variable
definition. Maybe the name in all caps was confusing. Maybe I should
have named it "AlgorithmDataType".

I didn't want to return a pointer to the structure because in my
actual code, I want a function other than the caller of Algorithm() to
get access to algorithm.c's data directly. For example, I want
Algorithm2(), which does not call Algorithm() to be able to call
GetAlgorithmData() to get access to the data structure defined in
algorithm.c. I realize I did not specify this calling structure in my
oversimplified example. I have added the Algorithm2() code below.
Please let me know if I have misunderstood you.

main.c:
#include "algorithm.h"
#include "algorithm2.h"

int main(void)
{
Algorithm();
Algorithm2();

return 0;
}

algorithm.c:
#include "algorithm.h"

static ALGORITHM_DATA alg;

void GetAlgorithmData(const ALGORITHM_DATA **out)
{
*out = &alg;
}

void Algorithm(void)
{
alg.data1 = 1.0;
alg.data2 = 2.0;
alg.data2 = 3.0;
}

algorithm.h:
#ifndef ALGORITHM_H_
#define ALGORITHM_H_

typedef struct {
double data1;
double data2;
double data3;

} ALGORITHM_DATA;

void GetAlgorithmData(const ALGORITHM_DATA **out);
void Algorithm(void);

#endif /*ALGORITHM_H_*/

algorithm2.c:
#include <stdio.h>
#include "algorithm.h"

void Algorithm2(void)
{
const ALGORITHM_DATA *alg_ptr;

GetAlgorithmData(&alg_ptr);
/*alg_ptr->data1 = 99.9;*/ /* This is not allowed */

/* do something with data here */
printf("%f\n", alg_ptr->data1);
printf("%f\n", alg_ptr->data2);
printf("%f\n", alg_ptr->data3);
}

algorithm2.h:
#ifndef ALGORITHM2_H_
#define ALGORITHM2_H_

void Algorithm2(void);

#endif /*ALGORITHM2_H_*/


.



Relevant Pages

  • Re: [ANNOUNCE,RFC] rcuhashbash synchronization benchmark module
    ... Details of the algorithm appear later in this mail. ... Inserting the item into the new bucket before removing it from the old ... struct rcuhashbash_bucket { ... goto unlock_and_loop; ...
    (Linux-Kernel)
  • [ANNOUNCE,RFC] rcuhashbash synchronization benchmark module
    ... I developed a new algorithm for moving a hash table ... Inserting the item into the new bucket before removing it from the old ... struct rcuhashbash_bucket { ... goto unlock_and_loop; ...
    (Linux-Kernel)
  • Re: "data hiding" prototype code
    ... struct definition in the header. ... I'm not sure what you mean by "publish the struct definition in the ... However, assuming that Algorithm really does own this data, I would ... void GetAlgorithmData ...
    (comp.lang.c)
  • Code vectorization syntax woes (C++ or other?)
    ... struct A { ... If this is used as a static part of some other algorithm, inlining will give optimal code, ... Often library authors will even move the vectorizing loop into the algorithm, ... When this chain is set up at runtime, every little part of glue is called by function pointer, so ...
    (comp.lang.misc)
  • Re: "data hiding" prototype code
    ... However, assuming that Algorithm really does own this data, I would ... Algorithmcan return a pointer to this ... I *did* define the struct inside algorithm.c (named ... void GetAlgorithmData ...
    (comp.lang.c)