Could a struct with size 44 bytes point always points to a char array with size 2048 bytes?

Discussion in 'C Programming' started by eagle_jyjh@citiz.net, Apr 9, 2006.

  1. Guest

    For example:

    the
    msg = temp_buf;
    is alwawys ok?

    //test_msg.cpp
    struct msg_head
    {
    char a01[4];
    char a02[4];
    char a03[4];
    char a04[4];
    char a05[4];
    char a06[4];
    char a07[4];
    char a08[4];
    char a09[4];
    char a10[4];
    char a11[4];
    };

    int main()
    {
    struct msg_head * msg;
    char temp_buf[2048];
    ...
    msg = temp_buf;
    ...
    }
     
    , Apr 9, 2006
    #1
    1. Advertising

  2. pete Guest

    wrote:
    >
    > For example:
    >
    > the
    > msg = temp_buf;
    > is alwawys ok?


    No, not always. There may be alignment issues.

    >
    > //test_msg.cpp


    ..c is probably a better way to end your c program names,
    unless they are really c++ programs, in which case
    you should be posting to the c++ newsgroup.

    > struct msg_head
    > {
    > char a01[4];
    > char a02[4];
    > char a03[4];
    > char a04[4];
    > char a05[4];
    > char a06[4];
    > char a07[4];
    > char a08[4];
    > char a09[4];
    > char a10[4];
    > char a11[4];
    > };
    >
    > int main()


    int main() is obsolecent.
    int main(void) is prototype style, that's more better.

    > {
    > struct msg_head * msg;
    > char temp_buf[2048];
    > ...
    > msg = temp_buf;
    > ...
    > }


    Alignment issues:
    It's possible that your compiler might only allow
    struct msg_head pointers to point to addresses
    which are a multiple of the size of the structure,
    and that an array of char might begin at an address,
    which isn't a multiple of the size of the structure.

    And then, you would have undefined behavior from
    attempting to assigng the address of the array
    to the struct pointer.

    --
    pete
     
    pete, Apr 9, 2006
    #2
    1. Advertising

  3. pete Guest

    pete wrote:
    >
    > wrote:
    > >
    > > For example:
    > >
    > > the
    > > msg = temp_buf;
    > > is alwawys ok?

    >
    > No, not always. There may be alignment issues.
    >
    > >
    > > //test_msg.cpp

    >
    > .c is probably a better way to end your c program names,
    > unless they are really c++ programs, in which case
    > you should be posting to the c++ newsgroup.
    >
    > > struct msg_head
    > > {
    > > char a01[4];
    > > char a02[4];
    > > char a03[4];
    > > char a04[4];
    > > char a05[4];
    > > char a06[4];
    > > char a07[4];
    > > char a08[4];
    > > char a09[4];
    > > char a10[4];
    > > char a11[4];
    > > };
    > >
    > > int main()

    >
    > int main() is obsolecent.
    > int main(void) is prototype style, that's more better.
    >
    > > {
    > > struct msg_head * msg;
    > > char temp_buf[2048];
    > > ...
    > > msg = temp_buf;
    > > ...
    > > }

    >
    > Alignment issues:
    > It's possible that your compiler might only allow
    > struct msg_head pointers to point to addresses
    > which are a multiple of the size of the structure,
    > and that an array of char might begin at an address,
    > which isn't a multiple of the size of the structure.
    >
    > And then, you would have undefined behavior from
    > attempting to assigng the address of the array
    > to the struct pointer.


    Two ways around this:
    1 malloc the char array, instead of using an automatic object.
    pointers returned from malloc are aligned for everything.
    2 make a union from your large array and a single instance
    of the structure in question. Assign the address of that
    union, to your pointer.

    --
    pete
     
    pete, Apr 9, 2006
    #3
  4. Guest

    For the first way,
    the following codes is OK?

    struct msg_head * msg;
    // char temp_buf[2048];
    p_temp_buf = new char[2048];
    ...
    //msg = temp_buf;
    msg = p_temp_buf;
    ...
    delete p_temp_buf;
    }

    thank you.
     
    , Apr 9, 2006
    #4
  5. pete Guest

    wrote:
    >
    > For the first way,
    > the following codes is OK?
    >
    > struct msg_head * msg;
    > // char temp_buf[2048];
    > p_temp_buf = new char[2048];
    > ...
    > //msg = temp_buf;
    > msg = p_temp_buf;
    > ...
    > delete p_temp_buf;
    > }
    >
    > thank you.


    I think that's c++ code.
    try
    news:comp.lang.c++

    --
    pete
     
    pete, Apr 9, 2006
    #5
  6. Flash Gordon Guest

    Re: Could a struct with size 44 bytes point always points to a chararray with size 2048 bytes?

    wrote:
    > For the first way,
    > the following codes is OK?
    >
    > struct msg_head * msg;
    > // char temp_buf[2048];
    > p_temp_buf = new char[2048];


    <snip>

    Not in C it isn't. As someone else said, if you want to talk about C++
    this is not the right place. That's what comp.lang.c++ is for.
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
     
    Flash Gordon, Apr 9, 2006
    #6
    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. aj
    Replies:
    3
    Views:
    4,193
    Jaakko Varteva
    Nov 24, 2005
  2. Michel Claveau - abstraction méta-galactique non t

    Small Problem P 2.4 (line>2048 Bytes)

    Michel Claveau - abstraction méta-galactique non t, Dec 15, 2004, in forum: Python
    Replies:
    3
    Views:
    323
    Michel Claveau - abstraction méta-galactique non t
    Dec 15, 2004
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,062
    Ian Collins
    May 9, 2006
  4. Replies:
    8
    Views:
    506
    Bob Hairgrove
    Apr 10, 2006
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,349
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page