Solved

Pretty easy question in C

Posted on 2003-12-05
4
324 Views
Last Modified: 2010-05-18
I have this program which i wrote in C.  Right now it just needs to open a file, then read the pairs of parts from the file (word then int, word then int, over and over) until there are no more pairs and stop.  In my try_to_read function my fscanf is working perfectly and everything dealing with the int y part is working good, however, I have an error in there and im storing the string wrong.  The program compiles fine with no errors.  My teacher told me how to fix the program, but he wants to know, if i leave my program like it is now, where are the words actually being stored to.  (Like are they being put into random bits, or the adress of my struct or what, and i cant figure out where they are going, like what do i have to printf in order to get them out)

struct part
{
char x[20];
int y;
};

int try_to_read_part(FILE *fp, struct part *p);

int main()
{
      struct part p;
      FILE *fp;
      fp=fopen("parts.dat","r");
      while(try_to_read_part(fp, &p);      
      return 0;
}

int try_to_read_part(FILE *fp, struct part *p)
{
      if (fscanf(fp,"%s%d",&(p->x[20]),&(p->y))==2) ********(line im talking about)
            return 1;
      return 0;
}

************(fscanf(fp,"%s%d",p->x,&(p->y))==2) ********correct way to do it
0
Comment
Question by:mastergamer232
  • 2
4 Comments
 
LVL 22

Expert Comment

by:cookre
ID: 9885856
Interesting to see what the compiler thinks:

The 'proper' way:
mov     eax, DWORD PTR _p$[esp-4]
mov     edx, DWORD PTR _fp$[esp-4]
lea     ecx, DWORD PTR [eax+20]
push    ecx
push    eax
push    OFFSET FLAT:??_C@_04MOFI@?$CFs?$CFd?$AA@ ; `string'
push    edx
call    _fscanf
add     esp, 16                        ; 00000010H
xor     ecx, ecx
cmp     eax, 2
sete    cl
mov     eax, ecx
The 'wrong way'
mov     eax, DWORD PTR _p$[esp-4]
mov     ecx, DWORD PTR _fp$[esp-4]
add     eax, 20                        ; 00000014H
push    eax
push    eax
push    OFFSET FLAT:??_C@_04MOFI@?$CFs?$CFd?$AA@ ; `string'
push    ecx
call    _fscanf
add     esp, 16                        ; 00000010H
xor     edx, edx
cmp     eax, 2
sete    dl
mov     eax, edx

0
 
LVL 2

Accepted Solution

by:
joele23 earned 300 total points
ID: 9885888
Hi,

so basically you have an array of 20 characters, the starting index at index 0 and going to 19.
In your fscanf you the string you scan in goes to &(p->x[20]) which is saying store the string starting at the memory location of p and shift 20 characters. So basically wrinting into memeory that is just past what you allocated. This memory could really contain anything and it is not allocated for your use.

Another thing to note is that if even when you do it with your teachers 'correct' way this is possibly not safe either. In your teacher case the p->x is the memory location with a 0 offset. Which is what you want but if the string(word) you are reading in is longer than 20 characters then you have once again passed the bounds of memory allocated to it and your writing into that unassigned memory again.
0
 

Author Comment

by:mastergamer232
ID: 9886021
thanks joele23, u win
0
 
LVL 22

Expert Comment

by:cookre
ID: 9886316
FWIW:

struct part
       {
       char x[20];
       int y;
       };

void silly(struct part *);


void main(int argc, char* argv[])
{
struct part p;

strcpy(p.x,"Howdy Do!");
p.y=1234;
printf("<%s> (%d)\n",p.x,p.y);
silly(&p);
return;
}

void silly(struct part *p)
{
printf("  Right: %08x\n",p->x);
printf("  Wrong: %08x\n",&(p->x[20]));
printf("p is at: %08x\n",p);
printf("Other 1: %08x\n",&(p->x)[20]);
printf("Other 2: %08x\n",(&(p->x))[20]);
return;
}

<Howdy Do!> (1234)
    Right: 0012ff6c
  Wrong: 0012ff80
   p is at: 0012ff6c
Other 1: 0012ff80
Other 2: 001300fc

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This is about my first experience with programming Arduino.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now