Compiles but "Stops Responding" Any safer way to use memove?

Hi I am relatively new to c/c++ and have created this function that takes 2 char arrays and compares them by index, by adding in dummy index's where one has them and the other does so they become the same.

When i run my program it can run for a few runs but then stops responding, I think i have tracked the problem down to my if and else if statements where my with printf (4 and 6) come out, as far as i know my memmove statements are unsafe or going out of bounds or something.

Is there a safer/simpler way to move the tail of the arrays to a temp one so that the dummy nodes can be placed and then tail be put back on to the original array?


Thanks in advance.
Last edited on
It is unclear whether you were trying to use C, or C++.

nukem266 wrote:
Any safer way to use memove?
Nope, dealing with raw memory is inherently unsafe.

Could you explain in more detail what you're trying to do? I'm sure there's a much simpler and much safer way to do it.
Hi LB,

I am using a bit of both c++ mainly, although i am very new to both of them.

The function as i said at the top memcpys 2 arrays into from previously made population of 500.

Reason for the copy is because I am manipulating them and do not want to change the originals.

1 into fittestprog -- ( this stays the same throughout a generation of 500)
1 into compared_p ( this is the other 499 best is also included in this. )

I then have to compare both the arrays to each other, to do that i have to add in dummy indexes where they exist in one array and not the other and vice versa.

So for example.

fittestprog{4532412}
compared_p{55326112}

To make them the same i have to add in the nodes deepening on the arity (if you have ever worked with binary trees it is very similar) the code below explains the arity for the next part each token is represented by 1,2,3,4,5,6,7

arity is declared 1 less than it is.

7 being my ghost(dummy) node

fittestprog{45324712}
compared_p{55326112}

for each index of the 2 arrays i compare them if they are the same no need to change if they are different i check the arity by which is greater than the other, at which point the one which is great means i need to add arity difference to the other array.

This is where the if statement and else statement come in because i do not want to overwrite the next index in line of the array I am adding it to too so i cut the tail off using memove and add in the difference of arity and then momove the tail back on.

Then the next index is compared and the the whole process is started again, for these 2 arrays, I think somewhere on the memmove like near the end it it is trying to move the size of the array that is impossible because its already near the end but not sure where to or how to implement it differently.

With arrays that are up to 1000 indexes long which means they can double when adding in the ghost_nodes.

I hope that helped and i did not confuse you further.
Last edited on
Doesn't matter I fixed it my sizes were wrong for the

memmove((void *) &fittestprog[a+1], (void *) &temp2[k], population[best_prog].proglen+population[index].proglen);

the fix

memmove((void *) &fittestprog[a+1], (void *) &temp2[k], sizeof temp2);
Topic archived. No new replies allowed.