malloc problems, but no errors.

Hi
I have written function readFile
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
bool readFile(char* infile_name, char* file_content)
{
    FILE* file_ptr = fopen(infile_name, "rb");
    if (file_ptr == NULL)
    {
        printf("Could not open file %s\n", infile_name);
        return false;
    }

    fseek(file_ptr, 0, SEEK_END);
    long file_size = ftell(file_ptr);
    fseek(file_ptr, 0, SEEK_SET);

    file_content = (char*) malloc(file_size + 1);
    if (file_content == NULL)
    {
        printf("Could not allocate memory.\n");
        return false;
    }
    
    fread(file_content, 1, file_size, file_ptr);
    fclose(file_ptr);
    file_content[file_size] = '\0';

    return true;
}


But at the end of file variable file_content are 0x0. Here are debug info from GDB
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
Breakpoint 1, readFile (infile_name=0x7fffffffe27d "labas.txt", file_content=0x0)
    at secret_text.c:67
67	    FILE* file_ptr = fopen(infile_name, "rb");
(gdb) n
68	    if (file_ptr == NULL)
(gdb) 
74	    fseek(file_ptr, 0, SEEK_END);
(gdb) 
75	    long file_size = ftell(file_ptr);
(gdb) 
76	    fseek(file_ptr, 0, SEEK_SET);
(gdb) 
78	    file_content = (char*) malloc(file_size + 1);
(gdb) 
79	    if (file_content == NULL)
(gdb) 
85	    fread(file_content, 1, file_size, file_ptr);
(gdb) 
86	    fclose(file_ptr);
(gdb) 
87	    file_content[file_size] = '\0';
(gdb) 
89	    return true;
(gdb) info locals
file_ptr = 0x602010
file_size = 27
(gdb) p file_content
$1 = 0x0


in line 78 file_content should be memory address from malloc if malloc fails it should be caught by if(file_content == NULL). But after all this lines file_content are NULL (0x0) but it was not caught. Or do I do not notice something?
Last edited on
I have updated my function
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
bool readFile(char* infile_name, char* file_content)
{
    FILE* file_ptr = fopen(infile_name, "rb");
    if (file_ptr == NULL)
    {
        printf("Could not open file %s\n", infile_name);
        return false;
    }

    fseek(file_ptr, 0, SEEK_END);
    long file_size = ftell(file_ptr);
    fseek(file_ptr, 0, SEEK_SET);

    file_content = (char*) malloc(sizeof(char) * (file_size + 1));
    if (file_content == NULL)
    {
        printf("Could not allocate memory.\n");
        return false;
    }
    
    size_t result = fread(file_content, sizeof(char), file_size, file_ptr);
    if (result != file_size)
    {
        printf("File read error.\n");
        return false;
    }
    
    fclose(file_ptr);
    file_content[file_size] = '\0';

    return true;
}

And GDB result are
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
Breakpoint 1, readFile (infile_name=0x7fffffffe27d "labas.txt", file_content=0x0)
    at secret_text.c:67
67	    FILE* file_ptr = fopen(infile_name, "rb");
(gdb) n
68	    if (file_ptr == NULL)
(gdb) 
74	    fseek(file_ptr, 0, SEEK_END);
(gdb) 
75	    long file_size = ftell(file_ptr);
(gdb) 
76	    fseek(file_ptr, 0, SEEK_SET);
(gdb) 
78	    file_content = (char*) malloc(sizeof(char) * (file_size + 1));
(gdb) 
79	    if (file_content == NULL)
(gdb) p file_content
$1 = 0x0
(gdb) n
85	    size_t result = fread(file_content, sizeof(char), file_size, file_ptr);
(gdb) 
86	    if (result != file_size)
(gdb) 
92	    fclose(file_ptr);
(gdb) 
93	    file_content[file_size] = '\0';
(gdb) 
95	    return true;
(gdb) info locals
file_ptr = 0x602010
file_size = 27
result = 27
(gdb) p file_content
$2 = 0x0
Found the problem. Here are new function.
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
bool readFile(char* infile_name, char** file_content)
{
    FILE* file_ptr = fopen(infile_name, "rb");
    if (file_ptr == NULL)
    {
        printf("Could not open file %s\n", infile_name);
        return false;
    }

    fseek(file_ptr, 0, SEEK_END);
    long file_size = ftell(file_ptr);
    fseek(file_ptr, 0, SEEK_SET);

    *file_content = (char*) malloc(sizeof(char) * (file_size + 1));
    if (*file_content == NULL)
    {
        fputs("Could not allocate memory.\n", stderr);
        return false;
    }
    
    size_t result = fread(*file_content, sizeof(char), file_size, file_ptr);
    if (result != file_size)
    {
        fputs("File read error.\n", stderr);
        return false;
    }
    
    fclose(file_ptr);
    (*file_content)[file_size] = '\0';

    return true;
}
Topic archived. No new replies allowed.