Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > const struct members

Reply
Thread Tools

const struct members

 
 
Bill Pursell
Guest
Posts: n/a
 
      05-01-2006
Suppose I have a structure with many members, and I pass a pointer to
that structure to a function. I'd like the prototype of the function
to specify that it will only be changing certain members of the
function, but I have only the following horrific way to do that. Can
someone please suggest a better alternative?

#include <stdio.h>

struct foo {
char *a;
char *b;
};

struct const_foo {
const char *a;
const char *b;
};

struct foo_const_a {
const char *a;
char *b;
};

struct foo_const_b {
char *a;
const char *b;
};

void modify_a(struct foo_const_b *f)
{
f->a = "a has been changed!";
}

void modify_b(struct foo_const_a *f)
{
f->b = "b has been changed!";
}

void print_foo(struct const_foo *f)
{
printf("f.a = %s\nf.b = %s\n", f->a, f->b);
}

int main(void)
{
struct foo F = {"a string", "b string"};

print_foo((struct const_foo*)&F);
modify_a((struct foo_const_b*)&F);
modify_b((struct foo_const_a*)&F);
print_foo((struct const_foo*)&F);

return 0;
}

 
Reply With Quote
 
 
 
 
Rod Pemberton
Guest
Posts: n/a
 
      05-01-2006

"Bill Pursell" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Suppose I have a structure with many members, and I pass a pointer to
> that structure to a function. I'd like the prototype of the function
> to specify that it will only be changing certain members of the
> function, but I have only the following horrific way to do that. Can
> someone please suggest a better alternative?
>
> #include <stdio.h>
>
> struct foo {
> char *a;
> char *b;
> };
>
> struct const_foo {
> const char *a;
> const char *b;
> };
>
> struct foo_const_a {
> const char *a;
> char *b;
> };
>
> struct foo_const_b {
> char *a;
> const char *b;
> };
>
> void modify_a(struct foo_const_b *f)
> {
> f->a = "a has been changed!";
> }
>
> void modify_b(struct foo_const_a *f)
> {
> f->b = "b has been changed!";
> }
>
> void print_foo(struct const_foo *f)
> {
> printf("f.a = %s\nf.b = %s\n", f->a, f->b);
> }
>
> int main(void)
> {
> struct foo F = {"a string", "b string"};
>
> print_foo((struct const_foo*)&F);
> modify_a((struct foo_const_b*)&F);
> modify_b((struct foo_const_a*)&F);
> print_foo((struct const_foo*)&F);
>
> return 0;
> }
>


Since modify_b can't modify 'a' and modify_a can't modify 'b', why do you
need to protect the other structure elements? Is there a data corruption
issue here?

If so, you might want to have two versions of the structure: "protected" and
"unprotected". At a critical point in the program, copy the "protected"
structure to the "unprotected" one. Modify elements of the "unprotected"
structure via a normal routine or assignment. When done, you have a few
choices. You could discard the "unprotected" structure, copy the
"unprotected" structure to the "protected" one, copy the modified element
from the "unprotected" structure to the "protected", or compare the
structures to see what's going wrong.


Rod Pemberton


 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      05-02-2006
On 1 May 2006 12:19:48 -0700, "Bill Pursell" <(E-Mail Removed)>
wrote:

>Suppose I have a structure with many members, and I pass a pointer to
>that structure to a function. I'd like the prototype of the function
>to specify that it will only be changing certain members of the
>function, but I have only the following horrific way to do that. Can
>someone please suggest a better alternative?
>
>#include <stdio.h>
>
>struct foo {
> char *a;
> char *b;
>};
>
>struct const_foo {
> const char *a;
> const char *b;
>};
>
>struct foo_const_a {
> const char *a;
> char *b;
>};
>
>struct foo_const_b {
> char *a;
> const char *b;
>};
>
>void modify_a(struct foo_const_b *f)
>{
> f->a = "a has been changed!";
>}
>
>void modify_b(struct foo_const_a *f)
>{
> f->b = "b has been changed!";
>}
>
>void print_foo(struct const_foo *f)
>{
> printf("f.a = %s\nf.b = %s\n", f->a, f->b);
>}
>
>int main(void)
>{
> struct foo F = {"a string", "b string"};
>
> print_foo((struct const_foo*)&F);


Unfortunately, this need not work. You have no guarantee that the
padding, or lack of same, in a struct foo is the same as that in a
struct const_foo). offsetof(struct foo,b) could be 4 while
offsetof(struct const_foo,b) could be 8. Unlikely, I admit, but still
possible, especially on the DS9000.
> modify_a((struct foo_const_b*)&F);
> modify_b((struct foo_const_a*)&F);
> print_foo((struct const_foo*)&F);
>
> return 0;
>}



Remove del for email
 
Reply With Quote
 
Bill Pursell
Guest
Posts: n/a
 
      05-02-2006
Rod Pemberton wrote:
> "Bill Pursell" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> > Suppose I have a structure with many members, and I pass a pointer to
> > that structure to a function. I'd like the prototype of the function
> > to specify that it will only be changing certain members of the
> > function, but I have only the following horrific way to do that. Can
> > someone please suggest a better alternative?
> >
> > #include <stdio.h>
> >
> > struct foo {
> > char *a;
> > char *b;
> > };
> >
> > struct const_foo {
> > const char *a;
> > const char *b;
> > };
> >
> > struct foo_const_a {
> > const char *a;
> > char *b;
> > };
> >
> > struct foo_const_b {
> > char *a;
> > const char *b;
> > };
> >
> > void modify_a(struct foo_const_b *f)
> > {
> > f->a = "a has been changed!";
> > }
> >
> > void modify_b(struct foo_const_a *f)
> > {
> > f->b = "b has been changed!";
> > }
> >
> > void print_foo(struct const_foo *f)
> > {
> > printf("f.a = %s\nf.b = %s\n", f->a, f->b);
> > }
> >
> > int main(void)
> > {
> > struct foo F = {"a string", "b string"};
> >
> > print_foo((struct const_foo*)&F);
> > modify_a((struct foo_const_b*)&F);
> > modify_b((struct foo_const_a*)&F);
> > print_foo((struct const_foo*)&F);
> >
> > return 0;
> > }
> >

>
> Since modify_b can't modify 'a' and modify_a can't modify 'b', why do you
> need to protect the other structure elements? Is there a data corruption
> issue here?
>


There's no data corruption problem, I just observed that in many
instances I have data structures with several buffers getting passed
into functions, and the only manner by which I can tell which function
modifies which buffer is to inspect the function body/comments. I
could modify the protoypes to specify that the struct is constant, but
that of course gives no assurance on the contents of foo->a. I suppose
the only other solution is to define the structure as having const
members and making it difficult to assign/modify things. Something
like:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct foo {
const char *a;
const char *b;
};

void
print_foo(const struct foo *f)
{
printf("f.a = %s\n", f->a);
printf("f.b = %s\n", f->b);
}

void
modify(const struct foo *f, char *dest)
{
const char *src = f->a;
dest[0] = src[0]+1;
}

int
main(void)
{
struct foo f;
f.a = malloc(20);
f.b = malloc(20);
if (f.a == NULL || f.b == NULL)
perror(0), exit(-1);

strcpy((char *)f.a, "string a");
strcpy((char *)f.b, "string b");

print_foo(&f);
modify(&f, (char *)f.b);
print_foo(&f);
return 0;
}


The trouble with this is that modify() (or print_foo()!) could be still
be written to do:
((char *)f->a)[3]++;
but that's pathologically poor coding, so I can avoid that. In this
instance, the caller is doing all the casting, so it's fairly clear
from context which members are being modified.

 
Reply With Quote
 
Rod Pemberton
Guest
Posts: n/a
 
      05-02-2006

"Bill Pursell" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Rod Pemberton wrote:
> > "Bill Pursell" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed) oups.com...
> > > Suppose I have a structure with many members, and I pass a pointer to
> > > that structure to a function. I'd like the prototype of the function
> > > to specify that it will only be changing certain members of the
> > > function, but I have only the following horrific way to do that. Can
> > > someone please suggest a better alternative?
> > >

> >
> > Since modify_b can't modify 'a' and modify_a can't modify 'b', why do

you
> > need to protect the other structure elements? Is there a data

corruption
> > issue here?
> >

>
> There's no data corruption problem, I just observed that in many
> instances I have data structures with several buffers getting passed
> into functions, and the only manner by which I can tell which function
> modifies which buffer is to inspect the function body/comments. I
> could modify the protoypes to specify that the struct is constant, but
> that of course gives no assurance on the contents of foo->a. I suppose
> the only other solution is to define the structure as having const
> members and making it difficult to assign/modify things. Something
> like:
>
>


It seems that you are trying to create C features with may be available in
C++. I know very little about C++, but IIRC, you can implement data
protection due to the object oriented feature of binding code with data.
The code gets executed for each action on the data. Which means, you can
prevent variables from being multiply assigned, set, reset or whatever...
Perhaps you might look into using a small amount of C++.


Rod Pemberton


 
Reply With Quote
 
Dave Thompson
Guest
Posts: n/a
 
      05-11-2006
On 1 May 2006 12:19:48 -0700, "Bill Pursell" <(E-Mail Removed)>
wrote:

> Suppose I have a structure with many members, and I pass a pointer to
> that structure to a function. I'd like the prototype of the function
> to specify that it will only be changing certain members of the
> function, but I have only the following horrific way to do that. Can
> someone please suggest a better alternative?
>
> #include <stdio.h>
>
> struct foo {
> char *a;
> char *b;
> };
>
> struct const_foo {
> const char *a;
> const char *b;
> };
>
> struct foo_const_a {
> const char *a;
> char *b;
> };
>
> struct foo_const_b {
> char *a;
> const char *b;
> };
>
> void modify_a(struct foo_const_b *f)
> {
> f->a = "a has been changed!";
> }
>
> void modify_b(struct foo_const_a *f)
> {
> f->b = "b has been changed!";
> }
>

Those don't (even) do what you asked for. What you declared const is
the data pointed to by the struct member e.g. a, not the member a
itself. In your modify_a (struct foo_const_b *f)
f->b = something would work with no (required) complaint.
You wanted
struct foo_const_a { char * const a; char * b; };
etc.

As already noted, formally these 'slightly different' structs are not
required to be 'compatible' i.e. laid out the same, although I don't
see any plausible reason an implementation would not do so.

And as you already noted, there is nothing to prevent the called
routine from casting away these 'partial consts', so in general you're
going to have to read the code of those functions anyway. Ideally you
(or whoever) should give the functions, and sometimes their
interface/prototype parameters, names that accurately describe what
they do, so just by looking at them you can tell. That's not always
easy to do however, especially in code that has evolved (and often
deteriorated) over time, changes, and (supposed) enhancements.
If the routines/library you are calling are in really bad shape, you
might be better off just rewriting up to scratch. Your call.

- David.Thompson1 at worldnet.att.net
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
C - Const Struct Members Mas C Programming 1 12-04-2005 11:26 PM
error Message: "only const static integral data members can be initializedinside a class or struct" Susan Baker C++ 2 07-03-2005 12:29 PM
About static const members appearing in another static const definitions Rakesh Sinha C++ 4 01-13-2005 08:11 AM



Advertisments