470,819 Members | 1,603 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,819 developers. It's quick & easy.

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

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 '06 #1
5 1922
ea********@citiz.net 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
Apr 9 '06 #2
pete wrote:

ea********@citiz.net 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
Apr 9 '06 #3

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 '06 #4
ea********@citiz.net 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
Apr 9 '06 #5
ea********@citiz.net 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
Apr 9 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by fix | last post: by
reply views Thread by William Stacey | last post: by
21 posts views Thread by softwindow | last post: by
5 posts views Thread by desktop | last post: by
6 posts views Thread by Urs Thuermann | last post: by
18 posts views Thread by lovecreatesbea... | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.