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

Discussion in 'C++' 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(void)
    {
    struct msg_head * msg;
    char *p_temp_buf = new char[2024];
    ...
    msg = (struct msg_head *)p_temp_buf;
    ...
    delete p_temp_buf;
    return 0;
    }
    , Apr 9, 2006
    #1
    1. Advertising

  2. Guest

    or
    the following is always OK?

    int main(void)
    {
    struct msg_head * msg;
    char temp_buf[2024];
    ...
    msg = (struct msg_head *)temp_buf;
    ...

    return 0;
    }
    , Apr 9, 2006
    #2
    1. Advertising

  3. Ben Pope Guest

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

    wrote:
    > 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(void)
    > {
    > struct msg_head * msg;
    > char *p_temp_buf = new char[2024];
    > ...
    > msg = (struct msg_head *)p_temp_buf;


    If it was "ok", you wouldn't need the cast.

    What are you trying to achieve?

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...
    Ben Pope, Apr 9, 2006
    #3
  4. On 2006-04-09, <> wrote:
    > 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(void)
    > {
    > struct msg_head * msg;
    > char *p_temp_buf = new char[2024];
    > ...
    > msg = (struct msg_head *)p_temp_buf;
    > ...
    > delete p_temp_buf;
    > return 0;
    > }
    >


    While it is "nasty", it will work. I can see no potential for your
    struct size to ever increase over 2024 "char"s due to , say,
    alignment.

    But why? Whyt are you trying to do? Save "malloc" time by using a
    global buffer for allsorts of temproary data?
    Richard G. Riley, Apr 9, 2006
    #4
  5. Pavel Guest

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

    wrote:
    > 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(void)
    > {
    > struct msg_head * msg;
    > char *p_temp_buf = new char[2024];
    > ...
    > msg = (struct msg_head *)p_temp_buf;
    > ...
    > delete p_temp_buf;
    > return 0;
    > }
    >

    As per the Standard, it is probably undefined behavior. In practice, it
    is "ok" in the sense you will not access wrong memory. The structure may
    not have its normal (for the compiler/architecture) alignment though
    (this especially often will happen with your second example where you
    use the buffer on stack). This should not be a problem if you operate
    with the character fields only but may be a problem (like bus error) if
    you, for example, assign to or even from such structure as a whole and
    the compiler generates some optimized code. The following hack would
    take care of this problem, most of the time for 64- or less- bit
    architectures:

    const int WHOLE_LENGTH = 2031;
    const int GUARANTEED_LENGTH = 2024;
    char *p_allocated_temp_buf = new char[WHOLE_LENGTH];
    char *p_temp_buf = ((p_allocated_temp_buf - 1) >> 3) << 3;
    if(p_temp_buf != p_allocated_temp_buf) p_temp_buf += 8;
    // ... use p_temp_buf for up to Guaranteed_length
    delete [] p_allocated_temp_buf;
    Pavel, Apr 9, 2006
    #5
  6. Jack Klein Guest

    On Sun, 09 Apr 2006 11:06:30 -0400, Pavel <> wrote in
    comp.lang.c++:

    > wrote:
    > > 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(void)
    > > {
    > > struct msg_head * msg;
    > > char *p_temp_buf = new char[2024];
    > > ...
    > > msg = (struct msg_head *)p_temp_buf;
    > > ...
    > > delete p_temp_buf;
    > > return 0;
    > > }
    > >

    > As per the Standard, it is probably undefined behavior. In practice, it
    > is "ok" in the sense you will not access wrong memory. The structure may
    > not have its normal (for the compiler/architecture) alignment though
    > (this especially often will happen with your second example where you
    > use the buffer on stack). This should not be a problem if you operate
    > with the character fields only but may be a problem (like bus error) if
    > you, for example, assign to or even from such structure as a whole and
    > the compiler generates some optimized code. The following hack would
    > take care of this problem, most of the time for 64- or less- bit
    > architectures:
    >
    > const int WHOLE_LENGTH = 2031;
    > const int GUARANTEED_LENGTH = 2024;
    > char *p_allocated_temp_buf = new char[WHOLE_LENGTH];
    > char *p_temp_buf = ((p_allocated_temp_buf - 1) >> 3) << 3;


    If you think there is a C++ compiler in existence that will compile
    the line above, I think you are mistaken. Even if you can get it to
    compile, the subexpression "p_allocated_temp_buf - 1" invokes
    undefined behavior all by itself. Decrementing a pointer to the start
    of allocated space is undefined.

    > if(p_temp_buf != p_allocated_temp_buf) p_temp_buf += 8;
    > // ... use p_temp_buf for up to Guaranteed_length
    > delete [] p_allocated_temp_buf;
    >


    Complete rubbish.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Apr 9, 2006
    #6
  7. Pavel Guest

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

    Jack Klein wrote:
    ....

    > If you think there is a C++ compiler in existence that will compile
    > the line above, I think you are mistaken. Even if you can get it to
    > compile, the subexpression "p_allocated_temp_buf - 1" invokes
    > undefined behavior all by itself. Decrementing a pointer to the start
    > of allocated space is undefined.

    ....

    >
    > Complete rubbish.
    >

    A bit gross even if formally true. I just wanted to demonstrate the
    idea. The working code follows (the behavior is, of course theoretically
    "undefined" as I mentioned in my first post, but in practice workable
    and even pretty well portable):

    #include <iostream>

    using namespace std;

    char *align_forward(char *ptr, unsigned n) {
    if(!ptr) return ptr;
    return (char *)(((((reinterpret_cast<unsigned long>(ptr) - 1) >> n) +
    1) << n));
    }

    int main() {
    const int WHOLE_LENGTH = 2031;
    const int GUARANTEED_LENGTH = 2024;
    char *p_allocated_temp_buf = new char[WHOLE_LENGTH];
    char *p_temp_buf = align_forward(p_allocated_temp_buf, 3);
    cout << "aligned " << (void *) (p_allocated_temp_buf) << "="
    << (void *) align_forward(p_allocated_temp_buf, 3) << ", "
    << (void *) (p_allocated_temp_buf + 1) << "="
    << (void *) align_forward(p_allocated_temp_buf + 1, 3) << endl;
    delete [] p_allocated_temp_buf;
    return 0;
    }
    Pavel, Apr 9, 2006
    #7
  8. Guest

    Pavel wrote:
    > wrote:
    > > For example:
    > >
    > > the
    > > msg = temp_buf;
    > > is alwawys ok?
    > >
    > > //test_msg.cpp
    > > struct msg_head
    > > {
    > > char a01[4];

    //... (more char[4]s)
    > > };
    > >
    > > int main(void)
    > > {
    > > struct msg_head * msg;
    > > char *p_temp_buf = new char[2024];
    > > ...
    > > msg = (struct msg_head *)p_temp_buf;
    > > ...
    > > delete p_temp_buf;
    > > return 0;
    > > }
    > >

    > As per the Standard, it is probably undefined behavior. In practice, it
    > is "ok" in the sense you will not access wrong memory. The structure may
    > not have its normal (for the compiler/architecture) alignment ..


    Wrong. new char[] must return memory that's suitably allocated for any
    type.
    The reason is that char also serves as C++'s byte type. (Besides, since
    the
    structure only has char[]s, I expect most compilers to align it like a
    char[] )
    HTH,
    Michiel Salters.
    , Apr 10, 2006
    #8
  9. On 9 Apr 2006 07:30:53 -0700, ""
    <> wrote:

    > delete p_temp_buf;


    This should be:

    delete [] p_temp_buf;

    --
    Bob Hairgrove
    Bob Hairgrove, Apr 10, 2006
    #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. Replies:
    5
    Views:
    515
    Flash Gordon
    Apr 9, 2006
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,007
    Ian Collins
    May 9, 2006
  3. Replies:
    3
    Views:
    719
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,286
    Saraswati lakki
    Jan 6, 2012
  5. Tuan  Bui
    Replies:
    14
    Views:
    461
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page