Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

algorithm for my friend

Posted on 2004-04-20
4
Medium Priority
?
217 Views
Last Modified: 2010-04-15
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?
0
Comment
Question by:oldb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10875312
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
 
LVL 2

Expert Comment

by:anupvijay
ID: 10876138
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
 

Author Comment

by:oldb
ID: 10911730
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
 
LVL 45

Accepted Solution

by:
sunnycoder earned 320 total points
ID: 10918760
> (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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question