Discussion:
[scribus] a development question on initializing ItemMultipleDuplicateData
ale rimoldi
2018-09-10 09:32:45 UTC
Permalink
hi jean and craig

i have a question to you:

in scribusMainWindow::duplicateItem() mdData is
initialized through a memset():

ItemMultipleDuplicateData mdData;
memset(&mdData, 0, sizeof(mdData));

https://github.com/scribusproject/scribus/blob/master/scribus/scribus.cpp#L6431

on the one side, usertaskstructs.h does not seem to be explicitly
imported which is a bit odd to me.

on the other side, for the extension to multiple duplicate i've
programmed (and which i will present very soon: yeah!) i needed to add
a string to ItemMultipleDuplicateData and now memset() does not work any
more and gives me the error:

/home/ale/src/scribus-github/scribus/scribus.cpp: In member function
‘void Scri busMainWindow::duplicateItem()’:
/home/ale/src/scribus-github/scribus/scribus.cpp:6438:35: warning:
‘void* memse t(void*, int, size_t)’ clearing an object of type ‘struct
ItemMultipleDuplicate Data’ with no trivial copy-assignment; use
assignment or value-initialization i nstead [-Wclass-memaccess]
memset(&mdData, 0, sizeof(mdData));

my question: can i replace the two lines above with:

ItemMultipleDuplicateData mdData{};

?

in my eyes, it does the same... but i don't know much about memset()...

ciao
a.l.e

___
Scribus Mailing List: ***@lists.scribus.net
Edit your options or unsubscribe:
http://lists.scribus.net/mailman/listinfo/scribus
See
Václav Šmilauer
2018-09-14 10:51:15 UTC
Permalink
Although I am not the adressee of the question, mu 2/100€:
memset to zero makes only sense on POD-types (c-compatible layout).
Those have trivial copy-assignment (bitwise-copyable). String is not a
POD type (unlike char*), as it contains opaque internal data (like
length, pointer to the buffer, maybe more) and thus setting it to 0
will not do anything useful.
Post by ale rimoldi
ItemMultipleDuplicateData mdData{};
No. Unless you provide default constructor which will set to zero all
POD-types contained in the struct (non-POD types like strings will be
initialized using their default ctors), and then you can omit the "{}"
as well (If you upgraded to c++11, you can put (POD and non-POD)
initilizers inline). In that case, you can also delete the memset(...)
because that will have been done by the ctor already.

Like this:

struct ItemMultipleDuplicateData
{
// default ctor
ItemMultipleDuplicateData():
type(0),copyCount(0),copyShiftOrGap(0),copyShiftGapH(0),...{};
int type;
int copyCount;
int copyShiftOrGap;
double copyShiftGapH;
double copyShiftGapV;
double copyRotation;
int gridRows;
int gridCols;
double gridGapH;
double gridGapV;
};


or like this (c++11):

struct ItemMultipleDuplicateData{ int type=0; int
copyCount=0; int copyShiftOrGap=0; double
copyShiftGapH=0.; double copyShiftGapV=0.; double
copyRotation=0.; int gridRows=0; int gridCols=0; d
ouble gridGapH=0.; double gridGapV=0.;};


Cheers, v.
Post by ale rimoldi
hi jean and craig
in scribusMainWindow::duplicateItem() mdData isinitialized through a
ItemMultipleDuplicateData mdData;memset(&mdData, 0, sizeof(mdData));
https://github.com/scribusproject/scribus/blob/master/scribus/scribus
.cpp#L6431
on the one side, usertaskstructs.h does not seem to be
explicitlyimported which is a bit odd to me.
on the other side, for the extension to multiple duplicate
i'veprogrammed (and which i will present very soon: yeah!) i needed
to adda string to ItemMultipleDuplicateData and now memset() does not
/home/ale/src/scribus-github/scribus/scribus.cpp: In member
function‘void Scri
busMainWindow::duplicateItem()’:/home/ale/src/scribus-
github/scribus/scribus.cpp:6438:35: warning:‘void* memse t(void*,
int, size_t)’ clearing an object of type ‘structItemMultipleDuplicate
Data’ with no trivial copy-assignment; useassignment or value-
initialization i nstead [-Wclass-memaccess] memset(&mdData, 0,
sizeof(mdData));
?
in my eyes, it does the same... but i don't know much about
memset()...
ciaoa.l.e
or unsubscribe:http://lists.scribus.net/mailman/listinfo/scribusSee
also:http://wiki.scribus.nethttp://forums.scribus.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.scribus.net/pipermail/scribus/attachments/20180914/6f380a74/attachment.html>
___
Scribus Mailing List: ***@lists.scribus.net
Edit your options or unsubscribe:
http://lists.scribus.net/mailman/listinfo/scribus
See also:
http://wiki.scribus.ne
ale rimoldi
2018-09-17 08:21:30 UTC
Permalink
hi václav,

thank you for the precious comment!
Post by Václav Šmilauer
and then you can omit the "{}"
personally, i like to see the "{}"s when initializing... to show that i
am caring about how the values are initialized (in this case i did
wrong, but i still was caring!)

am i doing it wrong? (ok, there is little that can be defined as
**wrong** in c++... : - )
Post by Václav Šmilauer
as well (If you upgraded to c++11, you can put (POD and non-POD)
initilizers inline). In that case, you can also delete the memset(...)
because that will have been done by the ctor already.
since c++ 11 is ok in scribus, now, i've initialized the members inline:

https://github.com/aoloe/scribus/commit/fbc8e2144b6870c3e56bc1cfd12feaa6cbf05860

ciao && thanks!
a.l.e

___
Scribus Mailing List: ***@lists.scribus.net
Edit your options or unsubscribe:
http://lists.scribus.net/mailman/listinfo/scribus
See also:
http://wiki.scribus.net
http://forums.sc

Loading...