Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #106999

Re: Parsing struct memory?

Message-Id <s4teqd-5m6.ln1@walter.foo>
From "Peter \"Shaggy\" Haywood" <phaywood@alphalink.com.au>
Subject Re: Parsing struct memory?
Newsgroups comp.lang.c
Date 2017-03-24 18:06 +1100
References <62daf505-60e2-4dd5-b2c7-c619085dd548@googlegroups.com> <fb4e0c1d-6ef0-4bc1-8136-86199b994903@googlegroups.com> <441stw94gb.fsf@lowell-desk.lan>

Show all headers | View raw


Please excuse the lateness of this followup. Also, although this is
ostensibly replying to Lowell's post, it's actually directed to
supercat and Thiago.

Groovy hepcat Lowell Gilbert was jivin' in comp.lang.c on Fri, 17 Mar
2017 10:39 am. It's a cool scene! Dig it.

> supercat@casperkitty.com writes:
> 
>> On Thursday, March 16, 2017 at 2:58:24 PM UTC-5, Thiago Adams wrote:
>>> Having some struct;
>>> 
>>> struct {
>>>   char c;
>>>   void* p;
>>>   double d;
>>> };

  Of course you need a name for the struct, either a tag or a typedef
name. Otherwise there's not a great deal you can do with it.

>>> Is it possible to parse it just knowing each type?
>>> Something similar of var_arg but for structs.
>>> 
>>> char c = arg(pointer, char);
>>> void* p = arg(pointer, void*);
>>> double d = arg(pointer, double);
>>
>> Nothing in the C Standard would impose sufficient restrictions on
>> structure
>> layout to allow such a construct to be implemented in "portable"
>> code.

  Oh?

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

struct foo
{
  char c;
  void *p;
  double d;
};

void *lookup_foo_member(struct foo *p, const char *type)
{
  void *r = NULL;

  if(0 == (strcmp(type, "char")))
    r = &(p->c);
  else if(0 == (strcmp(type, "void*")))
    r = &(p->p);
  else if(0 == (strcmp(type, "double")))
    r = &(p->d);

  assert(r != NULL);

  return r;
}

#define arg(p, t) (*(t*)lookup_foo_member(p, #t))

int main(void)
{
  struct foo x = {'a', NULL, 1.234};
  char c = arg(&x, char);
  void *p = arg(&x, void*);
  double d = arg(&x, double);

  printf("c = %c, p = %p, d = %f\n", c, p, d);
  return 0;
}

"Sufficient restrictions on structure layout" (whatever that means) has
nothing to do with it. Presumably you mean that the layout of struct
members plays some part in achieving the stated goal of determining
which struct member to access by its type. But, as I've just shown,
nothing could be further from the truth.

>>       It could be implemented in a fashion that would work with the 
>> vast majority of compilers by determining the required alignment for
>> each type and adjusting the "current scan" pointer according to that
>> alignment, make a note of the resulting address, adding the size of
>> the return type to that pointer, and then returning the object stored
>> at the previous address.

  Um..., what?

-- 


----- Dig the NEW and IMPROVED news sig!! -----


-------------- Shaggy was here! ---------------
              Ain't I'm a dawg!!

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Parsing struct memory? Thiago Adams <thiago.adams@gmail.com> - 2017-03-16 12:58 -0700
  Re: Parsing struct memory? Thiago Adams <thiago.adams@gmail.com> - 2017-03-16 13:21 -0700
    Re: Parsing struct memory? Ian Collins <ian-news@hotmail.com> - 2017-03-17 11:16 +1300
      Re: Parsing struct memory? James Kuyper <jameskuyper@verizon.net> - 2017-03-16 22:09 -0400
        Re: Parsing struct memory? Ian Collins <ian-news@hotmail.com> - 2017-03-17 15:23 +1300
        Re: Parsing struct memory? David Brown <david.brown@hesbynett.no> - 2017-03-17 10:11 +0100
          Re: Parsing struct memory? "James R. Kuyper" <jameskuyper@verizon.net> - 2017-03-17 11:35 -0400
    Re: Parsing struct memory? gordonb.7zilv@burditt.org (Gordon Burditt) - 2017-03-16 20:17 -0500
      Re: Parsing struct memory? James Kuyper <jameskuyper@verizon.net> - 2017-03-16 21:56 -0400
  Re: Parsing struct memory? supercat@casperkitty.com - 2017-03-16 13:51 -0700
    Re: Parsing struct memory? Lowell Gilbert <lgusenet@be-well.ilk.org> - 2017-03-16 19:39 -0400
      Re: Parsing struct memory? BartC <bc@freeuk.com> - 2017-03-17 12:16 +0000
      Re: Parsing struct memory? "Peter \"Shaggy\" Haywood" <phaywood@alphalink.com.au> - 2017-03-24 18:06 +1100
        Re: Parsing struct memory? supercat@casperkitty.com - 2017-03-24 14:35 -0700
  Re: Parsing struct memory? Keith Thompson <kst-u@mib.org> - 2017-03-16 19:14 -0700
    Re: Parsing struct memory? scott@slp53.sl.home (Scott Lurndal) - 2017-03-17 12:45 +0000
  Re: Parsing struct memory? scott@slp53.sl.home (Scott Lurndal) - 2017-03-17 12:41 +0000
  Re: Parsing struct memory? Thiago Adams <thiago.adams@gmail.com> - 2017-03-17 08:17 -0700
  Re: Parsing struct memory? Thiago Adams <thiago.adams@gmail.com> - 2017-03-20 05:07 -0700

csiph-web