Thread: Initialization failed for int type

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    13

    Initialization failed for int type

    Hi everyone
    I have a small code listed below. The initialization in structure_factor for double type (lower_first_bin,lx,ly,lamdax,lamday) is successful. However, the initialization and assingment failed for the int type. During the budging, I found num_bin and num_ky are -524288,while the num_kx and dump_freq are 0. I have no idea what is wrong? Could anyone help me?

    Code:
    #include <fstream>
    #include <iostream>
    #include <stdlib.h>
    #include <cstring>
    #include <math.h>
    #include <memory>
    
    using namespace  std;
    
    //use 3layer no force 11 m/s trajectory to debug
    #define Num_step 10000000
    #define Dump_freq  5000
    #define Num_part 3303
    #define Num_bin 25
    #define Num_coor 3
    #define Lower_first_bin 10.000
    #define Width_bin 1.0
    #define Lx 36.96
    #define Ly 34.676
    #define Lambdax 0.8
    #define Lambday 0.8
    
    class Allocate_memory
        {
    protected:
        int num_dump,
             num_part,
             num_coor;
    
    
    public:
        Allocate_memory (int i_dim_x, int i_dim_y, int i_dim_z):
                num_dump (i_dim_x), num_part (i_dim_y), num_coor (i_dim_z) {};
    
        double *** allocate_memory_3d_array (double *** array_3d)
            {
    
                array_3d=new double ** [num_dump];
                for (int i=0;i<num_dump;i++)
                    {
                        array_3d[i]=new double * [num_part];
                        for (int j=0;j<num_part;j++)
                            {
                                array_3d[i][j]=new double [num_coor];
                            }
                    }
                return array_3d;
            }
    
        double ** allocate_memory_2d_array (double **array_2d)
            {
                array_2d=new double * [num_dump];
                for (int i=0;i<num_dump;i++)
                    {
                        array_2d[i]=new double [num_part];
                    }
    
                return array_2d;
            }
    
        void free_memory_3d_array (double *** array_3d)
            {
                 for (int i=0;i<num_dump;i++)
                     {
                         for (int j=0;j<num_part;j++)
                                  {
                                      delete [] array_3d[i][j];
                                      array_3d[i][j]=NULL;
                                  }
                         delete [] array_3d[i];
                         array_3d[i]=NULL;
                     }
                 delete [] array_3d;
                 array_3d=NULL;
            }
    
        void free_memory_2d_array (double ** array_2d)
            {
                for (int i=0;i<num_dump;i++)
                    {
                        delete [] array_2d[i];
                        array_2d[i]=NULL;
                    }
    
                delete [] array_2d;
                array_2d=NULL;
    
            }
    
    //virtual ~Allocate_memory ();
    
        };
    
    class Structure_factor:public Allocate_memory
        {
    private:
        double  *** coor,
    //            coor[Num_step/Dump_freq][Num_part][Num_coor],
                coor_bin[Num_bin][Num_part][Num_coor],
                time_averaged_factor_bin_3D[Num_bin][int (Lx/Lambdax)][int (Ly/Lambday)],
                  ** mass,
                   * lower_bin,
                   * up_bin,
                   * kx,
                   * ky,
                   * num_atom_bin, //store number of atoms for each bin
                     lower_first_bin,
                     width_bin,
                     lx,
                     ly,
                     lambdax,
                     lambday,
                     no_time_averaged_factor_bin[Num_step/Dump_freq][int(Num_bin)][int(Lx/Lambdax-1)*int(Ly/Lambday-1)],
                     time_averaged_factor_bin_2D[Num_bin][int(Lx/Lambdax-1)*int(Ly/Lambday-1)];
    
    
        const string name,
                         output_3d_array,
                         output_2d_array;
    
        string tem,
                mol,
                type,
                x_c,
                y_c,
                z_c,
                i_x,
                i_y,
                i_z,
                s_mass;
    
        fstream lammpstrj;
    
        int dump_freq,
            num_bin,
            num_kx,
            num_ky;
    
    public:
        Structure_factor (const string i_name, const string i_output_3d_array, const string i_output_2d_array, 
                int i_dim_x, int i_dim_y, int i_dim_z, int i_dump_freq, int i_num_bin, double i_lower_first_bin, 
                double i_width_bin,double i_lx,double i_ly,double i_lambdax,double i_lambday):
            Allocate_memory (i_dim_x,i_dim_y,i_dim_z),
            dump_freq (i_dump_freq),
            name (i_name),
            output_3d_array (i_output_3d_array),
            output_2d_array (i_output_2d_array),
            num_bin (i_num_bin),
            lower_first_bin (i_lower_first_bin),
            width_bin (i_width_bin),
            lx (i_lx),
            ly (i_ly),
            lambdax (i_lambdax),
            lambday (i_lambday)
    
        {
            coor=allocate_memory_3d_array (coor);
            mass=allocate_memory_2d_array (mass);
            num_kx=int (lx/lambdax);
            kx=new double [(num_kx-1)];
            num_ky=floor (ly/lambday);
            ky=new double [(num_ky-1)];
            lower_bin=new double [(num_bin)];
            up_bin=new double [int(num_bin)];
            num_atom_bin=new double [(num_bin)];
        }
    
        void open_file ()
            {
                 lammpstrj.open (name.c_str(),ios::in);
                 if (!lammpstrj)
                     {
                         cout << "Can not open the flow_solid_Couetteall.lammpstrj" << endl;
                         exit(1);
                     }
                 else
                     {
                         cout << "open the flow_solid_Couetteall.lammpstrj successfully" << endl;
                         read_data ();
                     }
                 lammpstrj.close ();
            }
    
         void read_data ()
             {
                 for (int i=0;i<num_dump;i++)
                     {
                         for (int j=0;j<9;j++)
                             {
                                 getline (lammpstrj,tem);
                             }
                         for (int k=0;k<num_part;k++)
                             {
                                 getline (lammpstrj,mol,' ');
                                 getline (lammpstrj,type,' ');
                                 getline (lammpstrj,x_c,' ');
                                 coor[i][k][0]=strtod(x_c.c_str(),NULL);
                                 getline (lammpstrj,y_c,' ');
                                 coor[i][k][1]=strtod(y_c.c_str(),NULL);
                                 getline (lammpstrj,z_c,' ');
                                 coor[i][k][2]=strtod(z_c.c_str(),NULL);
                                 getline (lammpstrj,i_x,' ');
                                 getline (lammpstrj,i_y,' ');
                                 getline (lammpstrj,i_z,' ');
                                 getline (lammpstrj,s_mass);
                                 mass[i][k]=strtod(s_mass.c_str(),NULL);
                             }
                     }
             }
    
    
        ~Structure_factor ()
            {
                free_memory_3d_array (coor);
                free_memory_2d_array (mass);
                delete [] lower_bin;
                lower_bin=NULL;
                delete [] up_bin;
                up_bin=NULL;
                delete [] num_atom_bin;
                num_atom_bin=NULL;
                delete [] kx;
                kx=NULL;
                delete [] ky;
                ky=NULL;
            }
        };
    
    int main ()
        {
            auto_ptr<Structure_factor> structure_factor (
        new Structure_factor ("flow_solid_Couetteall.lammpstrj","coordinate","mass",
            Num_step/Dump_freq,Num_part,Num_coor,Dump_freq,Num_bin,
            Lower_first_bin,Width_bin,Lx,Ly,Lambdax,Lambday));
    
    
    
            structure_factor->open_file ();
        }
    [
    Last edited by Salem; 10-18-2017 at 11:12 PM. Reason: removed font and size abuse from code section

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It's a fine example of C shoehorned into C++.
    #defines and DIY memory management all over the place.

    If your variables are mysteriously changing, then watchpoints are the way to go.
    Debugging with GDB: Set Watchpoints
    ba (Break on Access) | Microsoft Docs
    How to: Set a Data Breakpoint (Native Only)

    TBH, the code is too bloated with poorly named variables to spend too much time staring at it.

    FWIW, you seem to have several variables which almost mean the same thing, with almost the same values, which lead you to an array overrun somewhere.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2016
    Posts
    13
    Hi
    After I set the breakpoints at "void open_file ()" function,I checked the value of the variable.The values for double type (lower_first_bin,lx,ly,lamdax,lamday) are correct, while the values fornum_bin, num_k, num_kx and dump_freq are 0. So, it seems the the member initialization member list is not working properly for the int type, or the value from the main routine is not passing to the constructor of structure_factor class. I am new to C++ and I am not able to identify the problem. So, could you please help me out?

    Fan Li

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It works for me.
    Code:
    $ g++ -g foo.cpp
    $ gdb -q ./a.out
    Reading symbols from ./a.out...done.
    (gdb) b Structure_factor::Structure_factor
    Breakpoint 1 at 0x4019e1: file foo.cpp, line 119.
    (gdb) run
    Starting program: /home/salem/Documents/a.out 
    
    Breakpoint 1, Structure_factor::Structure_factor (this=0x7fffc9dd0010, 
        i_name="", i_output_3d_array="", i_output_2d_array="", i_dim_x=2000, 
        i_dim_y=3303, i_dim_z=3, i_dump_freq=5000, i_num_bin=25, 
        i_lower_first_bin=10, i_width_bin=1, i_lx=36.960000000000001, 
        i_ly=34.676000000000002, i_lambdax=0.80000000000000004, 
        i_lambday=0.80000000000000004) at foo.cpp:119
    119	        width_bin(i_width_bin), lx(i_lx), ly(i_ly), lambdax(i_lambdax), lambday(i_lambday) {
    (gdb) s
    Allocate_memory::Allocate_memory (this=0x7fffc9dd0010, i_dim_x=2000, i_dim_y=3303, i_dim_z=3) at foo.cpp:30
    30	        num_coor(i_dim_z) {
    (gdb) 
    31	    };
    (gdb) print *this
    $1 = {num_dump = 2000, num_part = 3303, num_coor = 3}
    (gdb) b Structure_factor::open_file
    Breakpoint 2 at 0x401ecc: file foo.cpp, line 132.
    (gdb) c
    Continuing.
    
    Breakpoint 2, Structure_factor::open_file (this=0x7fffc9dd0010) at foo.cpp:132
    132	        lammpstrj.open(name.c_str(), ios::in);
    (gdb) print *this
    value of type `Structure_factor' requires 758756480 bytes, which is more than max-value-size
    (gdb) print this->num_coor
    $2 = 3
    (gdb) print this->num_part
    $3 = 3303
    (gdb) s
    133	        if (!lammpstrj) {
    (gdb) 
    137	            cout << "open the flow_solid_Couetteall.lammpstrj successfully" << endl;
    (gdb) 
    open the flow_solid_Couetteall.lammpstrj successfully
    138	            read_data();
    (gdb) 
    Structure_factor::read_data (this=0x7fffc9dd0010) at foo.cpp:144
    144	        for (int i = 0; i < num_dump; i++) {
    (gdb) print num_dump
    $4 = 2000
    $ g++ --version
    g++-5.real (Ubuntu 5.4.0-6ubuntu1~16.04.5) 5.4.0 20160609
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2016
    Posts
    13
    Hi


    Please change the line 161 from “num_ky=floor (ly/lambday);” to “num_ky=int (ly/lambday);” I set two break points at Structure_factor and open_file function.

    Code:
    
    
    Code:
                             
    g++ -g debug.cpp  
     gdb -q ./a.out
     Reading symbols from ./a.out...done.
     (gdb) b Structure_factor::Structure_factor
     Breakpoint 1 at 0x401971: file debug.cpp, line 154.
     (gdb) run
     Starting program: /home/fanli/try/a.out  
     
     
     Breakpoint 1, Structure_factor::Structure_factor (this=0x7fffc9dd1010, i_name="", i_output_3d_array="", i_output_2d_array="", i_dim_x=2000, i_dim_y=3303, i_dim_z=3, i_dump_freq=5000, i_num_bin=25,  
         i_lower_first_bin=10, i_width_bin=1, i_lx=36.960000000000001, i_ly=34.676000000000002, i_lambdax=0.80000000000000004, i_lambday=0.80000000000000004) at debug.cpp:154
     154            lambday (i_lambday)
     (gdb) b Structure_factor::open_file
     Breakpoint 2 at 0x401e58: file debug.cpp, line 170.
     (gdb) c
     Continuing.
     
     
     Breakpoint 2, Structure_factor::open_file (this=0x7fffc9dd1010) at debug.cpp:170
     170                 lammpstrj.open (name.c_str(),ios::in);
     (gdb) print *this
     value of type `Structure_factor' requires 758756480 bytes, which is more than max-value-size
     (gdb) print this->num_bin
     $1 = 0
     (gdb) print this->num_kx
     $2 = 0
     (gdb) print this->num_ky
     $3 = 0
     (gdb) print this->dump_freq
     $4 = 0
     (gdb) print this->lambdax
     $5 = 0.80000000000000004
     (gdb) print this->lx
     $6 = 36.960000000000001
     (gdb) print->num_coor
     (gdb) print this->num_coor
     $7 = 3
     (gdb) print this->num_part
     $8 = 3303
     (gdb) print this->num_dump
     $9 = 2000
     (gdb) 
    



    Yes, the values of num_coor, num_part and num_dump are correct. But the values of num_bin, num_k, num_kx and dump_freq are still 0. The num_bin is supposed to be 25 same as the i_num_bin, and the dump_freq is supposed to be 5000 same as the i_dump_freq. What is wrong?


    During debugging, there is a statement “value of type `Structure_factor' requires 758756480 bytes, which is more than max-value-size” . Is the wrong number related to this?

  6. #6
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269
    Not related to your problem, but if the dimensions of each 2d array, and each row, etc are same within the 3d array then it would perhaps be easier to just allocate a single dimensional array:
    double *arr=new double[size1*size2*size3] and access elements as arr[index1*size1*size2+index2*size2+index3] (this could be made into a function) for example. So the freeing code would just become delete arr.
    "Highbrow philosophical truth: Everybody is an ape in monkeytown" --Oscar Wilde

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Not sure why this failed
    By garth in forum C++ Programming
    Replies: 19
    Last Post: 08-09-2012, 02:28 PM
  2. NtSetSystemTime() failed
    By medp7060 in forum C++ Programming
    Replies: 11
    Last Post: 04-02-2010, 02:58 AM
  3. GetPrivateProfileString failed
    By vart in forum Windows Programming
    Replies: 3
    Last Post: 02-24-2008, 01:19 PM
  4. Initialization from incompatible pointer type
    By Jailan in forum C Programming
    Replies: 9
    Last Post: 10-28-2007, 09:17 PM
  5. Error: initialization of non-const reference type
    By JerryL in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2004, 07:43 PM

Tags for this Thread