By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,538 Members | 958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,538 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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.