Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


dynamic arrays, stucts and reading from files

Posted on 2004-10-10
Medium Priority
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))
            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.

Question by:FlabbyDabbyDooDaah
  • 4
  • 2
  • 2
  • +1
LVL 23

Expert Comment

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.


Author Comment

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!!

LVL 22

Expert Comment

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 ) );

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 23

Assisted Solution

brettmjohnson earned 400 total points
ID: 12277507

> "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,
    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.

Author Comment

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

Expert Comment

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

Accepted Solution

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

structure * array;

and then say:


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.


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.  


Author Comment

ID: 12280537
>What is numberOfLines() actually returning?

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

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.


Author Comment

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 ;)

Featured Post

Independent Software Vendors: 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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.

564 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