Close

////
Last edited on
1
2
3
4
5
6
7
8
9
for (int i = 0; i < size1; i++) {
		if (*(ary1 + i) % 2 == 0) {
			*(iPtrEven + counter1) = *(ary1 + i);
			counter1++;
		} else {
			*(iPtrOdd + counter2) = *(ary1 + i);
			counter2++;
		}
	}


The first time this is hit, counter1 and 2 are both negative values. This is undefined behavior. You're likely writing into memory that isn't yours, or some compilers treat this as indexing from the back going to the front. Why not initialize the counters to 0?

1
2
3
for (int i = 0; i < size1 && i < counter1; i++) {
		*(iPtrEven + i) = *(ary1 + i);
	}

This doesn't make sense. You've already populated iPtrEven with the even elementsin ary1, so why are now sticking everything into here?

1
2
3
4
5
6
for (int i = 0, j= 0; i < counter1 || j < counter2; i++, j++) {
		*(iPtrEven + i) = *(ary2 + i);
		if (i < counter1) {
		*(iPtrOdd + j) = *(ary2 + i);
		}
	}

I would bet the issue is happening here. But I'm not too sure. Your code is not the easiest to read.
bump
Topic archived. No new replies allowed.