Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #106347
| Newsgroups | comp.lang.c |
|---|---|
| Date | 2017-03-17 08:17 -0700 |
| References | <62daf505-60e2-4dd5-b2c7-c619085dd548@googlegroups.com> |
| Message-ID | <65c4bc78-bc2d-4eea-bef8-e75720d5d5e5@googlegroups.com> (permalink) |
| Subject | Re: Parsing struct memory? |
| From | Thiago Adams <thiago.adams@gmail.com> |
On Thursday, March 16, 2017 at 4:58:24 PM UTC-3, Thiago Adams wrote:
> Having some struct;
>
> struct {
> char c;
> void* p;
> double d;
> };
>
> 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);
>
> I was trying https://en.wikipedia.org/wiki/Data_structure_alignment
>
> for x86 this is working on visual studio to return the next pointer.
>
> #define NEXT(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
>
> For x64 not. Changing int for __int64 doesn't work as well.
This one is working on x86 and x64.
The previous macro was copied from va_arg x86 and I think on the stack the alignment was always the same as int.
I am comparing the offset with the calculated one.
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#define NEW_OFFSET(offset, type) (((offset) + sizeof(type) - 1) & ~(sizeof(type) - 1))
int main()
{
struct X {
char a1;
char a2;
void* a3;
char a4;
} x;
int i = 0;
i = offsetof(struct X, a1);
printf("%d %d\n", i, (int) NEW_OFFSET(i,char));
i = offsetof(struct X, a2);
printf("%d %d\n", i, (int)NEW_OFFSET(i, char));
i = offsetof(struct X, a3);
printf("%d %d\n", i, (int)NEW_OFFSET(i, void*));
i = offsetof(struct X, a4);
printf("%d %d\n", i, (int)NEW_OFFSET(i, char));
printf("\n");
return 0;
}
For my specific problem, I can build the struct. (build the memory) and doing this probably the program is very portable.
I need to copy values to heap and then later to copy from heap to stack.
I am not sure if I must used the correct alignment or if it is optional.
To cast some memory for double for instance, does it need to be aligned?
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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