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

x
?
Solved

code fix

Posted on 2000-03-08
6
Medium Priority
?
190 Views
Last Modified: 2010-04-15
help..
somethings wrong w/ this code..
esp on mallocing.

#define MAX 20

typedef struct indexrecord {
   int    index[26];
   long   start;
   long   count;
} indexrecord;

indexrecord *idxrec[MAX];
long wordctr=0;

void readindex(void) {
   FILE *fpidx;
   register int i, j;
   int intvar;
   long longvar;

   fpidx = fopen("index.txt", "r");
   if (!fpidx) {
       printf("error opening file\n");
       exit(1);
   }    
   for (j=0; j<25; j++) {
      idxrec[j] = (indexrecord *) malloc(sizeof(indexrecord));
      for (i=0; i<26; i++) {
         idxrec[j]->index[i] = (int) malloc(sizeof(int));
         idxrec[j]->index[i] = 0;
        }
        idxrec[j]->count = (long) malloc(sizeof(longvar));
        idxrec[j]->start = (long) malloc(sizeof(longvar));
      idxrec[j]->count = 0;
      idxrec[j]->start = 0;
   }

   for (j=0; j<23; j++) {
      for (i=0; i<26; i++) {
         fscanf(fpidx, "%d", &intvar);
         idxrec[j]->index[i] = intvar;
      }
      fscanf(fpidx, "%ld", &longvar);
      idxrec[j]->start = longvar;
        fscanf(fpidx, "%ld", &longvar);
        idxrec[j]->count = longvar;
        wordctr += longvar;
   }
   fclose(fpidx);
}
0
Comment
Question by:ibo
[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
  • 2
  • 2
6 Comments
 
LVL 2

Accepted Solution

by:
bbousquet earned 150 total points
ID: 2598658
Why are you using mallocs everywhere?

You just need to malloc each indexrecord structure once and store the pointer in your idxrec array. No need to allocate very single member from the structure.

[and don't forget to use free to deallocate the malloc'ed vars]

0
 
LVL 3

Expert Comment

by:terajiv
ID: 2599119
Hi ibo,
see the code below
////////////////////////////////////////
for (j=0; j<25; j++) {
      idxrec[j] = (indexrecord *) malloc(sizeof(indexrecord));
      for (i=0; i<26; i++) {
         idxrec[j]->index[i] = (int) malloc(sizeof(int));
         idxrec[j]->index[i] = 0;
  }
  idxrec[j]->count = (long) malloc(sizeof(longvar));
  idxrec[j]->start = (long) malloc(sizeof(longvar));
      idxrec[j]->count = 0;
      idxrec[j]->start = 0;
   }
//////////////////////////////////////////
Here u have already allocated memory to the Structure by Typing
>>idxrec[j] = (indexrecord *) malloc(sizeof(indexrecord));

and after that again ur allocating memory to the same structure elements.

See when u allocate memory to Structure memory is allocated to the Members. So no need to Allocate it Seperately and this is the main cause of ur Problem Remove that and see u will get good Result.

Rajiv
0
 
LVL 2

Expert Comment

by:bbousquet
ID: 2599168
terajiv, that is EXACTLY what I just said above.

It is the custom around here to respond to questions with comments. The question asker will then decide which comment better answers his problem/question. That way, the question is not locked for others to answer.

0
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.

 
LVL 3

Author Comment

by:ibo
ID: 2600825
sorry terjiv but i have to give the credit to whoever gave the answer first.
comments are answers too.
0
 
LVL 3

Expert Comment

by:terajiv
ID: 2603148
Its ok...Actually I should say Sorry...
Anyway
The Imp thing is ur Problem is solved.

So Cheers
Rajiv
0
 
LVL 3

Author Comment

by:ibo
ID: 2604933
hi im back..
so what was left was

idxrec[j] = (indexrecord *) malloc(sizeof(indexrecord));

i replaced it with:
*idxrec = (indexrecord *) malloc(sizeof(indexrecord));

but it produced a core dump. it crashed.

sorry i didnt really tried the solution right away.. i just assumed it was the right thing.
actually i had my reason for allocating each element in the structure. i originally programmed this in turbo c, tried this way but didnt work.. so i was able to make it work only by allocating each of the elements. but now in linux it doesnt work.
help pls. im not yet very familiar with unix c.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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…
The goal of this video is to provide viewers with basic examples to understand opening and reading files 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.

664 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