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

E

eagle_jyjh

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;
...
}
 
P

pete

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.
 
P

pete

pete said:
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.
 
E

eagle_jyjh

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.
 
P

pete

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
 
F

Flash Gordon

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top