Problem: Dynamic memory allocation

Hi guys, I'm new in C/C++ programming and I got a problem in dynamic memory allocation.

I have a program that reads a file, than do some calculations. Than makes an output file with the results:

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
int main(){

    int size=0;
    float x[5];
    float A[5][5];
    float b[5];
    int l=0,c=0;
    char line[100]; 
    FILE *file;
    char Path1[50];
    char Path2[50];
    char Path3[50];
    char Path4[50];

    printf("Path1:");
    gets(Path1);
    printf("Path2:");
    gets(Path2);
    printf("Path3:");
    gets(Path3);
	
    file = fopen(Path1,"r");

    if(file == NULL) {
        printf("file unknow - file_name.txt\n");
        return 0;
    }

    while(fgets(line, sizeof(line), file) != NULL) {
        size++;
    }

    fclose(file);

    file = fopen(Path1,"r");

    if(file == NULL) {
        printf("file unknow - file_name.txt\n");
        return 0;
    }

    while(fgets(line, sizeof(line), file) != NULL) {

        char *split = strtok(line," ");

        if(split)
        {
            A[l][c] = (float)strtod(split,NULL);
            c++;
        }

        while(split != NULL)
        {
           split=strtok(NULL," ");

           if (c < size)
             A[l][c] = (float)strtod(split,NULL);
           c++;
        }

        c=0;
        l++;
    }

    fclose(file);

    c=0;
    l=0;

    file = fopen(Path2,"r");

    if(file == NULL) {
        printf("file unknow - file_name.txt\n");
        return 0;
    }

    while(fgets(line, sizeof(line), file) != NULL) {

        if (c < size)
             b[c] = (float)strtod(line,NULL);

        c++;
    }

    int k,i,j; float m;
    for (k=0;k<=size-2;k++){
         for (i=k+1;i<=size-1;i++){
             m=A[i][k]/A[k][k];
             for(j=0;j<size;j++){
                A[i][j]=A[i][j]-(m*A[k][j]);
             }
             b[i]=b[i]-(m*b[k]);
         }

         ofstream fout(strcat(Path3,"\\matriz.txt"));
         for(i=0; i<size; i++){
            for(j=0; j<size; j++){
               fout << A[i][j] << "\t";
            }
            fout << endl;
          }
    }

    retrosubstituicao(A,b,x,size);

    ofstream  fout(strcat(Path3,"\\matriz2.txt"));
    for(i=0; i<size; i++){
       fout << "x " << i << " "<< x[i] << "\t";
       fout << endl;
    }

    return 0;
}


And it's working. But now I want to parse this to a dynamic allocation, and than I did this:

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
int main(){

    int size=0;
    int l=0,c=0;
    char line[100]; 
    FILE *file;
    char Path1[50];
    char Path2[50];
    char Path3[50];
    char Path4[50];
    float *x = (float*) malloc(sizeof(float));
    float **A = (float**) malloc(sizeof(float));
    float *b = (float*) malloc(sizeof(float));

    printf("Path1:");
    gets(Path1);
    printf("Path2:");
    gets(Path2);
    printf("Path3:");
    gets(Path3);

    file = fopen(Path1,"r");

    if(file == NULL) {
        printf("file unknow - file_name.txt\n");
        return 0;
    }

    while(fgets(line, sizeof(line), file) != NULL) {
        size++;
    }

    fclose(file);

    file = fopen(Path1,"r");

    if(file == NULL) {
        printf("file unknow - file_name.txt\n");
        return 0;
    }

    while(fgets(line, sizeof(line), file) != NULL) {

        char *split = strtok(line," ");

        if(split)
        {
            A[l][c] = (float)strtod(split,NULL);
            c++;
        }

        while(split != NULL)
        {
           split=strtok(NULL," ");

           if (c < size)
             A[l][c] = (float)strtod(split,NULL);
           c++;
        }

        c=0;
        l++;
    }

    fclose(file);

    c=0;
    l=0;

    file = fopen(Path2,"r");

    if(file == NULL) {
        printf("file desconhecido - file_name.txt\n");
        return 0;
    }

    while(fgets(line, sizeof(line), file) != NULL) {

        if (c < size)
             b[c] = (float)strtod(line,NULL);

        c++;
    }

    int k,i,j; float m;
    for (k=0;k<=size-2;k++){
         for (i=k+1;i<=size-1;i++){
             m=A[i][k]/A[k][k];
             for(j=0;j<size;j++){
                A[i][j]=A[i][j]-(m*A[k][j]);
             }
             b[i]=b[i]-(m*b[k]);
         }

         ofstream fout(strcat(Path3,"\\matriz.txt"));
         for(i=0; i<size; i++){
            for(j=0; j<size; j++){
               fout << A[i][j] << "\t";
            }
            fout << endl;
          }
    }

    retrosubstituicao(A,b,x,size);

    ofstream  fout(strcat(Path3,"\\matriz.txt"));
    for(i=0; i<size; i++){
       fout << "x " << i << " "<< x[i] << "\t";
       fout << endl;
    }

    return 0;
}



Then it compiles normaly but when executing it says that the .exe has stopped working and does not give any error.
Anyone know what might be happening?
- Yes, you have most likely made an error in your usage of pointers.

- Additionally, i suggest that you use some debugging statements to locate the problem as i am not going to look through your code at the moment.
On line 13, you allocate memory for a single float and assign it to b:

float *b = (float*) malloc(sizeof(float));

However, on line 80, you seem to be treating it as an array:

b[c] = (float)strtod(line,NULL);

This would only work if you'd allocated memory for several floats. If c > 0, that line will write into memory which hasn't been allocated, causing undefined behaviour.

You've similarly only allocated enough memory for a single float for A and x, so you've probably got the same problem there.

Edit: Why are using old C-style malloc anyway? If you're writing in C++, use new.
Last edited on
In C++, use the new operator and not malloc.

1
2
// float *x = (float*) malloc(sizeof(float));
float *x = new float;


This statement is strange:

1
2
3
   
    float **A = (float**) malloc(sizeof(float));


It's trying to cast a float pointer (float*) to a pointer-to-a-float-pointer (float**). Since float is not a pointer, A[l][c] is not going to work right.

Do this instead:
1
2
3
4
5
6
7
8
// float A[5][5];
// in main
    float** A = new float*[5];
    for(int i = 0; i < 5; ++i)
    {
        A[i] = new float[5];
    }


Topic archived. No new replies allowed.