read text file in an Array

I am trying to read a text file "b.txt" in an Array
The file is sinply,it looks like:

b.txt
1
2
3
4
15 2 43 5
22 3 55 3
5 7 8 5
21 43 54 6

all lines have the same amount of numbers, separated with blank

The code gives an error in Valgrind line 27

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
#include<unistd.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;

int main()
{
int i, j, k, l, m;
unsigned char  **Arr = 0;
char x[20];
FILE *File;

i = 0;
File = fopen("b.txt", "r");
while (!feof(File))
{
	fgets(x, 20, File);
	if (feof(File))
		break;
	l = j = 0;
	Arr = (unsigned char **)realloc(Arr, (i + 1) * sizeof(unsigned char *));
	Arr[i] = (unsigned char *)calloc(1, sizeof(unsigned char));
	for (k = 0; k < strlen(x); k++)
		if (x[k] == ' ')
		{
			Arr[i] = (unsigned char *)realloc(Arr[i], (j + 1) * sizeof(unsigned char));
			Arr[i][j] = 0;
			for (m = l; m < k; m++)
				Arr[i][j] = Arr[i][j] * 10 + (x[m] - 48);
			l = k + 1;
			j++;
			Arr[i][j] = '\0';
		}
	i++;
}
fclose(File);

for (k = 0; k < j + 1; k++)
	free(Arr[k]);
free(Arr);

return 0;
}


Valgrind error in line 27
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
[root@localhost sase]# valgrind --leak-check=full --show-leak-kinds=all ./a.out
==10190== Memcheck, a memory error detector
==10190== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==10190== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==10190== Command: ./a.out
==10190== 
==10190== Invalid write of size 1
==10190==    at 0x400AD6: main (a.cpp:33)
==10190==  Address 0x6117f91 is 0 bytes after a block of size 1 alloc'd
==10190==    at 0x4C2AB9D: realloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==10190==    by 0x400A12: main (a.cpp:27)
==10190== 
==10190== 
==10190== HEAP SUMMARY:
==10190==     in use at exit: 104,608 bytes in 6,430 blocks
==10190==   total heap usage: 43,480 allocs, 37,050 frees, 165,900,941 bytes allocated
==10190== 
==10190== 1,334 bytes in 1,334 blocks are definitely lost in loss record 1 of 3
==10190==    at 0x4C2A9C7: calloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==10190==    by 0x40099B: main (a.cpp:23)
==10190== 
==10190== 30,570 bytes in 5,095 blocks are definitely lost in loss record 2 of 3
==10190==    at 0x4C2AB9D: realloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==10190==    by 0x400A12: main (a.cpp:27)
==10190== 
==10190== 72,704 bytes in 1 blocks are still reachable in loss record 3 of 3
==10190==    at 0x4C28C50: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==10190==    by 0x552F1EF: ??? (in /usr/lib64/libstdc++.so.6.0.21)
==10190==    by 0x400F669: call_init.part.0 (dl-init.c:72)
==10190==    by 0x400F77A: call_init (dl-init.c:30)
==10190==    by 0x400F77A: _dl_init (dl-init.c:120)
==10190==    by 0x4000CC9: ??? (in /usr/lib64/ld-2.21.so)
==10190== 
==10190== LEAK SUMMARY:
==10190==    definitely lost: 31,904 bytes in 6,429 blocks
==10190==    indirectly lost: 0 bytes in 0 blocks
==10190==      possibly lost: 0 bytes in 0 blocks
==10190==    still reachable: 72,704 bytes in 1 blocks
==10190==         suppressed: 0 bytes in 0 blocks
==10190== 
==10190== For counts of detected and suppressed errors, rerun with: -v
==10190== ERROR SUMMARY: 30608 errors from 3 contexts (suppressed: 0 from 0)


any idea with regards of malloc, realoc, where I am doing the mistake, it will be helpful
thank you !
Last edited on
ok, I'll change the solution, it makes 2 scans to the file instead of one but ... thya, simply is better

http://cboard.cprogramming.com/c-programming/153982-reading-text-file-into-2-dimensional-array-junk-values.html
Last edited on
Topic archived. No new replies allowed.