Dear all,
I have an existing piece of code with a struct with some PODs.
struct A
{
int x;
int y;
};
This struct is created somewhere and initialized via a memset.
A a;
memset(&a,0,siz eof(A));
Now I want to extend the structure with an object, e.g a vector:
struct A
{
int x;
int y;
std::vector<int > z;
};
The sequence
A a;
memset(&a,0,siz eof(A));
would be fatal now, because I overwrite the instanciated vector a.z.
In reality the struct is very huge with a bunch of PODs inside, so using a
ctor would be possible, but quite a lot of work. (I am also scared to miss
something). How would you proceed to avoid the devastating memset?
I thought of kind of
struct A
{
int x;
int y;
int no_memsetbeyond _this_point;
std::vector<int > z;
};
and
A a;
memset(&a,0,&a. no_memsetbeyond _this_point-&a);
It is not really nice, but I have to avoid as much code-rework as possible
for the moment.
Kind regards,
Patrick
Jan 19 '06
14 8463
"Patrick Kowalzick" <pa************ ***@mapandguide .de> skrev i
meddelandet news:ne******** ************@pr oxy.mapandguide .de... Hello Bo,
Perfect. Thats a rather good solution. Anyway for this case I will change to
struct PodA { PodA() { memset(this,0,s izeof(PodA)); } ... };
But now you have a constructor, so it's not a POD anymore. :-(
Hmm, ok. I call it "public class with only PODs inside" :).
Ok, I didn't explain it properly:
If it's not a POD, you cannot use memset() on it.
That was the original problem! :-)
Bo Persson
Hello Bo, Hmm, ok. I call it "public class with only PODs inside" :). Ok, I didn't explain it properly:
If it's not a POD, you cannot use memset() on it.
Why not? I will take a look in the standard, now, but I was quite sure, that
adding a function to POD-struct will not change the memory structure of this
struct. I see no obvious reason, why this should be dangerous.
If it is dangerous there is still the soution Zara suggests...
That was the original problem! :-)
Not really. The problem was an inner class, which itself may initialize,
inside a structure which is overwirtten by a memset on the outer struct. The
initialization of the inner class is lost (and even worse the instance might
be invalid).
Kind regards,
Patrick
>>> Hmm, ok. I call it "public class with only PODs inside" :). Ok, I didn't explain it properly:
If it's not a POD, you cannot use memset() on it.
Why not? I will take a look in the standard, now, but I was quite sure, that adding a function to POD-struct will not change the memory structure of this struct. I see no obvious reason, why this should be dangerous.
I think, a class with a ctor can still be a POD-class. There fore two cites
from the standard:
"(3.9.10) Arithmetic types (3.9.1), enumeration types, pointer types, and
pointer to member types (3.9.2), and cv-qualified versions of these types
(3.9.3) are collectively called scalar types. Scalar types, POD-struct
types, POD-union types (clause 9), arrays of such types and cv-qualified
versions of these types (3.9.3) are collectively called POD types."
"(9.0.4) {...} A POD-struct is an aggregate class that has no non-static
data members of type non-POD-struct, non-POD-union (or array of such types)
or reference, and has no user-defined copy assignment operator and no
user-defined destructor. Similary, a POD-union is an aggregate union that
has no non-static data members of type non-POD-struct, non-POD-union (or
array of such types) or reference, and has no user-defined copy assignment
operator and no user-defined destructor. A POD class is a class that is
either a POD-struct or a POD-union."
I have no other non-POD typed, nor a copy assignment, nor a dtor. So it is
still a POD? But what about a vtable? Might this still be a POD?
Regards,
Patrick
> I think, a class with a ctor can still be a POD-class. There fore two cites from the standard:
Bullshit :).
"(3.9.10) Arithmetic types (3.9.1), enumeration types, pointer types, and pointer to member types (3.9.2), and cv-qualified versions of these types (3.9.3) are collectively called scalar types. Scalar types, POD-struct types, POD-union types (clause 9), arrays of such types and cv-qualified versions of these types (3.9.3) are collectively called POD types."
"(9.0.4) {...} A POD-struct is an aggregate class that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator and no user-defined destructor. Similary, a POD-union is an aggregate union that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator and no user-defined destructor. A POD class is a class that is either a POD-struct or a POD-union."
I have no other non-POD typed, nor a copy assignment, nor a dtor. So it is still a POD? But what about a vtable? Might this still be a POD?
"[8.5.1.1] An aggregate is an array or a class (clause 9) with no
user-declared constructors (12.1), no private or protected non-static data
members (clause 11), no base classes (clause 10), and no virtual functions
(10.3)."
And for sure, read the FAQ: http://www.parashift.com/c++-faq-lit....html#faq-26.7
Thanks all a lot for you input in this thread. I hope I got it now.
Kind regards,
Patrick
Hello Alf, For now, rename the orginal struct to PodA,
struct PodA { ... };
then derive struct A from that,
struct A: PodA { A(): PodA() {} std::vector<int > z; };
and also check that your compiler supports default-initialization of POD's (unfortunately some don't).
I checked this. Some of my compilers support default-initialization, some
not :(. This is really sad.
So I use now this here (even if there is a difference to memset in the
result):
struct A_PODs{ ... };
struct A : public A_PODs
{
A()
{
static A_PODs zero_init = { 0 };
// static A_PODs zero_init = { }; // would be enough, but does not
work on all compilers.
*(static_cast< A_PODs * >(this)) = zero_init; // is this safe?
}
};
Kind regards,
Patrick This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Joe C |
last post by:
I'm a hobbiest, and made the forray into c++ from non-c type languages about
a year ago. I was "cleaning up" some code I wrote to make it more "c++
like" and have a few questions. I'm comfortable using new/delete when
dealing with arrays, and, so-far haven't used the STL (eg vectors) very much
when dealing with POD. I'm using a class to dump files into. The class
puts the file data into a 32-bit array, then offers both 32-bit and char*...
|
by: Nollie |
last post by:
Say you have a struct:
struct MYSTRUCT
{
int x;
int y;
int w;
int h;
};
|
by: bob_jenkins |
last post by:
{
const void *p;
(void)memset((void *)p, ' ', (size_t)10);
}
Should this call to memset() be legal? Memset is of type
void *memset(void *, unsigned char, size_t)
Also, (void *) is the generic pointer type.
My real question is, is (void *) such a generic pointer type that it
|
by: volunteers |
last post by:
I met a question about memset and have no idea right now. Could anybody
give a clue? Thanks
memset is sometimes used to initialize data in a constructor like the
example below. What is the benefit of initializing this way? Does it
work in this example? Does it work in general ? Is it a good idea in
general?
class A {
public:
|
by: dykeinthebox |
last post by:
Consider the following program:
#include <stdlib.h>
#include <string.h>
int main( void )
{
void *p = malloc( 4 );
if ( p )
{
| |
by: JohnQ |
last post by:
Are a default constructor, destructor, copy constructor and assignment
operator generated by the compiler for a struct if they are not explicitely
defined?
I think the answer is yes, because "there is no difference between a struct
and a class except the public/private access specification" (and a few minor
other things). When I create a class, I always start by declaring the
default constructor, copy constructor and assignment operator...
|
by: JohnQ |
last post by:
The implementation of classes with virtual functions is conceptually easy to
understand: they use vtables. Which begs the question about POD structs: how
are they associated with their member functions in common implementations?
And where is the 'this' ptr tucked away at for POD structs with member
functions?
John
|
by: Martin Wells |
last post by:
I'm trying to come up with a fully-portable macro for supplying memset
with an unsigned char rather than an int. I'm going to think out loud
as I go along. . .
I'll take a sample system before I begin:
CHAR_BIT == 16
sizeof(short) == sizeof(int) == 1
Assume none of the integer types have padding bits
Sign-magnitude
|
by: Gaijinco |
last post by:
I'm having a headache using memset()
Given:
int v;
memset((void*)v, 1, sizeof(v));
Can I be 100% positive than v = 1 for i 0, or there is something
else I have to do?.
|
by: viza |
last post by:
Hi all
Is this assertion guaranteed?
{
void *vptr;
memset( & vptr, 0, sizeof vptr);
assert( NULL == vptr );
}
What about these ones?
|
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...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |