I been banging my head on the wall with this one. I was able to narrow it down to the realloc portion of my code.
CalStatus readCalComp( FILE *const ics, CalComp **const pcomp )
{
CalStatus test;
CalProp * foldLine;
CalProp * temp;
CalComp ** subComp;
char * buffer;
static int depth = 0;
int count = 0;
int i = 0;
buffer = NULL;
foldLine = NULL;
subComp = NULL;
if (depth == 0)
{
test = readCalLine (ics, &buffer);
if (buffer == NULL)
{
return test;
}
foldLine = malloc (sizeof (CalProp));
assert (foldLine != NULL);
test.code = parseCalProp (buffer, foldLine);
free (buffer);
if ((strcmp ("BEGIN", foldLine->name) == 0) && ((strcmp ("VCALENDAR", foldLine->value) == 0)))
{
(*pcomp)->name = malloc (sizeof(char) * strlen(foldLine->value)+1);
assert ((*pcomp)->name != NULL);
strcpy((*pcomp)->name, foldLine->value);
depth = depth + 1;
free(foldLine->name);
free(foldLine->value);
free(foldLine);
while ((buffer != NULL) && (test.code == OK) && (depth > 0))
{
test = readCalLine (ics, &buffer);
if (buffer != NULL)
{
foldLine = malloc (sizeof(CalProp));
assert (foldLine != NULL);
test.code = parseCalProp (buffer, foldLine);
free (buffer);
if ((strcmp ("END", foldLine->name) == 0) && ((strcmp ("VCALENDAR", foldLine->value) == 0) || (strcmp ("VCALENDAR\r\n", foldLine->value) == 0) ))
{
depth = depth - 1;
free(foldLine->name);
free(foldLine->value);
free(foldLine);
return test;
}
else if (strcmp ("BEGIN", foldLine->name) == 0)
{
subComp = malloc(sizeof (CalComp *));
*subComp = malloc(sizeof(CalComp) + (1*sizeof(CalComp*)));
(*subComp)-> name = NULL;
(*subComp)-> nprops = 0;
(*subComp)-> prop = NULL;
(*subComp)-> ncomps = 0;
(*subComp)-> comp[0] = NULL;
(*subComp)-> name = malloc(sizeof(char) *strlen(foldLine->value)+1);
strcpy((*subComp)->name, foldLine->value);
if ((*pcomp)-> ncomps == 0)
{
(*pcomp)->comp[(*pcomp)->ncomps] = *subComp;
(*pcomp)->ncomps += 1;
}
else
{
(*pcomp)->ncomps += 1;
*pcomp = realloc(*pcomp, sizeof(CalComp) + (2*sizeof(CalComp*)));
(*pcomp)->comp[(*pcomp)->ncomps-1] = *subComp;
}
depth = depth + 1;
test = readCalComp (ics, subComp);
}
} // end of if buffer NULL
}// end of While
} // end of if VCALENDAR
} // End of if Depth
else
{
if (count == 0)
{
test = readCalLine (ics, &buffer);
count += 1;
}
while ((test.code == OK) && (buffer != NULL))
{
if (count != 1)
{
test = readCalLine (ics, &buffer);
}
else
{
count = 0;
}
if (buffer != NULL)
{
foldLine = malloc (sizeof(CalProp));
test.code = parseCalProp (buffer, foldLine);
free (buffer);
if ((strcmp ("END", foldLine->name) == 0) && ((strcmp ((*pcomp)->name, foldLine->value) == 0)))
{
depth = depth - 1;
free (foldLine->name);
free (foldLine->value);
free (foldLine);
return test;
}
else if (strcmp ("BEGIN", foldLine->name) == 0)
{
printf("Success in malloc 2!\n");
subComp = malloc(sizeof (CalComp *));
*subComp = malloc(sizeof(CalComp) + (1*sizeof(CalComp*)));
(*subComp)-> name = NULL;
(*subComp)-> nprops = 0;
(*subComp)-> prop = NULL;
(*subComp)-> ncomps = 0;
(*subComp)-> comp[0] = NULL;
(*subComp)-> name = malloc(sizeof(char) *strlen(foldLine->value)+1);
strcpy ((*subComp)->name, foldLine->value);
if ((*pcomp)-> ncomps == 0)
{
(*pcomp)->comp[(*pcomp)->ncomps] = *subComp;
(*pcomp)->ncomps += 1;
}
else
{
(*pcomp)->ncomps += 1;
(*pcomp) = realloc(*pcomp, sizeof(CalComp) + (2*sizeof(CalComp*)));
(*pcomp)->comp[(*pcomp)->ncomps-1] = *subComp;
printf ("First subcomponent is %s\n", (*pcomp)->comp[0]->name);
printf ("Second subcomponent is %s\n", (*pcomp)->comp[1]->name);
}
depth = depth + 1;
test = readCalComp (ics, subComp);
}
}
}
}
return test;
}
CalComp is a struct that consists of:
char * name;
int numProps;
CalProp *prop (linked list);
int numComp;
CalComp *comp[] (flexible array);
Checking inside the function, the name of the structs within the flexible array is correct, but when I try to access the names outside of the function, it either NULL value or been set to the name of the first linked list node of the structure.
Due to the nature of the assignment, I can not modify Calcomp struct nor the function parameters. The function must be recursive, and the flexible array must adjust based on need.
As I said before, I narrowed it down to the realloc, by printing out the values within the flexible array. The bug occurs when the program reallocs.
FYI, I have tried reallocing to a temp variable, checked to see if NULL and then assigned that one to pcomp but didn't help.
When you call realloc, you're not including ncomp in the additional size for the flexible structure, you're just hard-coding it to 2.
(*pcomp) = realloc(*pcomp, sizeof(CalComp) + (*pcomp->ncomps * sizeof(CalComp*)));
(*pcomp) = realloc(*pcomp, sizeof(CalComp) + (2*sizeof(CalComp*)));
change above line to
pcomp = realloc(*pcomp, sizeof(CalComp) + (2*sizeof(CalComp*)));
Related
The code I write is in C on Visual studio 2022.. I don't understand what part of my code is wrong, but it should be about string manipulation. The SplitStr splits string by another string (strtok does it by character).
#define SplitCount 20
TCHAR** SplitStr(TCHAR* pstr, TCHAR *psplitby)
{
if (pstr == NULL || psplitby == NULL)
return NULL;
TCHAR** res = malloc(SplitCount * sizeof(TCHAR*));
int i = 0, j = 0, c = 0;
TCHAR* splitby = _tcsdup(psplitby); // I need to duplicate here
int len = _tcslen(splitby);
TCHAR* str = _tcsdup(pstr); // and here
int strlen1 = _tcslen(str);
while (i < MAXSTR - strlen1) {
if (i + len < strlen1 && _tcsncmp(splitby, &str[i], len) == 0)
{
res[c] = malloc(MAXSTR * sizeof(TCHAR*));
if (res[c] == NULL)
break;
_tcsncpy_s(res[c], MAXSTR, &str[j], i-j);
i += len;
j = i;
c++;
i--;
}
i++;
}
res[c] = malloc(MAXSTR * sizeof(TCHAR*));
if (res[c] != NULL)
{
_tcsncpy_s(res[c], MAXSTR, &str[j], strlen1 - j);
c++;
}
res[c] = _tcsdup(_T("END"));
return res;
}
TCHAR* StrToLower(TCHAR* Str)
{
int i = 0;
TCHAR * tstr = malloc(MAXSTR * sizeof(TCHAR));
ZeroMemory(tstr, MAXSTR * sizeof(TCHAR));
if (tstr != NULL)
{
_tcsset_s(tstr, MAXSTR, _T('\0'));
for (; i < MAXSTR; i++)
{
tstr[i] = _totlower(Str[i]);
}
}
return tstr;
}
int SelectFromTable(TCHAR* Table, FIELDS fields[], TCHAR* Where);
int execute_sentence(TCHAR* psentence)
{
TCHAR * sentence = StrToLower(psentence);
StrTrim(sentence, _T(" "));
TCHAR* dlimiter = _T("?");
if (_tcsncmp(_T("select"), sentence, _tcslen(_T("select"))) == 0)
{
TCHAR ** split = SplitStr(sentence, _T("where"));
TCHAR* select = split[0];
StrTrim(select, _T(" "));
TCHAR* where = split[1];
StrTrim(where, _T(" "));
//_putts(where); // here value is OK
TCHAR ** split2 = SplitStr(select, _T("from"));
TCHAR * fields = split2[0] + _tcslen(_T("select"));
TCHAR * table = split2[1];
StrTrim(table, _T(" "));
_putts(where); // here value is distorted now if I don't use _tcsdup above
TCHAR ** split3 = SplitStr(fields, dlimiter);
TCHAR* thefield = split3[0];
StrTrim(thefield, _T(" "));
FIELDS fields_arr[Field_Count];
int c_field_pos = 0, i = 1;
while (_tcscmp(thefield, _T("END")) != 0)
{
StrTrim(thefield, _T(" "));
c_field_pos = 0;
if (thefield != NULL)
{
for (int i = 0; i < Field_Count; i++)
{
if (_tcsncmp(thefield, table_columns[i], _tcslen(table_columns[i])) == 0) {
fields_arr[c_field_pos] = (FIELDS)i;
c_field_pos++;
break;
}
}
}
thefield = split3[i];
i++;
}
fields_arr[c_field_pos] = F_END;
SelectFromTable(table, fields_arr, where);
}
return 0;
}
I never expected the need to duplicate, I even used a pointer and kept it intact, still problem exists..
I am new to C and am trying to code up a data structure, primarily, a ternary search tree. I am working under the assumption (for now) that valid char inputs are being passed in. I am having some issues with my insert function. Note that I am also inserting the original string in the last TSTnode where the last character of str will also be held.
Here is what I have so far
struct TSTnode {
char* word; // NULL if no word ends here
char self;
struct TSTnode *left, *sub, *right;
};
int insert_tst(struct TSTnode** tree, const char* str) {
return _insert(tree, str, 0);
}
int _insert(struct TSTnode** tree, const char* str, int position) {
if((*tree) == NULL) {
*tree = new_tst_node(*(str+position));
position = position + 1;
if(*(str+position) == '\0') {
(*tree)->word = strcpy((*tree)->word,str);
return 1;
}
}
else if ((*tree)->self > *(str+position)) {
position = position + 1;
_insert( &((*tree)->left), str, position);
}
else if ((*tree)->self < *(str+position)) {
position = position + 1;
_insert( &((*tree)->right), str, position);
}
else {
position = position + 1;
_insert( &((*tree)->sub), str, position);
}
return 0;
}
struct TSTnode* new_tst_node(char self) {
struct TSTnode* newNode = (struct TSTnode*) malloc(sizeof(struct
TSTnode));
if (newNode == NULL) {
return NULL;
}
newNode->word = NULL;
newNode->self = self;
newNode->left = NULL;
newNode->right = NULL;
newNode->sub = NULL;
return newNode;
}
Here is how I am testing:
struct TSTnode* tree = NULL;
char* words[1] = {"hello"};
for (int i = 0; i < 1; i++) {
if (insert_tst(&tree, words[i]) == 0) {
//print some error
}
else { //success }
EDIT - My issue is that none of my conditional branches are being taken and the insert function simply goes straight to return 0.
Note: You confusingly use tree for both TSTnode* and TSTnode**. I'm going to use tree_ptr for the latter, and pretend that you did the same.
Your claim is false. The body of if((*tree_ptr) == NULL) is executed. You do have a number of problems, though.
You don't handle the case where *tree_ptr == NULL && *(str+position+1) != '\0'.
You don't correctly handle the case where *tree_ptr != NULL && *(str+position+1) == '\0'.
You always return 0 when *tree_ptr != NULL || str[1] != '\0'.
You never allocate word, but you deference it. The thing is, you shouldn't be storing the string again anyway!
You don't handle the case where str[0] == '\0' (empty string).
Fixed:
int insert_tst(struct TSTnode** tree_ptr, const char* str) {
if (!*str)
return 0; /* Zero-length strings are not supported. */
return insert_tst_helper(tree_ptr, str, 0);
}
int insert_tst_helper(struct TSTnode** tree_ptr, const char* str, int position) {
if (*tree_ptr == NULL) {
*tree_ptr = new_tst_node(*(str+position));
if (*tree_ptr == NULL)
return 0; /* Memory allocation error. */
}
if (*(str+position+1) == '\0') { /* If the next char is a NUL */
(*tree_ptr)->is_word = 1;
return 1;
}
else if ((*tree_ptr)->self > *(str+position)) {
position = position + 1;
return insert_tst_helper( &((*tree_ptr)->left), str, position);
}
else if ((*tree_ptr)->self < *(str+position)) {
position = position + 1;
return insert_tst_helper( &((*tree_ptr)->right), str, position);
}
else {
position = position + 1;
return insert_tst_helper( &((*tree_ptr)->sub), str, position);
}
}
Untested.
Let's clean this up, though.
*(str+position)simplifies tostr[position]
ch == '\0'simplifies toch == 0then to!ch
position = position + 1; return insert_tst_helper(..., str, position);simplifies to++position; return insert_tst_helper(..., str, position);then toreturn insert_tst_helper(..., str, position+1);then toreturn insert_tst_helper(..., str+1, 0);then toreturn insert_tst(..., str+1);
Why is recursion being used at all???
Fixed:
int insert_tst(struct TSTnode** tree_ptr, const char* str) {
if (!*str)
return 0; /* Zero-length strings are not supported. */
while (1) {
if (*tree_ptr == NULL) {
*tree_ptr = new_tst_node(*str);
if (*tree_ptr == NULL)
return 0; /* Memory allocation error. */
}
if (!*(str+1)) { /* If the next char is a NUL */
(*tree_ptr)->is_word = 1;
return 1;
}
int cmp = *str - (*tree_ptr)->self;
if (cmp < 0) { tree_ptr = &( (*tree_ptr)->left ); }
else if (cmp > 0) { tree_ptr = &( (*tree_ptr)->right ); }
else { tree_ptr = &( (*tree_ptr)->sub ); }
++str;
}
}
Untested.
In the function RenameSubs() when I try debugging I get segmentation fault at this line of code
subsdir[i] = (char*)malloc((GetStringSize(moviesdir[i]) + 1 + 4) * sizeof(char));
I don't understand why — can you explain?
int main()
{
int number;
char *mainDirectory = NULL,**names = NULL;
printf("Give the movies directory: ");
mainDirectory = ReadMainDirectory();
if(GetFiles(&mainDirectory,&names,&number) != 0)
{
RenameSubs(number,mainDirectory,names);
system("PAUSE");
return 1;
}
system("PAUSE");
return 0;
}
char* ReadMainDirectory()
{
char *dir,c;
int size = 1;
dir = (char*)malloc(size+1);
dir[size-1] = '\0';
while((c = getchar()) != '\n')
{
dir[size-1] = c;
size++;
dir = (char*)realloc(dir,size+1);
dir[size-1] = '\0';
}
return dir;
}
int GetFiles(char **dir,char ***names,int *number)
{
struct dirent *dp;
DIR *fd;
if ((fd = opendir(*dir)) == NULL)
{
printf("Can't open directory %s!\n",*dir);
return 0;
}
*number = 0;
*names = (char**)malloc(((*number)+1) * sizeof(char*));
while ((dp = readdir(fd)) != NULL)
{
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
{
continue;
}
(*names)[*number] = strdup(dp->d_name);
(*number)++;
*names = (char**)realloc(*names,((*number)+1) * sizeof(char*));
}
closedir(fd);
return 1;
}
int GetStringSize(char *string)
{
int size = 0;
while(string[size] != '\0')
{
size++;
}
return size;
}
int StringEndsWith(char* string,char* extension)
{
int size;
char *strextension = NULL;
size = GetStringSize(string);
strextension = (char*)malloc(5 * sizeof(char));
strextension[0] = string[size-4];
strextension[1] = string[size-3];
strextension[2] = string[size-2];
strextension[3] = string[size-1];
strextension[4] = '\0';
if(strcmp(strextension,extension) == 0)
{
return 1;
}
return 0;
}
void RenameSubs(int number,char* mainDir,char** filenames)
{
int i,mainDirSize,movieDirSize[number],moviesdirnumber,subsdirnumber,j,y;
char **moviesdir = NULL,**subsdir = NULL,**subsdest = NULL,**moviesdirfilenames = NULL,**subsdirfilenames = NULL,*moviesname = NULL;
moviesdir = (char**)malloc(number * sizeof(char*));
mainDirSize = GetStringSize(mainDir);
for(i=0;i<number;i++)
{
movieDirSize[i] = mainDirSize + 1 + GetStringSize(filenames[i]);
moviesdir[i] = (char*)malloc((movieDirSize[i]+1) * sizeof(char));
strcpy(moviesdir[i],mainDir);
strcat(moviesdir[i],"\\");
strcat(moviesdir[i],filenames[i]);
GetFiles(&moviesdir[i],&moviesdirfilenames,&moviesdirnumber);
subsdir[i] = (char*)malloc((GetStringSize(moviesdir[i]) + 1 + 4) * sizeof(char));
strcpy(subsdir[i],moviesdir[i]);
strcat(subsdir[i],"\\");
strcat(subsdir[i],"Subs");
GetFiles(&subsdir[i],&subsdirfilenames,&subsdirnumber);
subsdest[i] = (char*)malloc((GetStringSize(subsdir[i]) + GetStringSize(subsdirfilenames[0]) + 1) * sizeof(char));
strcpy(subsdest[i],subsdir[i]);
strcat(subsdest[i],"\\");
strcat(subsdest[i],subsdirfilenames[0]);
for(j=0;j<moviesdirnumber;j++)
{
if(StringEndsWith(moviesdirfilenames[j],".mkv") || StringEndsWith(moviesdirfilenames[j],".mp4") || StringEndsWith(moviesdirfilenames[j],".avi"))
{
moviesname = (char*)malloc((GetStringSize(moviesdirfilenames[j]) - 4 + 1) * sizeof(char));
for(y=0;y<(GetStringSize(moviesdirfilenames[j]) - 4);y++)
{
moviesname[y] = moviesdirfilenames[j][y];
}
moviesname[y] = '\0';
break;
}
}
}
}
You are using array of pointers without initializing it first . initialize it.
In loop
subsdir[i] = malloc((GetStringSize(moviesdir[i]) + 1 + 4) * sizeof(char));
This is the second step . First u need to initialize
subsdir = malloc (sizeof(char*) * number);
this is the function that i am calling from main:
char** readTokens(char *userInput, const char *seperator)
{
char** c;
char line1[512],line2[512];
int wordCount = 0;
int index;
char* tmp;
strcpy(line1, userInput);
for (index=0;line1[index]!='\n';index++);
line1[index]='\0';
strcpy(line2,line1);
tmp = strtok(line1,seperator);
while (tmp!=NULL)
{
tmp=strtok(NULL,seperator);
wordCount = wordCount + 1;
}
if((wordCount) == ERROR)
{
return NULL;
}
c=(char**)malloc(((wordCount)+1)*sizeof(char*));
if (c == NULL)
{
printf("failed to allocate memory.\n");
return NULL;
}
tmp = strtok(line2,seperator);
index=0;
while (tmp!=NULL)
{
c[index]=(char*)malloc((strlen(tmp)+1*sizeof(char)));
if (c[index]==NULL)
{
printf("failed to allocate memory.\n");
return NULL;
}
strcpy(c[index],tmp);
tmp=strtok(NULL,seperator);
index++;
}
c[index] = NULL;//put NULL on last place
return c;
}
And this how i use it in main:
while (fgets(words, sizeof(words), filePointer) != NULL) // this line is a command of reading a line from the file.
{
/*here i am calling the function*/
array = readTokens(words, " ");
theGraph->graphEdges[index_i].sourceVertex = array[ZERO];
theGraph->graphEdges[index_i].destinationVertex = array[ONE];
theGraph->graphEdges[index_i].arcValue = atoi(array[TWO]);
for(index_j = ZERO ; index_j < vertexes ; index_j++)
{
if(theGraph->placeInTableIndex[index_j] == NULL)
{
theGraph->placeInTableIndex[index_j] = array[ZERO];
break;
}
else if(strcmp(theGraph->placeInTableIndex[index_j],array[ZERO]) == ZERO)
break;
}
for(index_j = ZERO ; index_j < vertexes ; index_j++)
{
if(theGraph->placeInTableIndex[index_j] == NULL)
{
theGraph->placeInTableIndex[index_j] = array[ONE];
break;
}
else if(strcmp(theGraph->placeInTableIndex[index_j],array[ONE]) == ZERO)
break;
}
theGraph->graphEdges[index_i+ONE].sourceVertex = array[ONE];
theGraph->graphEdges[index_i+ONE].destinationVertex = array[ZERO];
theGraph->graphEdges[index_i+ONE].arcValue = atoi(array[TWO]);
index_i+= TWO;
//freeTokens(array);
}
I tried to do free to the array in the end of the while but it not work i still have memory leak from this function (valgrind check). i am using this function to free:
void freeTokens(char** tokens)
{
while(*tokens != NULL)
{
*tokens = NULL;
free(*tokens);
*tokens++;
}
tokens = NULL;
free(tokens);
}
You're losing the original value of tokens (the thing you need to free) by incrementing it; then you set it to NULL, then try to free NULL.
Instead:
void freeTokens(char** tokens)
{
char **freeTokens = tokens;
while (*freeTokens != NULL)
{
free(*freeTokens);
*freeTokens = NULL; // not actually necessary, but must happen *after* if at all
freeTokens++;
}
free(tokens);
// tokens = NULL; // accomplishes nothing; doesn't change the caller's version
}
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have a memory lost problem I can not solve:
==19660== 14,583 (1,764 direct, 12,819 indirect) bytes in 49 blocks are definitely lost in loss record 27 of 27
==19660== at 0x4023F50: malloc (vg_replace_malloc.c:236)
==19660== by 0x80489F5: AllocateFct (function.c:27)
==19660== by 0x804BB51: InsertFct (reader.c:417)
==19660== by 0x804BFFB: InsertShape (reader.c:591)
==19660== by 0x804AEED: main (main.c:103)
Generating by the following features :
function AllocateFct(char* model)
{
function fct = (function) malloc(sizeof(struct _function_));
if (fct == NULL)
return NULL;
if (model==NULL)
fct->model = NULL;
else
{
fct->model = malloc(sizeof(char) * (strlen(model) + 1));
fct->model[0] = '\0';
if (fct->model==NULL){
fprintf(stderr,"Not enough memory!\n");
exit(1);
}
strcpy(fct->model, model);
}
fct->detail = NULL;
fct->brief = NULL;
fct->bug = NULL;
fct->f = NULL;
fct->Parameter = NULL;
fct->ret = NULL;
fct->def = NULL;
fct->next = NULL;
return fct;
}
=============================
void InsertFct(function* fct, content c)
{
if (type > 0)
{
type = c.code;
(*fct)->def = (char*) malloc(sizeof(char) * (strlen(c.message) + 1));
(*fct)->def[0]='\0';
if ((*fct)->def==NULL){
fprintf(stderr,"Not enough memory!\n");
exit(1);
}
strcpy((*fct)->def, c.message);
return;
}
switch (c.code)
{
case FN:
type = 0;
*fct = AllocateFct(c.message);
break;
case PARAM:
type = 0;
AddParameterFunction(*fct, c.message);
break;
case BRIEF:
type = 0;
AddBriefFunction(*fct, c.message);
break;
case DETAILS:
type = 0;
AddDetailFunction(*fct, c.message);
break;
case RETURN:
AddRetourFunction(*fct, c.message);
break;
case BUG:
type = 0;
AddBugFunction(*fct, c.message);
break;
default:
type = c.code;
AddDefautFunction(*fct,c.message);
break;
}
}
===========================
int InsertShape(list heading, list source, shape* finalShape, shape* tmp)
{
fileRead* file = NULL;
shape s = NULL;
shape insert = AllocateShape(NULL);
function fct = AllocateFct(NULL);
function fctlist = NULL;
int i = 0;
int j = 0;
int pile = 0;
int n = 0;
int tag = 0;
list headerlist = NULL;
content c;
char chaine;
if (heading != NULL )
{
char* recover = (char*) GetNameFile(heading->name);
s = AllocateShape(recover);
free(recover);
recover=NULL;
}
else if (source != NULL )
{
char* recover = (char*) GetNameFile(source->name);
s = AllocateShape(recover);
free(recover);
recover=NULL;
}
if (heading != NULL )
{
file = OpenFileRead(heading->name);
n = CheckInclude(file->f, &headerlist);
if (n == 0)
n = 1;
fseek(file->f, -n, SEEK_CUR);
do
{
i = 0;
j = RecoverComments(file, &c, &tag);
InsertFct(&fct, c);
if(c.message!=NULL){
free(c.message);
c.message=NULL;
}
if (type > 0)
AddToShape(insert, fct);
if (j == -5 && i == 0)
{
type = 0;
do
{
if (fscanf(file->f, "%c", &chaine) == EOF)
{
i = 0;
break;
}
} while (chaine == ' ' || chaine == '\n');
if (chaine == '/')
{
i = 1;
j = 0;
fseek(file->f, -1, SEEK_CUR);
}
}
if (i == 0 && (j == -5))
{
if (type == 0)
{
pile++;
fctlist = AddFunction(fctlist, fct);
}
else
type = 0;
}
} while (j != -3);
FreefileRead(file);
}
i = 0;
j = 0;
type = 0;
if (source != NULL )
{
file = OpenFileRead(source->name);
n = CheckInclude(file->f, &headerlist);
if (n == 0)
n = 1;
fseek(file->f, -n, SEEK_CUR);
do
{
i = 0;
j = RecoverComments(file, &c, &tag);
InsertFct(&fct, c);
if(c.message!=NULL){
free(c.message);
c.message=NULL;
}
if (type > 0)
AddToShape(insert, fct);
if (j == -5 && i == 0)
{
type = 0;
do
{
if (fscanf(file->f, "%c", &chaine) == EOF)
{
i = 0;
break;
}
} while (chaine == ' ' || chaine == '\n');
if (chaine == '/')
{
i = 1;
j = 0;
fseek(file->f, -1, SEEK_CUR);
}
}
if (i == 0 && (j == -5))
{
if (type == 0)
fctlist = TourAndChange(fctlist, fct);
else
type = 0;
}
} while (j != -3);
FreefileRead(file);
}
s->fctList = AddFunction(s->fctList, fctlist);
s->headerList = headerlist;
*finalShape = CreateListShape(*finalShape, s);
*tmp = CreateListShape(*tmp, insert);
return 1;
}
=====================
The part concerned in the main :
InsertShape(tmpheading, tmproot, &s, &s2);
if (heading == NULL){
tmpheading = NULL;
}
if (root == NULL){
tmproot = NULL;
}
}
Thank you in advance for your response.
Well when do you ever free it? It doesn't look like you have a single free in your code! (Except for "freefileread", but we have no clue what that does and it doesn't seem designed for that function!)
Things I noticed:
fct->model = malloc(sizeof(char) * (strlen(model) + 1));
fct->model[0] = '\0';
if (fct->model==NULL){
fprintf(stderr,"Not enough memory!\n");
exit(1);
}
strcpy(fct->model, model);
Here the fct->model[0] = '\0'; could be dereferencing a null pointer. It should be removed. Also, this block of code could be replaced with a call to strdup.
void InsertFct(function* fct, content c)
{
if (type > 0)
... what is type? Some kind of global? Bad design if so.
The code following this could, again, be replaced with a call to strdup.
InsertShape(tmpheading, tmproot, &s, &s2);
if (heading == NULL){
tmpheading = NULL;
}
if (root == NULL){
tmproot = NULL;
}
Why do you set the pointers to NULL without freeing them first? BTW, free(NULL) does nothing, so you don't have to do something like if(ptr) free(ptr);... just call free(ptr);.