-4

My problem is I have a header file in which I store all my classes and in my cpp file I have my initializations of these classes, the initialization is not dynamic I have a number of arrays of different classes.

Now the problem is when I started expanding the classes in my header, adding more members and methods,the initialization of 1 specific class start throwing assertions at me of memory overlapping and suggested using memmove() instead of memcpy(), though I use neither of them in that class.

I tried replacing this class with a downgraded version of it that worked in older versions of my source but it still threw the same assertion at me I don't know what part of the code is relevant here is the assertion is being asserted in the initialization of the class without any pointer to what exactly is wrong.

this is my initialization of the class :

Shuriken(cpFloat m,cpVect veloc,cpFloat elast,cpFloat myu) : Spark() , Bang1() , ShurikenFlame()
{
smass = m;
sv = veloc;
se = elast;
su = myu;
ToDraw = false;
Removed = true;
AllocatedBombAnim = false;
DrawFlamedShuriken = false;
DeployFlameBang = false;
PassedLine = false;
hitfruit = false;
FruitIsBeingHit = false;
ToRemoveBody = false;
DummyAdded = false;
HitBossBanana = false;
fruitnum = 11;//11 means it has no fruit
Sec_FlameCounter = 3;

LitShuriken_UVs[0] = CIwFVec2(0, 0);
LitShuriken_UVs[2] = CIwFVec2(1.0/4, 1.0/4);
LitShuriken_UVs[3] = CIwFVec2(1.0/4, 0);
LitShuriken_UVs[1] = CIwFVec2(0, 1.0/4);

Sparkle_UVs[0] = CIwFVec2(0, 0);
Sparkle_UVs[2] = CIwFVec2(1.0/2, 1.0/4);
Sparkle_UVs[3] = CIwFVec2(1.0/2, 0);
Sparkle_UVs[1] = CIwFVec2(0, 1.0/4);

lastPos = cpvzero;
lastAngle = 0;

struct cpVect initShurikenBody_Verts[35] = 
{
    cpv(-128.01,41.26),
    cpv(-58.74,45.42),
    cpv(-47.79,32.04),
    cpv(-40.06,33.94),
    cpv(-20.63,48.29),
    cpv(-16.13,55.67),
    cpv(-25.33,69.7),
    cpv(0,134.67),
    cpv(25.34,70.16),
    cpv(16.14,55.67),
    cpv(20.75,48.1),
    cpv(39.98,34.04),
    cpv(47.96,32.15),
    cpv(58.86,45.38),
    cpv(128.01,41.26),
    cpv(74.55,-2.82),
    cpv(57.95,1.45),
    cpv(52.25,-5.19),
    cpv(45.16,-26.79),
    cpv(45.77,-35.34),
    cpv(61.86,-41.64),
    cpv(79.1,-108.95),
    cpv(20.79,-71.41),
    cpv(19.62,-54.33),
    cpv(11.91,-51.14),
    cpv(-12.02,-51.11),
    cpv(-19.64,-54.26),
    cpv(-20.81,-71.4),
    cpv(-79.11,-108.95),
    cpv(-61.87,-41.6),
    cpv(-45.77,-35.03),
    cpv(-45.18,-26.75),
    cpv(-52.23,-5.35),
    cpv(-57.88,1.31),
    cpv(-74.48,-2.8),
}; 

struct cpVect initShurikenShape_Verts1[5] = 
 {
    cpv(-128.01,41.26),
    cpv(-58.74,45.42),
    cpv(-47.79,32.04),
    cpv(-57.88,1.31),
    cpv(-74.48,-2.8),
 };

struct cpVect initShurikenShape_Verts2[5] = 
 {
    cpv(0,134.67),
    cpv(25.34,70.16),
    cpv(16.14,55.67),
    cpv(-16.13,55.67),
    cpv(-25.33,69.7),
 };

struct cpVect initShurikenShape_Verts3[5] = 
 {
    cpv(47.96,32.15),
    cpv(58.86,45.38),
    cpv(128.01,41.26),
    cpv(74.55,-2.82),
    cpv(57.95,1.45),
 };

struct cpVect initShurikenShape_Verts4[5] = 
 {
    cpv(79.1,-108.95),
    cpv(20.79,-71.41),
    cpv(19.62,-54.33),
    cpv(45.77,-35.34),
    cpv(61.86,-41.64),
 };

struct cpVect initShurikenShape_Verts5[5] = 
 {
    cpv(-79.11,-108.95),
    cpv(-61.87,-41.6),
    cpv(-45.77,-35.03),
    cpv(-19.64,-54.26),
    cpv(-20.81,-71.4),
 };

struct cpVect initShurikenShape_Verts6[4] = 
 {
    cpv(-47.79,32.04),
    cpv(-40.06,33.94),
    cpv(-52.23,-5.35),
    cpv(-57.88,1.31),
 };

struct cpVect initShurikenShape_Verts7[4] = 
 {
    cpv(-20.63,48.29),
    cpv(-16.13,55.67),
    cpv(16.14,55.67),
    cpv(20.75,48.1),
 };

struct cpVect initShurikenShape_Verts8[4] = 
 {
    cpv(39.98,34.04),
    cpv(47.96,32.15),
    cpv(57.95,1.45),
    cpv(52.25,-5.19),
 };

struct cpVect initShurikenShape_Verts9[4] = 
 {
    cpv(45.16,-26.79),
    cpv(45.77,-35.34),
    cpv(19.62,-54.33),
    cpv(11.91,-51.14),
 };

struct cpVect initShurikenShape_Verts10[4] = 
 {
    cpv(-12.02,-51.11),
    cpv(-19.64,-54.26),
    cpv(-45.77,-35.03),
    cpv(-45.18,-26.75),
 };

struct cpVect initShurikenShape_Verts11[10] = 
 {
    cpv(-40.06,33.94),
    cpv(-20.63,48.29),
    cpv(20.75,48.1),
    cpv(39.98,34.04),
    cpv(52.25,-5.19),
    cpv(45.16,-26.79),
    cpv(11.91,-51.14),
    cpv(-12.02,-51.11),
    cpv(-45.18,-26.75),
    cpv(-52.23,-5.35),
 };

for(int i=0;i<5;i++)
    ShurikenShape_Verts1[i] = initShurikenShape_Verts1[i];
for(int i=0;i<5;i++)
    ShurikenShape_Verts2[i] = initShurikenShape_Verts2[i];
for(int i=0;i<5;i++)
    ShurikenShape_Verts3[i] = initShurikenShape_Verts3[i];
for(int i=0;i<5;i++)
    ShurikenShape_Verts4[i] = initShurikenShape_Verts4[i];
for(int i=0;i<5;i++)
    ShurikenShape_Verts5[i] = initShurikenShape_Verts5[i];
for(int i=0;i<4;i++)
    ShurikenShape_Verts6[i] = initShurikenShape_Verts6[i];
for(int i=0;i<4;i++)
    ShurikenShape_Verts7[i] = initShurikenShape_Verts7[i];
for(int i=0;i<4;i++)
    ShurikenShape_Verts8[i] = initShurikenShape_Verts8[i];
for(int i=0;i<4;i++)
    ShurikenShape_Verts9[i] = initShurikenShape_Verts9[i];
for(int i=0;i<4;i++)
    ShurikenShape_Verts10[i] = initShurikenShape_Verts10[i];
for(int i=0;i<10;i++)
    ShurikenShape_Verts11[i] = initShurikenShape_Verts11[i];
for(int i=0;i<35;i++)
    ShurikenBody_Verts[i] = initShurikenBody_Verts[i];
}

I am using Visual Studio 2010 with Marmalade SDK and I am writing in C++.

Thanks for any help,

Anton

  • 4
    How can we help if we can't see the source code? – nogard May 14 '13 at 15:21
  • Are you calling `std::copy()` anywhere? It's implementation might use `memcpy()`. – hmjd May 14 '13 at 15:26
  • mm no I am not using `std::copy()` – user2382170 May 14 '13 at 15:31
  • @user2382170, post the code so we don't have to guess. – hmjd May 14 '13 at 15:32
  • guys the code is as simple as that, just the initialization is my issue, I am initializing this class 10 times as an array and straight from the first object I get overlapping assertion the compiler says there is `memcpy()` fun being called at initialization time of this class, though I cannot find it I am using chipmunk engine as my 2d physics engine its writted in C, I don't see how any of that is using `memcpy()` I'll try to dig deeper, sorry for the lack of info. – user2382170 May 14 '13 at 16:19

2 Answers2

0

Ok, so if we assume that ShurikenShape_Verts1 is either a regular C style array or a C++ style std::array and has the type cpVect, and cpVect is a POD struct:

for(int i=0;i<5;i++)
    ShurikenShape_Verts1[i] = initShurikenShape_Verts1[i];

can be copied as:

memcpy(ShurikenShape_Verts1, initShurikenShape_Verts1, 
       sizeof(initShurikenShape_Verts1));

However, I have made some assumptions here, since you haven't posted enough code. I'd hate to start a "can post this, can you post that" type thread, asking for a large number of other bits of your code, but it also means that I may not have answered the question correctly, since I don't know for sure that your cpVect is indeed a POD struct, for example.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • First of all thank you very much for your answer, ok `ShurikenShape_Verts1` is a simple C array and cpVect is just 2 floats made into a vector type so I guess its POD right? please correct me if I'm wrong, my assertion states I should use `memove()` not `memcpy()`, will `memcpy()` fix my overlapping assertion? from what I've read its better to use `memmove()`, anyway I'll give it a go tomorrow morning hope it will fix my problem, again thanks for helping – user2382170 May 14 '13 at 16:50
  • If there is an overlap somewhere, I can't see it in the code that has been posted. `memmove` works for cases where, for example, we need to extend a string, so we do `memmove(&str[10], &str[12], strlen(str)-10);` - in other words, the start and the end of the copy are both in the same block of memory, so we can't just splat everything in the source straight into the destination. – Mats Petersson May 14 '13 at 16:54
0

(I'd submit a comment but I don't have enough of a stackoverflow "reputation" to do so:-()

I would seriously stand back and look at why you are trying to copy this stuff to start with. Copying lots of stuff is comparatively slow. Far better to use a pointer to the existing array or a reference, which depends on your style.

If by "cpVect is just 2 floats made into a vector" you mean vector, then don't use memcpy or whatever. This is just going to give you heap issues later. Remember that these work by allocating behind the scenes. If you use memcpy you are going to get two objects pointing to the same content, and when the first goes out of scope that content will be freed.

johnfo
  • 1,676
  • 2
  • 17
  • 28