Is this valid?

Discussion in 'C Programming' started by AC, Jun 10, 2005.

  1. AC

    AC Guest

    Is the following code valid?

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

    typedef struct
    {
    int a;
    char s[];
    } test_struct;

    int main(void)
    {
    test_struct *t;
    t = malloc( sizeof *t + 4);
    strcpy(t->s,"oye");
    printf("%s\n",t->s);
    return 0;
    }
    AC, Jun 10, 2005
    #1
    1. Advertising

  2. AC

    Guest

    For C90: no.
    , Jun 10, 2005
    #2
    1. Advertising

  3. On Fri, 10 Jun 2005 12:34:08 -0400, AC wrote:

    > Is the following code valid?
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    >
    > typedef struct
    > {
    > int a;
    > char s[];
    > } test_struct;
    >
    > int main(void)
    > {
    > test_struct *t;
    > t = malloc( sizeof *t + 4);
    > strcpy(t->s,"oye");
    > printf("%s\n",t->s);
    > return 0;
    > }


    I don't see any problems with it. The flexible array feature is not
    standardized until C99 although many implementations supported it in some
    form prior to that (but that would, of course, not be portable).

    Robert Gamble
    Robert Gamble, Jun 10, 2005
    #3
  4. In article <d8cfe0$h0p$>, AC <> wrote:
    >Is the following code valid?
    >
    >typedef struct
    >{
    > int a;
    > char s[];
    >} test_struct;


    If you change the declaration of s to char s[1] the
    trick will work more often, i.e. nearly always.

    It is technically outside the bounds of c89 though.

    Search for "struct hack" using your favorite search engine
    if you would like more information.
    --
    7842++
    Anonymous 7843, Jun 10, 2005
    #4
  5. AC wrote:
    > Is the following code valid?


    It is not valid C89 (C90); it is valid C99.



    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    >
    > typedef struct
    > {
    > int a;
    > char s[];
    > } test_struct;
    >
    > int main(void)
    > {
    > test_struct *t;
    > t = malloc( sizeof *t + 4);
    > strcpy(t->s,"oye");
    > printf("%s\n",t->s);
    > return 0;
    > }
    >
    >
    Martin Ambuhl, Jun 10, 2005
    #5
  6. AC <> wrote:

    > Is the following code valid?


    > (snip "struct hack" code)


    FAQ 2.6 may be of interest:

    http://www.eskimo.com/~scs/C-faq/q2.6.html

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Jun 10, 2005
    #6
  7. AC

    Mark Guest

    "Martin Ambuhl" <> wrote in message
    news:eek:Ukqe.2289$...
    > AC wrote:
    >> Is the following code valid?

    >
    > It is not valid C89 (C90); it is valid C99.

    *May* be valid C99... depends on return from malloc() which is not checked.

    Quick question:
    cfaq question 7.24 (relating to whether allocated memory must be freed prior
    to exit)
    references a section 7.10.3.2 no longer existant (AFAICT) in the current
    standard...
    So what's the official take? Answer remains the same but unsubstantiated?

    Mark
    Mark, Jun 10, 2005
    #7
  8. On Fri, 10 Jun 2005 19:20:00 GMT, Mark
    <> wrote:

    > Quick question:
    > cfaq question 7.24 (relating to whether allocated memory must be freed
    > prior to exit) references a section 7.10.3.2 no longer existant
    > (AFAICT) in the current standard... So what's the official take?
    > Answer remains the same but unsubstantiated?


    Using a reference to a nonexistent object results in undefined behaviour
    <g>.

    Chris C
    Chris Croughton, Jun 10, 2005
    #8
  9. "Mark" <> writes:
    [...]
    > Quick question:
    > cfaq question 7.24 (relating to whether allocated memory must be
    > freed prior to exit) references a section 7.10.3.2 no longer
    > existant (AFAICT) in the current standard... So what's the official
    > take? Answer remains the same but unsubstantiated?


    In C90, 7.10.3.2 describes the "free" function. The corresponding
    section in C99 is 7.20.3.2.

    --
    Keith Thompson (The_Other_Keith) <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.
    Keith Thompson, Jun 10, 2005
    #9
  10. AC

    Mark Guest

    "Keith Thompson" <> wrote in message
    news:...
    > "Mark" <> writes:
    > [...]
    >> Quick question:
    >> cfaq question 7.24 (relating to whether allocated memory must be
    >> freed prior to exit) references a section 7.10.3.2 no longer
    >> existant (AFAICT) in the current standard... So what's the official
    >> take? Answer remains the same but unsubstantiated?

    >
    > In C90, 7.10.3.2 describes the "free" function. The corresponding
    > section in C99 is 7.20.3.2.


    Ah... they must have modified the description substantially then.
    I see nothing in the standard which would suggest free() is optional
    and it seems the previously quoted text 'quality of implementation issue'
    was removed from C99.

    Mark
    Mark, Jun 11, 2005
    #10
  11. "Mark" <> writes:
    > "Keith Thompson" <> wrote in message
    > news:...
    >> "Mark" <> writes:
    >> [...]
    >>> Quick question:
    >>> cfaq question 7.24 (relating to whether allocated memory must be
    >>> freed prior to exit) references a section 7.10.3.2 no longer
    >>> existant (AFAICT) in the current standard... So what's the official
    >>> take? Answer remains the same but unsubstantiated?

    >>
    >> In C90, 7.10.3.2 describes the "free" function. The corresponding
    >> section in C99 is 7.20.3.2.

    >
    > Ah... they must have modified the description substantially then.
    > I see nothing in the standard which would suggest free() is optional
    > and it seems the previously quoted text 'quality of implementation issue'
    > was removed from C99.


    No, there was no significant change in the wording from C90 to C99.
    (I don't think there was any change, but I'm too lazy to compare it.)

    The phrase "quality of implementation issue" in the FAQ is not a quote
    from the standard. Basically, once the program terminates the
    standard doesn't care what happens next.

    --
    Keith Thompson (The_Other_Keith) <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.
    Keith Thompson, Jun 11, 2005
    #11
  12. On Fri, 10 Jun 2005 19:20:00 +0000, Mark wrote:

    >
    > "Martin Ambuhl" <> wrote in message
    > news:eek:Ukqe.2289$...
    >> AC wrote:
    >>> Is the following code valid?

    >>
    >> It is not valid C89 (C90); it is valid C99.

    > *May* be valid C99... depends on return from malloc() which is not checked.
    >
    > Quick question:
    > cfaq question 7.24 (relating to whether allocated memory must be freed prior
    > to exit)
    > references a section 7.10.3.2 no longer existant (AFAICT) in the current
    > standard...
    > So what's the official take? Answer remains the same but unsubstantiated?


    The C language has never required you to free memory before the program
    terminates - strictly conforming programs do not need to do this.

    The standard is concerned about the behaviour of a program while it is
    executing up to the point it generates a termination status. What happens
    after it terminates is of no concern to the standard. In particular it has
    nothing to say about the reclamation of any of the resources that the
    program used. That includes memory allocated for code, automatic objects,
    static objects, allocated objects that have been freed, allocated objects
    that have not been freed, and anything else that the implementation uses
    to support the execution of the program.

    Whether these things are reclaimed or not is therefore an implementation
    issue. You may take a view that freed objects are more likely to be
    reclaimed than unfreed ones, but there's nothing in the standard to
    support that view, and very little evidence from real-world
    implementations either. The fact is that a implementation that doesn't
    reclaim fully will have resource leak and therefore stability issues
    that are rarely if ever acceptable.

    Perhaps a more solid reason for freeing memory before the program
    terminates is to help debugging tools such as memory leak detectors.

    Lawrence
    Lawrence Kirby, Jun 11, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aschel Kritsos

    Valid file types

    Aschel Kritsos, Nov 11, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    583
  2. Ajit
    Replies:
    1
    Views:
    3,583
    Victor Garcia Aprea [MVP]
    Apr 24, 2004
  3. CT

    vbc is not valid command

    CT, Oct 30, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    2,816
  4. Pial
    Replies:
    6
    Views:
    52,360
    Nassos
    Apr 28, 2005
  5. MM
    Replies:
    3
    Views:
    433
    Kai Harrekilde-Petersen
    Jun 19, 2004
Loading...

Share This Page