Solved

Pretty easy question in C

Posted on 2003-12-05
4
323 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

12 Experts available now in Live!

Get 1:1 Help Now