C/C++ Debugging Code in Dev-C++: Solving an Unexpected Disappearance

  • Thread starter Thread starter FrostScYthe
  • Start date Start date
  • Tags Tags
    Code Debugging
Click For Summary
The discussion revolves around a C++ code snippet where the user is trying to allocate memory for arrays but encounters an issue when declaring an uninitialized variable 'n'. The program runs successfully without the 'm' vector, but crashes when it is included, leading to a quick exit. The user realizes that the problem stems from not initializing 'n', which is necessary for proper memory allocation. This highlights the importance of initializing variables before use in C++ to prevent undefined behavior and program crashes.
FrostScYthe
Messages
80
Reaction score
0
Ok, I'm trying this code in Dev-C++

int main(int argc, char **argv)
{
double **a;
double *b;
// double *m;
int n, iter;
double tol;

b = new double[n];

a = new double*[n];
for (int i = 0; i < n + 1; i++)
a = new double[n];

// m = new double[n];

cout << "I'm ready to continue =)!";

system("PAUSE");
return 0;
}

If I omit the m vector, the program will run and not disappear in a second and it will be ready to continue.. but when I declare the m vector without even doing anything with it... it just runs and quits quicker than I can blink my eye...! Why is this happening =(?
 
Technology news on Phys.org
Forget this post, I just realized I had to initialize n before doing any of this ... tehe =)
 
n is undefined.

Edit: Sorry, just saw your follow up.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 30 ·
2
Replies
30
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K