i wrote a function to read line from file the function use realloc() but The program falls every time in another place when it use realloc()Or I use realloc()in other places in the program where it falls every run in another reading and the error that appears is :Exception thrown at 0x77152B4B (ntdll.dll) in 21_207986712.exe: 0xC0000005: Access violation reading location 0x00000014.
int f_getLine(FILE* source, char** str) {
int n = 0, isFinish = 0;
char* line = NULL, c = NULL;
while (isFinish = fscanf(source, "%c", &c) == 1 && c != '\n')
{
line = (char*)realloc(line, sizeof(char) * ++n);
line[n - 1] = c;
}
if (isFinish != 1 && c != '\n')
return-1;
line = (char*)realloc(line, sizeof(char) * ++n);
line[n - 1] = NULL;
*str = line;
return n;
}
the function to "implement" preProssesor exchang define open include and skip define
char* f_preProssesor(char* source, FILE* target)
{
char* word = NULL;
char* define = NULL;
char* include = NULL;
char* str = NULL;
int isFinish = 0;
FILE* my_source = fopen(source, "r");
while (isFinish = f_getLine(my_source, &str) > 0)
{
if (!str||!*str)
continue;
if (strncmp("//", str, 2) == 0) {
continue;
}
while (str&&*str)
{ //skip the note and the space
while (*str && f_note(my_source, &str) != 0 && *str != ' ')
{//write the space to targrt
if (*str == ' ')
{
fprintf(target, "%c", ' ');
printf(target, "%c", ' ');
}
//check the next char
str++;
}
if (*str == '#')
{
if (include = f_findInclude(my_source, str))
{//open the include and go to next line
f_preProssesor(include, target);
break;
}
else
{//add the include and go to next line
f_findDefine(my_source, str);
break;
}
}
else if (!str&&!*str)
continue;
else if (*str == '"')
{//find the string
f_writeToFile(f_findString(&str, my_source), target);
}
else {
//find the word
word = f_findWord(&str, my_source);
//return the exchange of define
if (define = f_exchangeDefine(word))
word = define;
//write to file
f_writeToFile(word, target);
}
}
fprintf(target,"\n");
printf("\n");
}
fclose(my_source);
}
this function to store the include for example:
in this function the realloc fails also
void f_findDefine(FILE* my_source, char* line) {
char* result = NULL;
int n = 0;
line = strstr(line, "define");
if (!line)
return NULL;
line = line + 6;
struct s_define* new_def_p = (struct s_define*)malloc(sizeof(struct s_define*));
new_def_p->key = NULL;
new_def_p->value = NULL;
new_def_p->next = NULL;
//space
while (*line == ' ' || f_note(my_source, &line) != 0)
{
line++;
}
//first
while (*line&&*line != ' ' || f_note(my_source, &line) != 0)
{
new_def_p->key = (char*)realloc(new_def_p->key, ++n * sizeof(char));
new_def_p->key[n - 1] = *line;
line++;
}
new_def_p->key = (char*)realloc(new_def_p->key, ++n * sizeof(char));
new_def_p->key[n - 1] = '\0';
//space
while (*line && *line == ' ' || f_note(my_source, &line) != 0)
{
if (*line == ' ')
printf("\t");
line++;
}
//second
n = 0;
while (*line)
{
while (*line&& *line != '\n' && *line != '\\')
{
new_def_p->value = (char*)realloc(new_def_p->value, ++n * sizeof(char));
new_def_p->value[n - 1] = *line;
line++;
}
if (!*line || *line == '\n')
{
new_def_p->value = (char*)realloc(new_def_p->value, ++n * sizeof(char));
new_def_p->value[n - 1] = '\0';
f_seachDef(new_def_p);
return;
}
f_getLine(my_source, &line);
}
}
Exception thrown at 0x77152B4B (ntdll.dll) in 21_207986712.exe: 0xC0000005: Access violation reading location 0x00000014.
User contributions licensed under CC BY-SA 3.0