My program crashes, but not in debug mode.

My source code:
http://www.text-upload.com/read.php?id=69316&c=7096036

datebase.txt :
**Current Stock info
Foldable bicycle: 10
Microwave oven: 10
Portable media Player: 10
Warning level: 2


**Client info
Number of Client: 12
Name: Peter Chow
ID: 12345
Money pay by card: 13000
Application: FB
VIP state: false


Name: Mary Chan
ID: 45678
Money pay by card:15000
Application: PMP
VIP state: false

**end of file


When I use (b)[Remove client], and type "Peter Chow", it crashes.
But in debug mode. It won't.
What's the problem?
This articles suggest some causes and some reasons why it might not happen when you use the debugger: http://en.wikipedia.org/wiki/Unusual_software_bug#Heisenbug
This page has some suggestions for finding "Heisenbugs" (a pun on the Heisenberg Uncertainty Principle (though really this is more to do with the Observer Effect in Physics where observing something changes what happens (http://en.wikipedia.org/wiki/Double-slit_experiment#Quantum_version_of_experiment)): http://stackoverflow.com/questions/4067644/strategies-for-finding-heisenbugs
Last edited on
Sorry, I am an absolute beginner.
The heisenbugs seems so difficult to me.
Or just change my question to "Why my program crashes" ?
...
printf("Name: ");
cin.getline($a = new char(), 1024);
printf("ID: ");
cin.getline($b = new char(), 1024); <-----It crashes here.
But why $b crashes, but $a won't?
$a is a pointer to char, you're making it point to a single character. cin.getline() is reading several characters into memory. The first time, it just so happens that it doesn't really matter because that memory isn't being used. The second time, the memory just happens to be in use, so the program crashes when it writes to memory that it doesn't own.

Your program is very strange, because you've mixed C and C++ code together. To be honest, I think you should re-write it, using either C or C++, not both. Also, though gcc seems to allow it, I wouldn't recommend prefixing variable names with a '$'; other compilers will probably complain about it.

I think you need to read up on dynamic memory allocation (DMA). Read this: http://cplusplus.com/doc/tutorial/dynamic/
It's strange because C++ is not my first language.
I've learnt AS2/3, JS and PHP, and I figured out C++ is an entirely different language and it's conflicting with my pre-concept of programming language.
It's rather harder than people who didn't learn anything before(My own opinion).

Anyway, could you please tell me which part is C and which is C++?
using $ as prefix is very convenient because when I type "$", the editor gives me the list of "$" prefixed variables and class.
If you say so, it's OK I won't use $ as you recommended.

So the problem causes by the memory is being used.
Is that $a and $b sharing the same address or cin.getline uses?

oh, and why it could run normally is debug mode?
Last edited on
tgckpg wrote:
I've learnt AS2/3, JS and PHP, and I figured out C++ is an entirely different language and it's conflicting with my pre-concept of programming language.

Definitely, each language is totally different and requires a different way of thinking. It's hard to get used to a new language - programs I write in Python or Perl still look suspiciously like C programs with different syntax - but it's generally not a good idea to mix two languages into one. It makes a huge mess.

tgckpg wrote:
Anyway, could you please tell me which part is C and which is C++?

It's hard because the parts where you've used C-style code are mixed into the C++ parts. You're using char* for strings, for example, or printf for output. It's ok to use C functions in C++ code, that's why the C standard library is provided, but mixing C input/output and C++ input/output is not a good idea, it would be like putting two kinds of fuel into a car and trying to drive.

Maybe you should read over the whole C++ tutorial here: http://cplusplus.com/doc/tutorial/
Then you can go back to it or use this reference if you get stuck: http://cplusplus.com/reference/

tgckpg wrote:
using $ as prefix is very convenient because when I type "$", the editor gives me the list of "$" prefixed variables and class.
If you say so, it's OK I won't use $ as you recommended.

I take it you're using a PHP IDE then? Find a C++ IDE like Code::Blocks or NetBeans, those are quite popular and fully-featured IDEs which will also give you autocomplete options.

tgckpg wrote:
So the problem causes by the memory is being used.
Is that $a and $b sharing the same address or cin.getline uses?

It looks like it. There could be other problems that I didn't see, though.

tgckpg wrote:
oh, and why it could run normally is debug mode?

That's what that stuff about "Heisenbugs" and the Observer Effect is about. The debugger changes the internal state of your program in a way that, just by chance, makes it work.
Thanks for the advices.

Sadly I don't have much time to read up whole tutorials.
The deadline of this assignment is coming... panic because this is the last assignment for the course.

How do I find out which function is C or C++ ?
I've change my code, again, it seems to skip the second "scanf" and looped twice the "while":
1
2
3
4
5
6
7
8
9
...
			char $a[32];
			char $b[32];
			bool $c = false;
			printf("Name: ");
			scanf("%s", $a);
			printf("ID: ");
			scanf("%s", $b);
...


Enter your selection? b
------------------------------------- Remove data
Name: Peter Chow
ID: ------------------------------------- Cannot find data
Enter your selection? Enter your selection?


The reason why I used cin.getline because when I press Enter(without any input) when using scanf, the program seems ignored the input and just give me a new line and wait for a input. That not what I want.

Is there a way that make scanf or C++ function could behave like cin.getline?
It skips it because when you enter the string "Peter Chow" there is actually an extra character added when you print enter - the newline character. You need to put a "\n" at the end of your calls to scanf, like this:
scanf("%s\n", $a);

tgckpg wrote:
How do I find out which function is C or C++ ?

Anything from stdio.h, stdlib.h, etc. is a C function. Anything that is prefixed with std:: is a C++ function (if you remove using namespace std; then you have to add std:: to functions in the standard namespace). Now, though, I think you should focus on getting your code working rather than worrying about mixing C and C++, and then try to go over the tutorial when you have enough time. You could probably go over a section or two in about an hour. I would recommend you copy out all the examples (actually type them, though, don't just copy and paste) so you get a feel for how they work, plus then you'll remember that when you need to read a file you can use the example in the relevant section for help.
Last edited on
Thanks again! You're a good people I've ever seen!
(Please forgive my English, I'm Chinese)
Last edited on
That's ok, and your English is pretty good :)
Last edited on
Topic archived. No new replies allowed.