Program crashes after running.

Hi,
I made this program, and it runs well, it writes the grouped numbers, as it should, and sometimes continues, sometimes crashes. It may be caused by memory problems. What is the problem with it?
(sorry because of the code length, ps. comments in the code are not very important)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <new>

using namespace std; 

int sizeleft;   //if i put these declarations in function main() I get an error:
int sizeright;  //"this application has requested the runtime to terminate it in an unusual way"...???
int main()      //the program runs perfectly, it randomly divide the numbers into two groups (left and right)
{grp:           //but sometimes it ends with a crash, and i dont know whats the problem.
     
     int * left = new  int [sizeleft];
    int * right = new  int [sizeright];
    sizeleft=0;
    sizeright=0;
    int m,n,l,k;
    cout << "Number of elements: ";
    cin >> m;
    int center[m];
    srand (time(NULL));
    for (n=0;n<m;n++)
    {
    center[n]=n;
    l=rand()% 2;
    if (right == 0 || left == 0)                  //im not certain about the position of this error part...
         {cout << "ERROR:MEMORY ALLOCATION!";}
    else{
    if (l==0)
    {
         
         sizeleft++;
         left[sizeleft-1]=n;                        //or left[sizeleft-1]=center[n];
         }
    else if (l==1)
    {
         
         sizeright++;
         right[sizeright-1]=n;                   //or right[sizeright-1]=center[n];
         }
    }
}
cout << "lft: ";
for (k=0;k<sizeleft;k++)
    {cout <<left[k] << ", ";}
    cout << "\n";
    cout << "rght: ";
for (k=0;k<sizeright;k++)
    {cout << right[k]<<", ";}
    cout << "\n";
    delete[] right;
    delete[] left;
    goto grp;  //for more "fun"
}
Last edited on
I'm using dev c++.
on line 14/15: sizeleft/sizeright are uninitialized and hence some random number.

after line 20 it should look like this:
1
2
3
4
    cin >> m;
     int * left = new  int [m];
    int * right = new  int [m];
    int *center = new int[m]; // a non const array is not valid C++ 
Well for starters you're using the following variables before they've been initialised:

1
2
int sizeleft;
int sizeright;


By the way, it's not good design to rely on goto tag. I would recomend that you re-design the program so it flows as intended without the goto. Maybe use a do...while() loop and call a function containing your code.
closed account (z05DSL3A)
ajh32 wrote:
Well for starters you're using the following variables before they've been initialised:
They are initialised to a default of zero as they are global. When they are local they are uninitialised.

Edit: Hang on...I'm going to check that....
Last edited on
They are initialised to a default of zero as they are global.

That's right! Though the standard refers to namespace scope variables rather than global variables.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

int i;

namespace TheFinalFrontier {
    int j;
}

class Foo {
public:
    static int m_bar;
};

int Foo::m_bar; // same deal for static class members

int main () {
    using namespace TheFinalFrontier;

    static int k; // same deal for static locals

    cout << i << endl;
    cout << j << endl;
    cout << k << endl;
    cout << Foo::m_bar << endl;;

    return 0;
}


0
0
0
0


Andy

PS I prefer to be explicit, so usually write the = 0 even though it's not actually needed.
Last edited on
closed account (z05DSL3A)
Momentary second thought...I hate it when I do that because I have to go and check.
closed account (S6k9GNh0)
Grey Wolf, I do it all the time. I sometime wonder if everyone else does it.. :D
Grey Wolf wrote
They are initialised to a default of zero as they are global


Sorry, I stand corrected.

1
2
3
4
cin >> m;
     int * left = new  int [m];
    int * right = new  int [m];
    int *center = new int[m]; // a non const array is not valid C++  

but i dont know how many elements will the left/right contain, cuz they will randomly divide int 2 "groups". im pretty new to arrays and pointers altough:/
i want to get 3 arrays, like "center" "left" and "right". Center contain all "m" elements. left randomly for example "sizeleft" elements. and same for the right, but keep all "m" in center too.
Last edited on
but i dont know how many elements will the left/right contain, cuz they will randomly divide int 2 "groups". im pretty new to arrays and pointers altough:/


No, you don't know how many elements left and right will contain, however you know that number must be between 0 and m, so m seems like a much better choice than the 0 you're using now.

This becomes simpler if you don't manage the memory yourself and use a container: http://ideone.com/N9NcmX
Thaks for all replies! I learned new things :P.
Also i solved my problem, i replaced the "new int[]" thing with malloc things:D, so i can expand the size one by one. it looks like this now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std; 

int group()      
{
    int sizeleft;
    int sizeright;
    sizeright=0;
    sizeleft=0;
    int * left = reinterpret_cast<int *>(malloc(sizeleft * sizeof(*left)));
    int * right = reinterpret_cast<int *>(malloc(sizeright * sizeof(*left)));
    int m,n,l,k;
    cout << "Number of elements: "; cin >> m;
    int *center = reinterpret_cast<int*>(malloc(m * sizeof(*center)));
    if (NULL == center) {cout << "Memory Error\n";} //i type in a very large number and starts an infinite loop. any suggestions?
    srand (time(NULL));
    for (n=0;n<m;n++)
    {
    center[n]=n;
    l=rand()% 2;
    if (l==0)
    {sizeleft++; left = (int*)realloc(left, sizeleft * sizeof(int)); left[sizeleft-1]=n;}
    else if (l==1)
    {sizeright++; right = (int*)realloc(right, sizeright * sizeof(int)); right[sizeright-1]=n;}
    }
cout << "\nleft: ";
for (k=0;k<sizeleft;k++)
    {cout <<left[k] << ",";}
    cout << "\n\n";
    cout << "right: ";
for (k=0;k<sizeright;k++)
    {cout << right[k]<<",";}
    cout << "\n\n";
    cout <<"Memory address of 'center': "<< &center << ".\n\n";
    cout <<"Memory address of 'left': "<< &left << ".\n\n";
    cout <<"Memory address of 'right': "<< &right << ".\n\n";
    free(center);
    free(right);
    free(left);
    center=NULL;
    right=NULL;
    left=NULL;
    return 0;
}

int main()
{
    while (1)
    {group();}
    return 0;
}
Last edited on
one more question: i still dont know how to place memory error messages.
really don't worry about memory. A standard computer has gigabytes.
A few (thousend) bytes more or less is negligible. More important is a proper initialization and clean up

the realloc() in your loop does more harm than good. It significantly slows down the processiing and the benefit tends to zero.

one more question: i still dont know how to place memory error messages.
if you want to do something with it you migth use set_new_handler:

http://www.cplusplus.com/reference/new/set_new_handler/


But honestly: if you run out of memory you're screwed anyway. There's not much you can do (printing a message might also consume memory). It can hardly be anything but a bug
i realized that the problem was not with the memory(i have more than enough :D) but integer overflowing when i picked a large number.@coder777 Im relly new to c++, and still cant use arrays for dynamic memory, i found this malloc simplier, still the speed is not important for me(but i will experience it:D). now everything works fine.the program is not very clean but its *solved*: heres a code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits> //i used INT_MAX but doesnt worked
#include <climits>
#include <windows.h>
using namespace std; 

int help()
{cout<<"What is this? ('y' for help)";char help;cin>>help;
if (help=='y'||help=='Y'){cout << "blablabla"; }} //purpose of this prog:P

int elements()
{
    cout<< "\nShow element id-s?\ny(es) or (n)o: ";
    char ans;
    cin >> ans;
    if (ans=='y'||ans=='Y') {cout << "Show id numbers.\n\n";return 1;}
    else if (ans=='n'||ans=='N'){cout << "No id-s will be shown.\n\n";return 0;}
    else {return 0;}
}

int group() 
{ 
    int eid=elements();
    int sizeleft;
    int sizeright;
    sizeright=0;
    sizeleft=0;
    int * left = reinterpret_cast<int *>(malloc(sizeleft * sizeof(*left)));
    int * right = reinterpret_cast<int *>(malloc(sizeright * sizeof(*left)));
    int *m,n,l,k,h;
    m=new int();
    cout << "Number of elements: "; cin >> *m; cin.ignore(9000,'\n');
    cout  << "Return: " << *m << endl;
    if (*m!=0&&*m<9000){
    int *center = reinterpret_cast<int*>(malloc(*m * sizeof(*center)));
    if (center == NULL ) {cout << "MEMORY ERROR.\n\n";return 0;}
    else{
    srand (time(NULL));
    for (n=0;n<*m;n++)
    {
    center[n]=n;
    l=rand()% 2;
    if (l==0)
    {sizeleft++; left = (int*)realloc(left, sizeleft * sizeof(int)); left[sizeleft-1]=n;}
    else if (l==1)
    {sizeright++; right = (int*)realloc(right, sizeright * sizeof(int)); right[sizeright-1]=n;}
    }
 cout << "Groups(left-right): " << sizeleft << "-" << sizeright << "\n";
if(eid==1){
cout << "Center 'core' element id-s: ";
for (k=0;k<n;k++)
    {cout << center[k]<<",";}
    cout << "\b\n";
   
cout << "Left id-s: ";
for (k=0;k<sizeleft;k++)
    {cout <<left[k] << ",";}
    cout << "\b\n";
    
cout << "Right id-s: ";
for (k=0;k<sizeright;k++)
    {cout << right[k]<<",";}
    cout << "\b\n";}
int eid=elements();
int counter;
cout << "\nNumber of bounces: "; cin >> counter;
int done;
for(done=0;done<counter;done++)
{
    int templeftsize;
    int temprightsize;
    templeftsize=0;
    temprightsize=0;
    int * templeft=reinterpret_cast<int *>(malloc (templeftsize * sizeof(*templeft)));
    int * tempright=reinterpret_cast<int *>(malloc (temprightsize * sizeof(*tempright)));
    int i,il,ir;
        
        for (il=0;il<sizeleft;il++)
        {
        int ll=rand()%2;
        if(ll==0)
          {temprightsize++; tempright = (int*)realloc(tempright, temprightsize * sizeof(int)); tempright[temprightsize-1]=left[il];}
        else if(ll==1)
          {templeftsize++; templeft = (int*)realloc(templeft, templeftsize * sizeof(int)); templeft[templeftsize-1]=left[il];}
        }
        for (ir=0;ir<sizeright;ir++)
        {
        int lr=rand()%2;
        if(lr==0)
          {templeftsize++; templeft = (int*)realloc(templeft, templeftsize * sizeof(int));templeft[templeftsize-1]=right[ir];}
        else if(lr==1)
          {temprightsize++; tempright = (int*)realloc(tempright, temprightsize * sizeof(int));tempright[temprightsize-1]=right[ir];}
        }
    
    cout << "\nNew groups:(left-right): " << templeftsize << "-" << temprightsize << endl;
    free(left);free(right);left=NULL;right=NULL;
    left = (int*)realloc(left, templeftsize * sizeof(int));
    right = (int*)realloc(right, temprightsize * sizeof(int));

if (eid==1){cout<<"New_Left: ";}
for(i=0;i<templeftsize;i++)
 {
    left[i]=templeft[i];
    if (eid==1){cout << left[i] << ",";}
 }
if (eid==1){cout <<"\nNew_Right: ";}
for(i=0;i<temprightsize;i++)
 {
    right[i]=tempright[i];
    if (eid==1){cout << right[i] << ",";}
 }
    sizeleft=templeftsize;
    sizeright=temprightsize;
    cout << endl;
    free(templeft);free(tempright);templeft=NULL;tempright=NULL;
}
cout <<"\nMemory address of 'center': "<< &center << ".\n"; //i checked something with these...
cout <<"Memory address of 'left': "<< &left << ".\n";
cout <<"Memory address of 'right': "<< &right << ".\n\n";

    free(center);
    free(right);
    free(left);
    center=NULL;
    right=NULL;
    left=NULL;
    return 0;}}
    else {cout << "ERROR:OVERFLOW/IT'S OVER 9000!?\n";delete m; return (1);} //check if ITS OVER 9000!!!
}

int main()
{
    help();
    cout << "-Entropy-\n";
    cout << "Groups: center'core', left, right.\n\n";
    while (group()!=1)
    {group();}
    cout << "END\n";
    Sleep(3000);
    return 0;
}


(this prog is a pc version of a physical experiment or at least try to be:Pi will look for better random generator but its ok till)
and thank you all!
Last edited on
Topic archived. No new replies allowed.