Results 1 to 3 of 3

Thread: My program crashs when I run it

  1. #1
    Join Date
    Mar 2015
    Posts
    24
    Thanks
    20
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Unhappy My program crashs when I run it

    Hi guys
    I'm an aerospace enginer and today wrote a program to use it as a calculator for determining some parameter of boundary layer. it's simple and no need to make it complex. ok, enough. now about the program, this is my program:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtMath>
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. }
    11.  
    12. MainWindow::~MainWindow()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void MainWindow::on_pushButton_clicked()
    18. {
    19.  
    20. // Sample input data
    21. quint32 Mmax = 100, Mest=21;
    22. quint32 Uinf = 10;
    23. double_t Cnu = 0.0002 , Xi = 1 , Xf = 2, Fm1, Yod, st, ta, A, B, C, H1, H, F1, F2, Y[100] , U[100], U0[100], V[100], V0[100], Re, DX, DY=0.00112, Ue=1, DUeDX=-0.05, X, Delt=0.0224, Delst=0.00771, Thetha=0.00297, Cf=0.00297;
    24. Re = Uinf/Cnu;
    25. X=Xi;
    26. quint32 i=0;
    27. QString text = " Program ILBLE\n 2-D Boundary layer, incompressible laminar\n 1ST order\n Explicit Method\n Input: MMAX, Re, Dy\n";
    28. text.append(" ");
    29. QString str = QVariant(Mmax).toString();
    30. text.append(str);
    31. text.append(" ");
    32. str = QVariant(Re).toString();
    33. text.append(str);
    34. text.append(" ");
    35. str = QVariant(DY).toString();
    36. text.append(str);
    37. text.append(" \n Flat plate plus ramp flow\n");
    38.  
    39. for(i=1 ; i <= Mmax ; i++){
    40. U[i]= Ue;
    41. U0[i]=Ue;
    42. V[i]=0;
    43. V0[i]=0;
    44. }
    45. U[1]=0;
    46. U0[1]=0;
    47. Fm1= Mest-1;
    48. for(i=2; i<=Mest; i++){
    49. Yod=(i-1)/Fm1;
    50. U0[i]=Ue*((1.5*Yod)-(0.5*qPow(Yod, 3)));
    51. }
    52. text.append(" X , Delt , Delst , Thetha , Cf , Ue , DUeDX");
    53. str = QVariant(X).toString();
    54. text.append(str);
    55. text.append(" ");
    56. str = QVariant(Delt).toString();
    57. text.append(str);
    58. text.append(" ");
    59. str = QVariant(Delst).toString();
    60. text.append(str);
    61. text.append(" ");
    62. str = QVariant(Thetha).toString();
    63. text.append(str);
    64. text.append(" ");
    65. str = QVariant(Cf).toString();
    66. text.append(str);
    67. text.append(" ");
    68. str = QVariant(Ue).toString();
    69. text.append(str);
    70. text.append(" ");
    71. str = QVariant(DUeDX).toString();
    72. text.append(str);
    73. text.append(" \n");
    74.  
    75. Hezar:
    76.  
    77. DX=0.4*U0[2]*DY*DY*Re;
    78. if(X <= Xf){goto navadnoh;}
    79. if(X+DX > Xf){
    80. DX=Xf-Xf;
    81. }
    82. X=X+DX;
    83. Ue=1.05-(X/20);
    84. DUeDX=-0.05;
    85. U0[Mmax]=Ue;
    86.  
    87. //Explicit
    88.  
    89. st=0.5;
    90. ta=0;
    91.  
    92. for(i=2; i <= Mmax-1 ; i++){
    93. A=(1/Re)-(V0[i]*(DY/2));
    94. B=(1/Re)+(V0[i]*(DY/2));
    95. C=(Ue*DUeDX*(qPow(DY,2)))-2*U0[i]/Re;
    96. H1=0.5*DY/DX;
    97. H=DX/(U0[i]*DY*DY);
    98. U[i]=U0[i]+H*((A*U0[i+1])+(B*U0[i-1])+C);
    99. V[i]=V[i-1]-H1*(U[i]-U0[i]+U[i-1]-U0[i-1]);
    100.  
    101. F1=1-(U[i]/Ue);
    102. F2=F1*U[i]/Ue;
    103. st=st+F1;
    104. ta=ta+F2;
    105.  
    106. }
    107.  
    108. //end
    109.  
    110. Delst=Delst*DY;
    111. Thetha=Thetha*DY;
    112.  
    113. for(i=0;i<=Mmax ; i++){
    114. if(U[i] > 0.99*Ue){
    115. Mest=i;
    116. break;
    117. }
    118. }
    119.  
    120. Delt=(Mest-1)*DY;
    121. Cf= (4*U[2]-U[3])/(qPow(Ue,2)*DY*Re);
    122. if(Cf <= 0.0001){
    123. text.append(" Near seperation at X= ");
    124. str = QVariant(X).toString();
    125. text.append(str);
    126. text.append(" \n");
    127. goto navadnoh;
    128. }
    129.  
    130. str = QVariant(X).toString();
    131. text.append(str);
    132. text.append(" ");
    133. str = QVariant(Delt).toString();
    134. text.append(str);
    135. text.append(" ");
    136. str = QVariant(Delst).toString();
    137. text.append(str);
    138. text.append(" ");
    139. str = QVariant(Thetha).toString();
    140. text.append(str);
    141. text.append(" ");
    142. str = QVariant(Cf).toString();
    143. text.append(str);
    144. text.append(" ");
    145. str = QVariant(Ue).toString();
    146. text.append(str);
    147. text.append(" ");
    148. str = QVariant(DUeDX).toString();
    149. text.append(str);
    150. text.append(" \n");
    151.  
    152. for(i=2 ; i <= Mmax ; i++){
    153. U0[i]=U[i];
    154. V0[i]=V[i];
    155.  
    156. }
    157.  
    158. goto Hezar;
    159.  
    160. navadnoh:
    161.  
    162. text.append(" i , Y , U , V \n");
    163. for(i=1; i<= Mmax ; i++){
    164. Y[i]=(i-1)*DY;
    165.  
    166. str = QVariant(i).toString();
    167. text.append(str);
    168. text.append(" ");
    169. str = QVariant(Y[i]).toString();
    170. text.append(str);
    171. text.append(" ");
    172. str = QVariant(U[i]).toString();
    173. text.append(str);
    174. text.append(" ");
    175. str = QVariant(V[i]).toString();
    176. text.append(str);
    177. text.append(" \n");
    178.  
    179. }
    180.  
    181. ui->textEdit->setText(text);
    182.  
    183.  
    184.  
    185. }
    To copy to clipboard, switch view to plain text mode 

    There is a button in the form of the program that when I click it, it's supposed to do the calculation and show me the result but when I run the program and click on "calculate", get this message:




    What's the problem?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: My program crashs when I run it

    Are you running your program in the debugger? Crashes like this are found by running in the debugger, and when the crash occurs you examine the call stack to see exactly which line the program is executing when it crashes.

    But I can give you a big hint. You're obviously porting this code from FORTRAN, where arrays are indexed starting at 1, so valid indexes are [1, ..., 100] for an array of size 100. In C/C++, arrays are indexed beginning at 0, so valid indexes are [0, ..., 99] for the same size array. For starters, look at the bounds on the indexes in your for() loops.

    But just fixing the for() loops isn't going to make this code run correctly - there are lots of hard-coded indexes (like U[2]. U[3], etc.) which will also need to be changed.

    The QVariant(X).toString() stuff is a little weird. It is more common to convert a number to a string using the QString::number() static methods:

    Qt Code:
    1. text += QString::number( X, 'g', 6 );
    To copy to clipboard, switch view to plain text mode 

    which will convert X to a string with 6 digits of precision and a format suitable for its magnitude. (That is, fixed point or scientific notation, whichever gives the shorter string).

    If you want to format a whole line of output, you can use the QString::arg() syntax:

    Qt Code:
    1. double X = 3.1415927e42;
    2. double Y = 3.1415927;
    3. long Z = 42;
    4. text = QString( "Here is a line with values for X: %1, Y: %2, and Z: %3\n" ).arg( X, 'g', 6 ).arg( Y, 'f', 3 ).arg( Z );
    To copy to clipboard, switch view to plain text mode 

    which will give the line: Here is a line with values for X: 3.141593E+42, Y: 3.141593, and Z: 42
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    Ryan111 (6th May 2017)

  4. #3
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: My program crashs when I run it

    Hello,

    your arrays U, ... have a size of 100, i.e. they provide access to the elements U[0], ..., U[99]. Your for-loop runs with indices 1 through 100 (inclusive), thus you access the arrays out of bound. A debugger will show you where the crashes occur.

    Best regards
    ars

    Edit: a little bit too late :-)

  5. The following user says thank you to ars for this useful post:

    Ryan111 (6th May 2017)

Similar Threads

  1. Replies: 4
    Last Post: 17th September 2012, 15:23
  2. Replies: 0
    Last Post: 20th August 2010, 13:45
  3. Replies: 1
    Last Post: 30th April 2010, 13:25
  4. running external console program by gui program
    By alireza.mixedreality in forum Qt Programming
    Replies: 4
    Last Post: 24th April 2010, 18:05
  5. Replies: 7
    Last Post: 19th January 2008, 15:29

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.