• C

algorithm for my friend

Hi experts,

int getdata(char *filename, ITEMS *items)
{
int i = 0;
char line [MAX_STR_LEN];
FILE *file_in;

if ( ( file_in = fopen (filename,"r")) == NULL) exit(EXIT_FAILURE);

 while ( fgets (line, MAX_LEN, file_in != NULL){

      sscanf (line,"%d %s", &item[i].number,item[i].string);
      i++;
 }
 return i;
}

Is this the best way to get data from a file ? How do I use malloc to replace MAX_STR_LEN so that I do not need to waste the space?
I would also like to do a timing algorithm to count the efficieny of my funtions, (eg sorting), how do I do that?
oldbAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
sunnycoderConnect With a Mentor Commented:
> (items+(*nel))->string = malloc(sizeof(char)*strlen(word) );
items here is an uninitialized pointer ... It can point to any memory location .... when you add *nel to it, you are adding *nel to some unknown qunatity ... When you dereference the result (->string), you are trying to dereference a random location which is not allocated to your program ... Thus your program crashes ...

To fix the issue, allocate memory to items before dereferencing it

items = (ITEMS *) malloc (sizeof(ITEM) * (*nel) ); ... perhaps you would like to initialize these elemets too before you derefernce them or you will get another seg fault for char * in the struct items (they will also have random values)
0
 
sunnycoderCommented:
Hi oldb,

> Is this the best way to get data from a file ?
Looks good ... using fscanf() would have been a bit more efficient ...

>How do I use malloc to replace MAX_STR_LEN so
> that I do not need to waste the space?
malloc will simply allocate the space in heap instead of stack ... you still need to allocate the space ... If you do not wish to use this memory, fscanf from the file directly into the array of ITEMS

for timing, use a profiling tool such as gprof or use clock () function

Sunnycoder
0
 
anupvijayCommented:
As Sunny pointed out malloc doesn't save space but it gives the user the control to decide when you need the memory and
when you don't. So when you need it you allocate and when you don't need it you free it.
But in case of arrays as in ur case it is static memory on stack and will be invalid once out of scope. And you can't free it.

So depending on the case malloc allows you to use memory more efficiently by not holding a memory block when you
don't need it. If we wanna call it saving space then ....well....go ahead.

But in your case you do not know how many bytes you are gonna get from reading a line from the file.
So the char[] will do just good.

But if you need this array in some other function of your code then malloc(heap) might be a better idea.But it doesn't look like
you require that.

Hope you find the info useful.

Cheers.
0
 
oldbAuthor Commented:
I have actually written this, but this gives SEGMENTATION FAULT when I try to access items. (eg items->number)

typedef struct items ITEMS;

struct items{ int number;
                   char *string;
                   };

/* nel is the number of elements */
ITEMS *getdata(char *filename, int *nel){

int num;
char word[MAX_STR_LEN];
FILE *file_in;
ITEMS *items;

if ( (file_in = fopen(filename,'r')) == NULL)
               error;

while ( !feof(file_in)){

         fscanf(file_in ,"%d %s" , &num,word);
         (items+(*nel))->string = malloc(sizeof(char)*strlen(word) );
          items = realloc( items, sizeof(ITEMS)*(*nel+1) );

          (items+(*nel))->number = num;
          strcpy((items+(*nel))->string, word);
          *nel++;
           }

fclose(file_in);
return items;
}

When I run the programs, It gives a segmentation fault. I used GDB and think that it stops at the line where I access (items+(*nel))->string.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.