473,395 Members | 2,783 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Could a struct with size 44 bytes point always points to a char array with size 2024 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(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 '06 #1
8 1952
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 '06 #2
ea********@citiz.net 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...
Apr 9 '06 #3
On 2006-04-09, ea********@citiz.net <ea********@citiz.net> 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?
Apr 9 '06 #4
ea********@citiz.net 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;
Apr 9 '06 #5
On Sun, 09 Apr 2006 11:06:30 -0400, Pavel <no****@nospam.com> wrote in
comp.lang.c++:
ea********@citiz.net 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/~a...FAQ-acllc.html
Apr 9 '06 #6
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;
}
Apr 9 '06 #7
Pavel wrote:
ea********@citiz.net 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 '06 #8
On 9 Apr 2006 07:30:53 -0700, "ea********@citiz.net"
<ea********@citiz.net> wrote:
delete p_temp_buf;


This should be:

delete [] p_temp_buf;

--
Bob Hairgrove
No**********@Home.com
Apr 10 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Geetesh | last post by:
Recently i saw a code in which there was a structer defination similar as bellow: struct foo { int dummy1; int dummy2; int last }; In application the above array is always allocated at...
15
by: fix | last post by:
Hi all, I am writing a program using some structs, it is not running and I believe it is because there's some memory leak - the debugger tells me that the code causes the problem is in the malloc...
0
by: William Stacey | last post by:
The following code works, but I can't figure out why. I take a struct with two members, a single byte and byte. I then marshal the whole struct to a byte. I create a new struct (without init'ing...
5
by: eagle_jyjh | last post by:
For example: the msg = temp_buf; is alwawys ok? //test_msg.cpp struct msg_head { char a01;
21
by: softwindow | last post by:
#include "stdio.h" #include "malloc.h" struct student{ int age; char *nms; struct student *next; }; struct student *create(){ int ags=0,size=sizeof(struct student); char *nms=" ";
5
by: desktop | last post by:
I have a function that takes two pointers to an array. The first point to the first element while the other points to the last element. int nums = { 1, 2, 3, 4, 5, 7, 8, 9}; int* result; int...
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
6
by: Urs Thuermann | last post by:
With offsetof() I can get the offset of a member in a struct. AFAICS, it is portable and clean to use this offset to access that member. I need to do something like this struct foo { struct...
18
by: lovecreatesbea... | last post by:
1. The following code snippet uses minus operation on two pointers to calculate the distance between struct members. This is illegal, right? 2. s1 and s2 are type of the same struct S. Can the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.