c99/c89

Discussion in 'C Programming' started by shaanxxx, Aug 24, 2006.

  1. shaanxxx

    shaanxxx Guest

    what is the problem with following programme.

    int * myMalloc(int Size)
    {
    int a[Size];
    return a;
    }

    can we use sizeof operator to find sizeof array 'a';

    Thanks
    shaan.
     
    shaanxxx, Aug 24, 2006
    #1
    1. Advertisements

  2. shaanxxx

    CBFalconer Guest

    You are returning something that no longer exists.
     
    CBFalconer, Aug 24, 2006
    #2
    1. Advertisements

  3. In c99, yes.

    You are however returning a pointer to something that
    doesn't exist when the function finishes. Don't do that.
     
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Aug 24, 2006
    #3
  4. shaanxxx

    Chris Dollin Guest

    Nit: they're returning a /pointer/ to something that no longer exists.

    Note: this assumes C99 (or a false friend with variable-length
    arrays).
     
    Chris Dollin, Aug 24, 2006
    #4
  5. shaanxxx

    jaysome Guest

    No, if I understand your question. Even if you avoid the undefined
    behavior by doing something like this:

    int * myMalloc(int Size)
    {
    static int a[Size];
    return a;
    }

    there is no standard way to find the "sizeof" 'a' usng only the return
    value from the function myMalloc.

    Best regards
     
    jaysome, Aug 24, 2006
    #5
  6. shaanxxx

    shaanxxx Guest

    In C++ and pre-C99 versions of C, an array's size must be a constant
    integral expression so that it can be calculated at compile-time. In
    C99, this restriction was relaxed. An array's size must be an integral
    expression, not necessarily a constant one. This allows you to declare
    a variable-length array or an array whose size is determined at
    runtime. For example:
    void func(int sz)
    {
    int arr[sz]; // sz ins't const; allowed only in C99
    }

    The size of arr may be different for every func() call. For example:
    int main()
    {
    int size;
    printf("enter array's size : ");
    scanf("%d", &size);
    func(size);
    }

    To support variable-length arrays, C99 adds the notation [*] in a
    function's parameter list to indicate a variable-length array. In
    addition, C99 extends the functionality of sizeof. When applied to a
    variable-length array, it calculates the its size at runtime. In my
    opinion, this is the most daring change in C99. Not only does it alter
    the semantics of a built-in operator from compile-time to runtime
    operation (only when using variable-length arrays), it's also a "quiet
    change" -- there's no explicit syntactic clue to distinguish between
    the two forms.

    ref:
    http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=215&rl=1
     
    shaanxxx, Aug 24, 2006
    #6
  7. shaanxxx posted:

    Get a C book and learn about the scope of variables.
     
    Frederick Gotham, Aug 24, 2006
    #7
  8. shaanxxx

    Simon Biber Guest

    That's not allowed either. Variable length arrays may not have static
    duration.

    C99 6.7.5.2 Array declarators, Example 4 says
    "Array objects declared with the static or extern storage-class
    specifier cannot have a variable length array (VLA) type."

    However, examples are not normative. Is there normative text that
    expresses this requirement? Is it a constraint violation?
     
    Simon Biber, Aug 24, 2006
    #8
  9. shaanxxx

    shaanxxx Guest

    Thanks , I put this question for discussion. Definately i will do what
    you suggested me.
     
    shaanxxx, Aug 24, 2006
    #9
  10. shaanxxx

    Bill Pursell Guest

    Double nit:
    the function is returning a pointer to something that going to cease to
    exist before the caller will receive the value.
     
    Bill Pursell, Aug 24, 2006
    #10
  11. Yes, it is a constraint violation:
    6.7.5.2p2:
    "If an identifier is declared to be an object with static storage
    duration, it shall not have a variable length array type."

    Robert Gamble
     
    Robert Gamble, Aug 24, 2006
    #11
  12. Scope has little to do with it. The issue here is the *lifetime* of
    the variable, not its scope.
     
    Keith Thompson, Aug 30, 2006
    #12
  13. shaanxxx

    shaanxxx Guest

    Get a C book and learn about the scope of variables. :)
     
    shaanxxx, Aug 30, 2006
    #13
  14. Please don't snip attribution lines, and provide enough context so
    your followup makes sense on its own. I happen to know what you're
    talking about because I've read the previous articles in the thread;
    if I hadn't, your followup wouldn't have made any sense at all.
     
    Keith Thompson, Aug 30, 2006
    #14
  15. Keith Thompson posted:

    They are equivalent, are they not? As per my current understanding, an
    object's lifetime ends when it goes out of scope.
     
    Frederick Gotham, Aug 30, 2006
    #15
  16. shaanxxx

    P.J. Plauger Guest

    int f()
    {
    int a; /* lifetime ends at end of block */
    static int b; /* lifetime ends at program termination */
    extern int c; /* lifetime ends at program termination */
    }

    Scope is about visibility, not lifetime.

    P.J. Plauger
    Dinkumware, Ltd.
    http://www.dinkumware.com
     
    P.J. Plauger, Aug 30, 2006
    #16
  17. shaanxxx

    Guest Guest

    In the general case, this is not true. A really simple example:

    #include <stdio.h>

    char *f(void) {
    static char hello[] = "Hello, world!";
    return hello;
    }

    int main(void) {
    puts(f()); /* valid, hello's lifetime has not ended */
    puts(hello); /* invalid, no object "hello" in scope */
    }
     
    Guest, Aug 30, 2006
    #17
  18. No, they are related but not equivalent.
    Scope defines where an *identifier* is visible to the program.
    Lifetime refers to the part of the program where an *object* is
    guaranteed to exist. They are not always the same, for example, a
    static object declared inside a function has a lifetime of the entire
    program execution and can be accessed outside the function via
    pointers. The identifier for the object defined in the function has a
    scope limited to the function.

    Robert Gamble
     
    Robert Gamble, Aug 30, 2006
    #18
  19. No, they're not equivalent. Think about static objects.

    Get a copy of the standard (the latest post-C99 draft is n1124.pdf)
    and read sections 6.2.1 (Scopes of identifiers) and 6.2.4 (Storage
    durations of objects).
     
    Keith Thompson, Aug 30, 2006
    #19
  20. shaanxxx

    pete Guest

    Scope is related to linkage.
    Lifetime has nothing to do with linkage.
     
    pete, Aug 31, 2006
    #20
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.