variable length array (was: question about dynamic memory allocation)

Discussion in 'C Programming' started by Fernando Barsoba, Nov 10, 2005.

  1. Hi,

    I decided to start a new topic about my memory allocation question. One
    of the answers was that variable length arrays are possible using C99. I
    found this topic that also mentions it.

    http://groups.google.com/group/comp...variable length array&rnum=2#f5335d22d6e6fb34

    I did some test, but I get strange results:

    int size_array = sizeof(struct ip) + cnf->msg_length;
    unsigned char packet[size_array];

    And then, when asking the size of the 'packet' variable I get 4. How is
    that? It should be another number (31).. but I guess i'm getting the
    size of type unsigned char. Right?


    Thanks,

    FBM
    Fernando Barsoba, Nov 10, 2005
    #1
    1. Advertising

  2. Fernando Barsoba

    Skarmander Guest

    Fernando Barsoba wrote:
    > I decided to start a new topic about my memory allocation question. One
    > of the answers was that variable length arrays are possible using C99. I
    > found this topic that also mentions it.
    >
    > http://groups.google.com/group/comp...variable length array&rnum=2#f5335d22d6e6fb34
    >
    >
    > I did some test, but I get strange results:
    >
    > int size_array = sizeof(struct ip) + cnf->msg_length;
    > unsigned char packet[size_array];
    >
    > And then, when asking the size of the 'packet' variable I get 4. How is
    > that? It should be another number (31).. but I guess i'm getting the
    > size of type unsigned char. Right?
    >

    Technically there's no way to tell, because we don't know what system
    you're compiling this on.

    That said, you're probably getting the size of an unsigned char*, to
    which `packet' decays.

    If sizeof packet != size_array after the declarations above, your
    compiler does not conform to the C99 standard. gcc in particular does
    not support VLAs; it supports an earlier gcc extension that is similar
    but not identical to C99 VLAs.

    S.
    Skarmander, Nov 10, 2005
    #2
    1. Advertising

  3. Fernando Barsoba

    Artie Gold Guest

    Fernando Barsoba wrote:
    > Hi,
    >
    > I decided to start a new topic about my memory allocation question. One
    > of the answers was that variable length arrays are possible using C99. I
    > found this topic that also mentions it.
    >
    > http://groups.google.com/group/comp...variable length array&rnum=2#f5335d22d6e6fb34
    >
    >
    > I did some test, but I get strange results:
    >
    > int size_array = sizeof(struct ip) + cnf->msg_length;
    > unsigned char packet[size_array];
    >
    > And then, when asking the size of the 'packet' variable I get 4. How is
    > that? It should be another number (31).. but I guess i'm getting the
    > size of type unsigned char. Right?
    >


    Show us the code you used to test.
    [You passed `packet' to a function, didn't you...]

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    http://goldsays.blogspot.com (new post 8/5)
    http://www.cafepress.com/goldsays
    "If you have nothing to hide, you're not trying!"
    Artie Gold, Nov 10, 2005
    #3
  4. Artie Gold wrote:
    > Fernando Barsoba wrote:
    >> Hi,
    >>
    >> I decided to start a new topic about my memory allocation question. One
    >> of the answers was that variable length arrays are possible using C99. I
    >> found this topic that also mentions it.
    >>
    >> http://groups.google.com/group/comp...variable length array&rnum=2#f5335d22d6e6fb34
    >>
    >>
    >> I did some test, but I get strange results:
    >>
    >> int size_array = sizeof(struct ip) + cnf->msg_length;
    >> unsigned char packet[size_array];
    >>
    >> And then, when asking the size of the 'packet' variable I get 4. How is
    >> that? It should be another number (31).. but I guess i'm getting the
    >> size of type unsigned char. Right?
    >>

    >
    > Show us the code you used to test.
    > [You passed `packet' to a function, didn't you...]


    I pass 'packet' to a function later on.. but not for this test. I
    checked the size of 'packet' after
    >>unsigned char packet[size_array];

    and there's where I get the value 4.

    Regarding to the other question, I'm using Eclipse + CDT (and the
    compiler is the one that comes with Cygwin which I am using too).

    FBM
    Fernando Barsoba, Nov 10, 2005
    #4
  5. Re: variable length array (was: question about dynamic memoryallocation)

    Fernando Barsoba <> writes:
    [snip]
    > I did some test, but I get strange results:
    >
    > int size_array = sizeof(struct ip) + cnf->msg_length;
    > unsigned char packet[size_array];
    >
    > And then, when asking the size of the 'packet' variable I get 4. How is
    > that? It should be another number (31).. but I guess i'm getting the
    > size of type unsigned char. Right?


    No, sizeof(unsigned char) is 1 by definition. It looks like it could
    be sizeof(unsigned char*), but that's not

    How exactly did you ask the size of the 'packet' variable? Show us
    some code.

    Here's an example that works properly with gcc 2.95.2, 3.4.4, and
    4.0.2:

    #include <stdio.h>
    int main(void)
    {
    int len = 42;
    char vla[len];
    printf("sizeof vla = %d\n", (int)sizeof vla);
    return 0;
    }

    The output is:

    sizeof vla = 42

    If you passed it as an argument to a function and applied sizeof to
    the parameter, you'd get the size of the pointer, but that's because
    array expressions decay to pointers in most contexts; if you apply
    sizeof directly to the array object, it doesn't decay and you get the
    size of the array.

    --
    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, Nov 10, 2005
    #5
  6. Fernando Barsoba

    Skarmander Guest

    Keith Thompson wrote:
    > Fernando Barsoba <> writes:
    > [snip]
    >
    >>I did some test, but I get strange results:
    >>
    >> int size_array = sizeof(struct ip) + cnf->msg_length;
    >> unsigned char packet[size_array];
    >>
    >>And then, when asking the size of the 'packet' variable I get 4. How is
    >>that? It should be another number (31).. but I guess i'm getting the
    >>size of type unsigned char. Right?

    >
    >
    > No, sizeof(unsigned char) is 1 by definition. It looks like it could
    > be sizeof(unsigned char*), but that's not
    >
    > How exactly did you ask the size of the 'packet' variable? Show us
    > some code.
    >
    > Here's an example that works properly with gcc 2.95.2, 3.4.4, and
    > 4.0.2:
    >

    <snip>
    And there's me badmouthing gcc about not supporting VLAs yet. At least
    this part works. :)

    S.
    Skarmander, Nov 10, 2005
    #6
  7. On Thu, 10 Nov 2005 00:31:56 GMT, Fernando Barsoba
    <> wrote:

    >Hi,
    >
    >I decided to start a new topic about my memory allocation question. One
    >of the answers was that variable length arrays are possible using C99. I
    >found this topic that also mentions it.
    >
    >http://groups.google.com/group/comp...variable length array&rnum=2#f5335d22d6e6fb34
    >
    >I did some test, but I get strange results:
    >
    > int size_array = sizeof(struct ip) + cnf->msg_length;
    > unsigned char packet[size_array];
    >
    >And then, when asking the size of the 'packet' variable I get 4. How is
    >that? It should be another number (31).. but I guess i'm getting the
    >size of type unsigned char. Right?


    Not likely. An unsigned char is guaranteed to have a size of one.


    <<Remove the del for email>>
    Barry Schwarz, Nov 10, 2005
    #7
  8. Re: variable length array (was: question about dynamic memoryallocation)

    Fernando Barsoba <> writes:
    > Artie Gold wrote:
    >> Fernando Barsoba wrote:

    [snip]
    >>> I did some test, but I get strange results:
    >>>
    >>> int size_array = sizeof(struct ip) + cnf->msg_length;
    >>> unsigned char packet[size_array];
    >>>
    >>> And then, when asking the size of the 'packet' variable I get 4. How is
    >>> that? It should be another number (31).. but I guess i'm getting
    >>> the size of type unsigned char. Right?
    >>>

    >> Show us the code you used to test.
    >> [You passed `packet' to a function, didn't you...]

    >
    > I pass 'packet' to a function later on.. but not for this test. I
    > checked the size of 'packet' after
    > >>unsigned char packet[size_array];

    > and there's where I get the value 4.


    That shouldn't be happening.

    Show us the code you used to test. That means posting a complete
    self-contained program that we can compile and run ourselves, along
    with the exact output that it gave you. Without that, we really can't
    help.

    > Regarding to the other question, I'm using Eclipse + CDT (and the
    > compiler is the one that comes with Cygwin which I am using too).


    Cygwin comes with gcc (version 3.4.4 if you've updated it recently).

    --
    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, Nov 10, 2005
    #8
  9. Keith Thompson wrote:
    > Fernando Barsoba <> writes:
    >> Artie Gold wrote:
    >>> Fernando Barsoba wrote:

    > [snip]
    >>>>
    >>>>
    >>> Show us the code you used to test.
    >>> [You passed `packet' to a function, didn't you...]

    >> I pass 'packet' to a function later on.. but not for this test. I
    >> checked the size of 'packet' after
    >> >>unsigned char packet[size_array];

    >> and there's where I get the value 4.

    >
    > That shouldn't be happening.
    >
    > Show us the code you used to test. That means posting a complete
    > self-contained program that we can compile and run ourselves, along
    > with the exact output that it gave you. Without that, we really can't
    > help.


    Ok, I tested the following code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <netinet/ip.h>


    int main(int argc, char **argv) {

    int size_array = sizeof(struct ip) + 11;
    unsigned char packet[size_array];
    // unsigned char ipheader_cpy[sizeof(struct ip)];

    printf("Result for sizeof(packet) is: %d", sizeof(packet));
    exit(0);

    }

    And I got as a result:
    $ ./Test.exe
    Result for sizeof(packet) is: 31

    So, I think I have to assume that it's working correctly. The problem is
    that I'm using Eclipse/CDT and the debugger seems to have problems with
    this kind of code (with VL Arrays). It showed me the '4' that I was
    referring to, in the debugging perspective/window from Eclipse... I
    thought the problem was with the code, but all you made me focus on the
    test.. I guess I relied too much on the debugger...sigh..

    Thanks,

    FBM

    >> Regarding to the other question, I'm using Eclipse + CDT (and the
    >> compiler is the one that comes with Cygwin which I am using too).

    >
    > Cygwin comes with gcc (version 3.4.4 if you've updated it recently).
    >
    Fernando Barsoba, Nov 10, 2005
    #9
    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. kaul
    Replies:
    7
    Views:
    481
    Barry Schwarz
    Sep 9, 2004
  2. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    681
    Eric Sosman
    Mar 22, 2005
  3. Ken
    Replies:
    24
    Views:
    3,837
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    972
    chris
    Oct 28, 2005
  5. Shivanand Kadwadkar

    Dynamic memory allocation with static pointer variable

    Shivanand Kadwadkar, Dec 25, 2010, in forum: C Programming
    Replies:
    7
    Views:
    2,507
    Seebs
    Jan 4, 2011
Loading...

Share This Page