-3

The code starts with declaring various arrays with a size that is pre-calculated, and will be used in the rest of the program. However, after a certain point in the list of declarations, C++ will fail to generate any output even after a successful compilation. After the comment in the middle of the code, no outputs can be generated. I have tried simple outputs like "cout" and writing in a file.

Edit: I have added a sample output written by one of the answers to demonstrate. The program just runs and does not generate anything. This is the terminal output: "

PS C:\Users\umroot.COLLAR\projects\CrackHole> g++ .\Peridynamics.cpp -o peri
PS C:\Users\umroot.COLLAR\projects\CrackHole> .\peri.exe
PS C:\Users\umroot.COLLAR\projects\CrackHole>
#include <math.h>
#include <iostream>
#include <vector>
#include <string>
#include <conio.h>

// #include "Ellipse.h"
#include <fstream>

using namespace std;

int main () {
    float length = 0.5;
    float width = 0.5;
    float radiusMajor = 0.05;
    float radiusMinor = 0.05;
    double ellipseCurvature = radiusMinor * radiusMinor / radiusMajor;
    float radiusPath = 0.08;
    int dt = 1;
    const double ELASTIC_MODULUS = 200e9;
    const float POISSON_RATIO = 0.3;
    const int NumofDiv_x = 100;
    const int NumofDiv_y = 100;
    int timeInterval = 2500;
    const double appliedPressure = 500e7;
    int initialTotalNumMatPoint = NumofDiv_x * NumofDiv_y;
    int maxFam = 200;
    float dx = length / NumofDiv_x;
    float delta = 3.015 * dx;
    float thick = dx;
    float volCorrRadius = dx / 2;
    const double SHEAR_MODULUS = ELASTIC_MODULUS / (2 * (1 + POISSON_RATIO));
    const double BULK_MODULUS = ELASTIC_MODULUS / (2 * (1 - POISSON_RATIO));
    const double ALPHA = 0.5 * (BULK_MODULUS - 2 * SHEAR_MODULUS);
    float area = dx * dx;
    float volume = area * thick;
    const float BCD = 2 / (M_PI * thick * pow(delta, 4));
    int temp = floor(9 * M_PI * initialTotalNumMatPoint);
    float nodeFam[100000][3] = {0.0};
    int nnum = 0;
    float coord_excess[initialTotalNumMatPoint][2] = {0.0};
    int path_horizontal[NumofDiv_x] = {0};
    // Ellipse centerHole(0, 0, radiusMajor, radiusMinor);
    // Ellipse leftTip((-1) * radiusMajor, 0, 0.005, 0.005);
    // Ellipse rightTip(radiusMajor, 0, 0.005, 0.005);
    float coordx = 0.0;
    float coordy = 0.0;
    int counter = 0;
    for (int i = 0; i < NumofDiv_x; i++) {
        for (int j = 0; j < NumofDiv_y; j++) {

            coordx = (length / 2) * (-1) + (dx / 2) + i * dx;
            coordy = (width / 2) * (-1) + (dx/2) + j * dx;
            // if (centerHole.InEllipse(coordx, coordy)){
            //     continue;
            //  }

             if (abs(coordy) <= dx && coordx >= 0) {
                path_horizontal[counter] = nnum;
                counter++;
            }
            coord_excess[nnum][0] = coordx;
            coord_excess[nnum][1] = coordy;
            nnum++;
        }
    }

    int totalNumMatPoint = nnum;
    float coord[totalNumMatPoint][2] = {0.0};

    for (int j = 0; j < 2; j++ ) {
        for (int i = 0; i < totalNumMatPoint; i++) {
            coord[i][j] = coord_excess[i][j];
        }
    }

    int numFam[totalNumMatPoint] = {0};
    int pointFam[totalNumMatPoint] = {0};
    float PDForce[totalNumMatPoint][2] = {0.0};
    float bodyForce[totalNumMatPoint][2] = {0.0};
    float PDforceold[totalNumMatPoint][2] = {0.0};
    float PD_SED_Distortion[totalNumMatPoint][2] = {0.0};
    float surCorrFactorDilatation[totalNumMatPoint][2] = {0.0};
    float surCorrFactorDistorsion[totalNumMatPoint][2] = {0.0};
    float disp[totalNumMatPoint][2] = {0.0};
    float totalDisp[totalNumMatPoint][2] = {0.0};
    float vel[totalNumMatPoint][2] = {0.0};
    
    // AFTER THIS POINT DOWNWARDS, NO OUTPUTS WILL BE GENERATED

    float velhalfold[totalNumMatPoint][2] = {0.0};
    float velhalf[totalNumMatPoint][2] = {0.0};
    float massvec[totalNumMatPoint][2] = {0.0};
    float PD_SED_Dilatation[totalNumMatPoint][2] = {0.0};
    float PD_SED_Dilatation_Fixed[totalNumMatPoint][2] = {0.0};
    int checkTime[timeInterval] = {0};
    float steadyCheck_x[timeInterval] = {0.0};
    float steadyCheck_y[timeInterval] = {0.0};
    float relPositionVector = 0.0;

    for (int j = 0; j < 2; j++ ) {
        for (int i = 0; i < totalNumMatPoint; i++) {
            coord[i][j] = coord_excess[i][j];
            std::cout << coord[i][j] << std::endl;
        }

}
  • 1
    You should note, that [variable length arrays aren't supported in the c++ standard](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). I urgently recommend you to use `std::vector` instead. – πάντα ῥεῖ Jan 07 '22 at 17:04
  • Hi, thanks for your comments. I know that it does have any output code. I have removed them in asking this question. I meant that if you were to put any output code below the comment, it would not print or work. But you are right. Maybe I should edit this question. – Ali Khoshrou Jan 07 '22 at 18:00
  • Use a debugger to step through the program line-by-line and watch what happens closely. When you see the program do something you don't expect (take the wrong path, store the wrong value, crash, ...) stop and figure out what happened. At the very least you now have a problem you can focus the question on. – user4581301 Jan 07 '22 at 18:16
  • Yes you are right, I have edited the question. But I cannot use a shorter sample of the code by removing some of the declaration, because that would solve the problem(!), as in, it will THEN generate the output. Which is odd because a few more array declarations should not avoid an output generation. But as you said I added a sample output generator. – Ali Khoshrou Jan 07 '22 at 18:18

2 Answers2

0

Your code, as is, is not "outputting" anything. I compiled and ran your code and added std::cout statements below and above your comment "AFTER THIS POINT DOWNWARDS, NO OUTPUTS WILL BE GENERATED". This successfully writes to stdout.

If, for example, you wanted to output all the values in the coords array you could do something like this while you are building it:

for (int j = 0; j < 2; j++ ) {
    for (int i = 0; i < totalNumMatPoint; i++) {
        coord[i][j] = coord_excess[i][j];
        std::cout << coord[i][j] << std::endl;
    }
}
  • Hey, thanks for you answer. I tried this however at the end of the code, but it does not print any output in the terminal – Ali Khoshrou Jan 07 '22 at 18:03
  • Is it possible you are redirecting stdout on your terminal? I moved the exact lines to the end of your code, compiled, and generated output. Try redirecting your output to a file and checking the contents of that file to see if they are blank. – TheFallenPickle Jan 07 '22 at 18:20
  • I have tried writing the output to a file and checking the content. Same thing. Below the comment line that I have found by debugging, no output file is opened. Above the comment line, the output file is successfully created and the output is written inside. This is the code I used for the file output: ofstream f("output.txt"); for (int i = 0; i < nnum; i++) { f << "An exmaple output" << endl; } f.close(); – Ali Khoshrou Jan 07 '22 at 18:28
  • You'll need to do what the other user suggested and debug your program as we can not recreate the issue you are experiencing. – TheFallenPickle Jan 07 '22 at 18:34
0

I used another PC with a different OS (i.e. Ubuntu) and it is running fine. Not sure what the problem was. Probably something run with my compiler and/or editor on the first computer.

  • One of the unfortunate effects of undefined behaviour is it might appear to work on one system and not another. You're better off isolating the exact cause of the problem on the computer on which it fails, and fix the problem if possible, so that you are less likely to encounter nasty surprises later. If your compiler supports it, [turn on ubsan](https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html) to help you find the problem . – user4581301 Jan 07 '22 at 19:10
  • Side note: Don't answer a question with a fuzzy answer. If you can't explain what went wrong, why, and hopefully how to fix it, don't answer. – user4581301 Jan 07 '22 at 19:11
  • Print out the value of `totalNumMatPoint` before you start using it and make sure the number is fairly small (< 4000). You have a whole bunch of [non-Standard Variable Length Arrays](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) based on it and are probably overflowing the stack and crashing the program. Windows operating systems, as suggested by the PS prefix on the output, have a 1MB default stack. Linux usually offers 4-8 MB, explaining why the code appears to work under Ubuntu. – user4581301 Jan 07 '22 at 19:30
  • Don't use Variable Length Arrays, even when supported by compiler extension, unless you have a really good reason, as they are an unending supply of nasty surprises. Use `std::vector` instead. – user4581301 Jan 07 '22 at 19:30
  • @All Khoshrou. IMO answer does not solve the nature the question. I find the code in the question problematic, especially the array initialization pattern, which leads to C++ language specific undetermined behavior. – huckfinn Jan 07 '22 at 20:00