Solved

dynamic arrays, stucts and reading from files

Posted on 2004-10-10
9
487 Views
Last Modified: 2012-06-27
Sorry for the vagueness of the title but ive got one of those problems...

Ok...im trying to read in from a file line by line and split each line up into seperate strings and then store the data in a struct.

basically ive got a function(tokeniser) which takes the stucture and the line and splits up and stores the data.

                structure *array;
                array = malloc(sizeof(structure) *numberOfLines());


                while(fgets(line, 99, file))
      {
            n++;
            tokeniser(&array[n], line);
            
      }

Now this sort of works...it reads in around half the the file into the structure which i can print out as i wish. However around half way through the while loop it just stops. Running debug it gives:

Unhandled exception at 0x7c93793d in test.exe: 0xC0000005: Access violation writing location 0x00030ffc.

There is no problem with the file im reading as it is of uniform format all the way through.
So i assume there is something wrong with the array structure.

But as it works for part of the file i am clueless as to what is wrong.

Any suggestions as to what is wrong...or alternative solutions would be very much appreciated.



0
Comment
Question by:FlabbyDabbyDooDaah
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 12273632
Time to debug the program.  Run it under the debugger (either the integrated one in your IDE
or an external one like gdb).   When the access violation occurs,  execution will halt and the
debugger will show the point of failure.  Examine the value of the variables to determine the
fault.  I suspect n exceeds the value returned by numberOfLines().  Also note that array indexes
are zero-based, so if numberOfLines() returns the true number of lines in the file, you will write
passed the end of the array on the last line.


0
 

Author Comment

by:FlabbyDabbyDooDaah
ID: 12274983
when i run debug and choose break when the access violation occurs i get:

"There is no source code available for the current location."

And in the disassembler the point of the problem is:

7C93793D  call        7C90112B

but this means nothing to me.

Ive made sure that my indexing is right ie the first line is line 0. So it shudnt write passed the end of the array...but since its only getting half way through that currently isnt the problem.

number of lines returns the value 91 which i use to set the size of the array.
Then when i run loop to send the lines through the tokeniser to store them, it stops at line 47.

This is driving me crazy!!


0
 
LVL 22

Expert Comment

by:grg99
ID: 12275583
"structure * array"   doesnt look right.

If you want  a pointer to an array of structures you need something more like:

typedef struct { int x,y, ... } MyStruct;

typedef  MyStruct    ArrayofThem[100];

typedef   ArrayOfThem *   PtrToArrayOfThem;


PtrtoArrayOfThem   PAT;

PAT = (PtrToArrayOfThem) malloc( sizeof( ArrayOfThem ) );


0
 
LVL 23

Assisted Solution

by:brettmjohnson
brettmjohnson earned 100 total points
ID: 12277507
grg99,

> "structure * array"   doesnt look right.

If structure is typedef'd as follows:
typedef struct { int x,y, ... } structure;

Then, array is a pointer to one or more objects of type structure,
and
    array = (structure *)malloc(sizeof(structure) *numberOfLines());
is an appropriate way to allocate the array.  [note my added cast].

The extra levels of obfuscating typedefs are not needed.  Specifically,
defining a type of fixed sized array (ArrayofThem) is completely inappropriate,
as the number of lines appears to vary.   Of course, depending upon the
implementation of numberOfLines(), it may be more appropriate to use
realloc() instead.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:FlabbyDabbyDooDaah
ID: 12277941
hi grg99

structure is typedef'ed and ive added the extra cast.

numberOflines() uses an fget loop to count through the lines until a NULL line is found and returns the number...which is 91 in the file im using.

So: array = (structure *)malloc(sizeof(structure) *numberOfLines());
should create an array 91 of structures right?

in the tokeniser() function ive tried starting at an index of 40 and going on from there...and it still stops at 47. And ive also tried starting at 47+ and nothing is stored  and the error is given right away.

So it appears that: array = (structure *)malloc(sizeof(structure) *numberOfLines());
is only creating an array of 47 structures for some reason...and tokeniser() is going out of bounds.

i dont think realloc is appropriate as i only need to allocate memory to the array once at runtime and malloc should do the job just fine.

thanks for your help so far...
0
 
LVL 7

Expert Comment

by:ravs120499
ID: 12278208
What is numberOfLines() actually returning?
0
 
LVL 22

Accepted Solution

by:
grg99 earned 250 total points
ID: 12278671
Hmmm, I'm really concerned about when you say:

structure * array;

and then say:

  &array[n]

Since array is a pointer to a structure, array[0] is a pointer to a structure, that works fine.

But "array[1]" is another pointer to a structure, located at array+sizeof(pointer).

What we want it to look like is a pointer to an ARRAY OF STRUCTURES.

not

an array of pointers to structures.

That may be the problem.   Then again with C the average joe is wrong 75% of the time, so IMBFOS.

That's why I always do the long-winded string of typedefs, so it's clear to both ME and to the COMPILER what I'm declaring.  


0
 

Author Comment

by:FlabbyDabbyDooDaah
ID: 12280537
ravs...
>What is numberOfLines() actually returning?

it returns an int which is the number of lines in the file...which is currently 91.

grg99...
i see your point with:   &array[n]

and have changed the line to:    tokeniser(array++, line);
is that better?

and i get the exact same results....the first 47 or so lines are split up and stored and printed as i want but the program stops and gets the exception and doesnt do the remaining 40 or so lines.

0
 

Author Comment

by:FlabbyDabbyDooDaah
ID: 12280933
guess what...the problem was with a few lines that i didnt show you...it was just how i was passing the array into a function and pointers and address etc...sorry to waste your time...hope some points will help ;)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
how to declare a 2-dimensional version of strlen 4 197
C hashtable library 3 71
why "." vs "->" 23 116
Need example 5 101
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
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.

747 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

11 Experts available now in Live!

Get 1:1 Help Now