Re: variable sized unions



"lyrx" <vijju.singh@xxxxxxxxx> writes:
> Hello. I have some legacy code which has
>
> union x {
> struct y;
> char z[64*1024];
> }
>
> I would like to change this to a variable sized union.
>
> I tried
>
> union x {
> struct y;
> char *z;
> }
>
> allocated 2k of memory, and cast it to this union. The code does not
> work. I just wanted to know if I was doing something that is not
> supposed to work.

Arrays and pointers are two entirely different things. Given "char *z",
for z to be valid it has to point to some valid chunk of memory,
either an object or a chunk allocated with malloc().

What exactly are you trying to accomplish?

You might find section 6 of the comp.lang.c FAQ, "Arrays and
Pointers", useful.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.



Relevant Pages

  • Re: realloc() implicit free() ?
    ... >> "The value is that of the named member of the object to which the first ... whole object the effective struct type "in the background". ... > different types that were not in a union. ... > the accesses were being made through struct pointers rather than ...
    (comp.lang.c)
  • Re: realloc() implicit free() ?
    ... ie. the object must be appropriate effective type. ... > whole object the effective struct type "in the background". ... But what happens if we add a union? ... >> the accesses were being made through struct pointers rather than ...
    (comp.lang.c)
  • [RFC: 2.6 patch] drivers/acpi/: possible cleanups
    ... +static void __exit ... union acpi_operand_object ... struct acpi_namespace_node ...
    (Linux-Kernel)
  • Re: Pointer to "base" type - what does the Standard say about this?
    ... One such exception is to access equivalent initial members of structs ... that are union members. ... Don't make the base type a struct member, ... struct s {int i; const int ci;}; ...
    (comp.lang.c)
  • Re: Pointing to high and low bytes of something
    ... >implementation-defined behavior (or possibly undefined behavior). ... >Bytes struct), but some articles I've read seemed to imply otherwise. ... padding bytes are inserted only when they serve a *good* ... You don't need the union at all for this purpose: ...
    (comp.lang.c)