Originally Posted by
pyroknife
Ok this is probably a stupid question. But, if I don't want to compile a part of the program, while can't I just take that part out and keep it in another file?
Does Debug actually tell you where the problem occurs?
There would be a chance that you put it back in your code incorrectly if you are copying and pasting - Especially if you are modifying code as you go.
There is something that does help you debug -> It's called "assert" and it works in a similar way.
Basically, you include assert.h - When you are making your program, you put asserts in to make sure all your assumptions are correct. (For example, in another thread (I can't remember which one) I have suggested that the OP asserts that a variable is never 0)
When the algorithm is working and you know it won't be 0, you define NDEBUG at the top of your code. All asserts are then optimised out of existence.
I'll give you an example if you wanted to get it working - Basically I've put in a few errors for you to find using the asserts. This little mess of a program was supposed to find the element in the array before '9' and print it out.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int get_preceding(int arr[], size_t arr_size, int element);
int main(void)
{
int my_arr[10] = {3, 6, 1, 4, 5, 2, 7, 8, 9, 10};
int i;
for (i=0; i<15; i++)
{
int preceding_element;
preceding_element = get_preceding(my_arr, sizeof(my_arr)/sizeof(*my_arr), i);
if (my_arr[i]==9)
{
printf("%d", preceding_element);
}
}
return EXIT_SUCCESS;
}
int get_preceding(int arr[], size_t arr_size, int element)
{
/* These asserts will automatically go when NDEBUG is defined */
assert(arr != NULL);
assert((element - 1) >= 0);
assert(element < arr_size);
/* This section has been manually designated to go
when NDEBUG is defined via the line below (finishes at #endif) */
#ifndef NDEBUG
// Once the program is running, I won't care about this...
printf("(%d) ", arr[element - 1]);
#endif
return arr[element - 1];
}
When you are confident that the program is working, you can define NDEBUG and all the asserts are optimised out of your code.
I like to make all my debug stuff around the define of NDEBUG -> That way it fits in nicely with the assert library.
[edit]
I forgot to say
#ifndef -> "if not defined"
[/edit]
[edit]
I reread the post and decided that it needed a few comments
[/edit]