user2242167
user2242167

Reputation: 55

Why do some C programs work in debug but not in release?

Ok,

So I am stuck here. I have code for a program that systematically executes people standing in a circle based off an algorithm, but I am having a problem with it crashing in release mode. My code runs fine if I run it using the debugger (codeblocks), but if I don't it crashes. I looked around online, and the only thing I am finding is unintialized variables, but I tried immediately setting values for my variables at declaration and it didn't fix the problem.

If anyone can see what my problem is, I would greatly appreciate help.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

// if the program does not work, please run in debugger mode. It will work.

void remove_person(int** array, int arraySize, int position)
{
    int i;
    for (i = 0; i < arraySize; ++i)
        printf("%d ", (*array)[i]);
    printf("\n");

    int* temp = malloc((arraySize - 1) * sizeof(int)); // create temporary array smaller by one element

    memmove(temp,*array,(position+1)*sizeof(int)); // copy entire array before position

    memmove(temp+position,(*array)+(position+1),(arraySize - position)*sizeof(int)); // copy entire array after postion

    for (i = 0; i < arraySize - 1; ++i)
        printf("%d ", (temp)[i]);
    printf("\n");

    free (*array);
    *array = temp;
}


int kill(int** a, int n)
{
    int pos = 0;
    int round = 1;
    while(n > 1)
    {
        pos = pos + 2 - (round % 2);

        while(pos >= n)
            pos = pos - n;

        remove_person(a,n,pos);
        n--;

        while(pos >= n)
            pos = pos - n;
        round++;
    }
    return *a[0];
}

void main()
{
    int n, survivor, i;
    int* people;

    printf("Enter number of people for Russian Roulette: \n");
    scanf("%d", &n);

    people = (int*) malloc(n*sizeof(int));

    for(i=0; i < n; i++)
    {
        people[i] = i;
    }

    survivor  = kill(&people, n);
    printf("The survivor is person #%d\n", survivor);
}

Upvotes: 5

Views: 9655

Answers (2)

nneonneo
nneonneo

Reputation: 179452

The basic answer to the title question ("Why do some C programs work in debug but not in release?") is "when they invoke undefined behaviour".

Here, in

memmove(temp,*array,(position+1)*sizeof(int)); // copy entire array before position

memmove(temp+position,(*array)+(position+1),(arraySize - position)*sizeof(int)); // copy entire array after postion

you copy too much. To see why, observe that the first memmove copies to temp[0], temp[1], ..., temp[position], and the second copies to temp[position], temp[position+1], ..., temp[position+arraySize-position-1] = temp[arraySize-1] (note the overlap at temp[position]). But temp only has space for arraySize-1 elements -- you copied one more than it was allowed to hold, so you get undefined behaviour.

It probably works in debug but not release mode because the heap is laid out differently (debug-mode allocators may pad the allocations with extra space to catch bugs like this when running under a debugger or profiler).

Upvotes: 7

Paul
Paul

Reputation: 21975

The program segfaults if I enter 4 (or even numbers higher than 4) as input, but I won't go into the code to see why that happens.

Apart from that the program works just fine, the problem is that you don't see the output because I think you run it on windows.

That being said you should add something like scanf("%d", &n); at the end or run cmd.exe, go to the directory that holds the executable and run it from there.

Upvotes: 0

Related Questions