473,993 Members | 2,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dealing with struct padding using a dynamic element

Problem:

I have a structure which needs to store its data in contiguous memory
by there is a dynamic element which can't be defined at compile time.
It needs to be aligned along a 4 byte boundary. This is what my
structure looks like:

START

struct tsBob
{
unsigned int fieldA;
unsigned int fieldB;

unsigned short varLen[0];
}

tsBob* myBob;

myBob = (tsBob*)malloc( sizeof(tsBob) + sizeof(unsigned short) * 1);

END

myBob needs an additional 3 bytes alloc'd to it to be aligned. I
don't want to use %

Finally the question,

Can I use a bitwise something to determine how many extra bytes I need
and malloc that with the
rest of the function call?
Oct 3 '08
14 1845
On Oct 4, 3:57 pm, Hrvoje Prge?a <hrvoje.prg...@ gmail.hrwrote:
Maxim Yegorushkin wrote:
Was it not the original intent of C++ to be compatible with C?
Up to a certain point. The original C++ required function
prototypes, for example, which didn't even exist in C at the
time.
It still is, but the c++ standard is always catching up with
the c standard. Unspecified/flexible length arrays are from
C99, the latest c++ standard is C++03 (which is an updated
version of c++98). It'll probably get in the new standard,
sometime in the future.
I don't think so. It would require at least a minimum of work
(a formal proposal, etc.), and no one thought it worth the
effort.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 6 '08 #11
On Oct 4, 3:20 pm, Hrvoje Prge?a <hrvoje.prg...@ gmail.hrwrote:
James Kanze wrote:
No. All that malloc (or operator new()) guarantee is that the
returned pointer is sufficiently aligned for any type.
I knew about malloc, but are you sure this is true for
operator new? It should know the requirement of the needed
type in advance so the largest aligment seems like a
overspecificati on?
The operator new function doesn't know what type it is being
called for, and must return a pointer suitably aligned for any
function. (See §3.7.4.1.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 6 '08 #12
On Oct 4, 10:14 pm, Ian Collins <ian-n...@hotmail.co mwrote:
Hrvoje Prge¹a wrote:
James Kanze wrote:
No. All that malloc (or operator new()) guarantee is that
the returned pointer is sufficiently aligned for any type.
I knew about malloc, but are you sure this is true for
operator new? It should know the requirement of the needed
type in advance so the largest aligment seems like a
overspecificati on?
No, operator new does not know which type is being allocated,
it is only passed the size.
There's a possibility of ambiguity with the expression "operator
new": it can be interpreted to mean a "new expression" or the
"new operator", or to mean the operator new function. In the
first case, it does know the type.

I specifically wrote "operator new()", with the parenthesis, to
make it clear that I was talking about the function.
Apparently, it wasn't clear enough.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 6 '08 #13
In article
<a4************ *************** *******@f63g200 0hsf.googlegrou ps.com>, James
Kanze <ja*********@gm ail.comwrote:
On Oct 4, 3:20 pm, Hrvoje Prge?a <hrvoje.prg...@ gmail.hrwrote:
James Kanze wrote:
No. All that malloc (or operator new()) guarantee is that the
returned pointer is sufficiently aligned for any type.
I knew about malloc, but are you sure this is true for
operator new? It should know the requirement of the needed
type in advance so the largest aligment seems like a
overspecificati on?

The operator new function doesn't know what type it is being
called for, and must return a pointer suitably aligned for any
function. (See =A73.7.4.1.)
Couldn't it do the same that new char [] and new unsigned char []
(operator, not function) does (C++03 section 5.3.4 paragraph 10)? There,
the memory is suitably aligned for any object of that size or less, since
one can't (legally) store an object of greater size.
Oct 7 '08 #14
On Oct 7, 6:37 pm, blargg....@gish puppy.com (blargg) wrote:
In article
<a479a519-6609-4a9b-976b-7ccd23ad8...@f6 3g2000hsf.googl egroups.com>, James
Kanze <james.ka...@gm ail.comwrote:
On Oct 4, 3:20 pm, Hrvoje Prge?a <hrvoje.prg...@ gmail.hrwrote:
James Kanze wrote:
No. All that malloc (or operator new()) guarantee is
that the returned pointer is sufficiently aligned for
any type.
I knew about malloc, but are you sure this is true for
operator new? It should know the requirement of the needed
type in advance so the largest aligment seems like a
overspecificati on?
The operator new function doesn't know what type it is being
called for, and must return a pointer suitably aligned for
any function. (See =A73.7.4.1.)
Couldn't it do the same that new char [] and new unsigned char
[] (operator, not function) does (C++03 section 5.3.4
paragraph 10)? There, the memory is suitably aligned for any
object of that size or less, since one can't (legally) store
an object of greater size.
The standard says (concerning the return value of an "allocation
functionr", §3.7.3.1/2): "The pointer returned shall be suitably
aligned so that it can be converted to a pointer of any complete
object type and then used to access the object or array in the
storage allocated (until the storage is explicitly deallocated
by a call to a corresponding deallocation function)." The first
part of the sentence is clear: it must be suitably aligned for
any type. The second part makes it a little less clear; it's
obvious that you can't use the pointer to access an object
larger than the ammount of memory allocated.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 8 '08 #15

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

Similar topics

5
17717
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have two questions: 1. Is it generally safe to "overlay" the structure on the buffer, e.g.: unsigned char buffer;
19
3104
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 runtime using malloc.In this last member of the structer "int last" is not
20
3002
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes) struct, I am storing an array of 27 pointers and a void pointer that can point to anything. typedef struct trieNode { struct trieNode *children; // The children nodes void *obj; // The object stored } TrieNode;
10
2452
by: Sean | last post by:
I have a struct that I wrote to test a protocol. The idea I had was to just declare the elements of the struct in the order in which they are sent and received as defined by the protocol. However, writing this struct to a file produces unexpected results. Here is a test struct I wrote: struct Tester { unsigned short first; unsigned int second;
10
3429
by: Mark A. Odell | last post by:
Is there a way to obtain the size of a struct element based only upon its offset within the struct? I seem unable to figure out a way to do this (short of comparing every element's offset with <offset>). What I would like to do is create an API something like this: #include <stddef.h> struct MemMap { unsigned char apple; // 8 bits on my platform
8
1692
by: Mike | last post by:
The following struct, DataStruct, is only part of a larger one that contains additional fields and arrays. I need the explicit layout because this struct is really a union, where some of the missing fields and arrays overlap. What's shown here, though, is sufficient for explaining the error. 290 bytes of data come from a serial device and is to be placed in this struct. Hence, I want this struct to be 290 bytes in size, and, if I'm...
5
4065
by: Hallvard B Furuseth | last post by:
Does struct assignment copy padding bytes? Some compilers do, but I couldn't find anything in the standard which says they must. What I need is for any padding bytes to contan initialized values before fwrite(), to shut up memory debuggers like Valgrind about writing uninitialized data to the file. Simplified code: static const struct S default_value = ...; struct S s, t;
8
4255
by: Chameleon | last post by:
I have a TGA image header struct. TGA has 18 bytes header, so the C struct too. why this return 20? sizeof(TGAHeader) I saw this in many structs. I believe compiler round up the size to 4 multiple.
3
3555
by: vikas talwar | last post by:
Hi All, Can you please explain me how the 'C' compiler allocate memory to 'struct'. Please go thu the example below and pls suggest me the solution for my problem. Here is my structure definition struct my_dev {
0
10416
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10234
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11935
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11502
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10172
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8560
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
5264
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.