DSPRelated.com
Forums

Reading list of floats from a file

Started by Gina Upperman November 17, 2005
Hi All -
I am trying to run the following code to read in the lists of floats stored
in different files. Right now, the code works as expected. However, if I
try to increase the size of "nums" (ie: double nums [20]), the program
crashes with the following error:
Trouble running Target CPU: Illegal opcode (80004e36) at pc = 0x00000200
Is there a limit to what I can declare "nums" as? Is there any other
workaround so that I could increase the size of "nums" without the program
crashing? Any suggestions would be appreciated.
Thank you very much,

Gina Upperman
----
----------------- #include <stdio
struct word

{

char * word_string;

double nums [10];

short nums_length;

};
int readVector(double nums [], char * filename);

void buildCodebook(struct word * codebook);
void main()

{

struct word * codebook;

buildCodebook(codebook);

}
int readVector(double nums [], char * filename)

{

FILE *f1;

int index = 0;

float num = 0.0;
f1 = fopen(filename, "r");

if (f1==0)

{

printf("\n input error\n");

return 0;

}
fscanf(f1, " %f\n", &num);

while ( num != 9999. )

{

nums[index]=num;

index++;

fscanf(f1, " %f\n", &num);

}
fclose(f1);

return index;

} void buildCodebook(struct word * codebook)

{

short i;
codebook[0].word_string = "C:\\test_file2.txt";

codebook[1].word_string = "C:\\test_file.txt";

codebook[2].word_string = "C:\\test_file2.txt";
for(i = 0; i < 3; i++)

{

codebook[i].nums_length =
readVector(codebook[i].nums, codebook[i].word_string);

}

}


It appears that you are not initializing the pointer in main. You are probably corrupting code memory when writing to the array.

Change main to this and try it. This places the data on the stack which has it's own limits.

void main()
{
struct word codebook;
buildCodebook(&codebook);
} TTFN,

Matt Townsend

Gina Upperman <upperman@uppe...> wrote: Hi All -
I am trying to run the following code to read in the lists of floats stored
in different files. Right now, the code works as expected. However, if I
try to increase the size of "nums" (ie: double nums [20]), the program
crashes with the following error:
Trouble running Target CPU: Illegal opcode (80004e36) at pc = 0x00000200
Is there a limit to what I can declare "nums" as? Is there any other
workaround so that I could increase the size of "nums" without the program
crashing? Any suggestions would be appreciated.
Thank you very much,

Gina Upperman
----
----------------- #include <stdio
struct word

{

char * word_string;

double nums [10];

short nums_length;

};
int readVector(double nums [], char * filename);

void buildCodebook(struct word * codebook);
void main()

{

struct word * codebook;

buildCodebook(codebook);

}
int readVector(double nums [], char * filename)

{

FILE *f1;

int index = 0;

float num = 0.0;
f1 = fopen(filename, "r");

if (f1==0)

{

printf("\n input error\n");

return 0;

}
fscanf(f1, " %f\n", &num);

while ( num != 9999. )

{

nums[index]=num;

index++;

fscanf(f1, " %f\n", &num);

}
fclose(f1);

return index;

} void buildCodebook(struct word * codebook)

{

short i;
codebook[0].word_string = "C:\\test_file2.txt";

codebook[1].word_string = "C:\\test_file.txt";

codebook[2].word_string = "C:\\test_file2.txt";
for(i = 0; i < 3; i++)

{

codebook[i].nums_length =
readVector(codebook[i].nums, codebook[i].word_string);

}

}


Hi Upperman,
I would see a programming bug in your program.
Even in your older queries you are commiting the same error.
Your are trying to use a pointer with out caring for where pointer is
referecing valid memory location. You have to properlly allocate memory to
that pointer.
Try the following piece of code ( try to understand the rational behind):
void main()
{
struct word array[3];
struct word * codebook;
codebook = &array[0]; //Pointer is initiazed to the start of the array

buildCodebook(codebook);
}
Regards,
Venugopala Krishna M
On 11/18/05, Gina Upperman <upperman@uppe...> wrote:
>
> Hi All - >
> I am trying to run the following code to read in the lists of floats
> stored
> in different files. Right now, the code works as expected. However, if I
> try to increase the size of "nums" (ie: double nums [20]), the program
> crashes with the following error: >
> Trouble running Target CPU: Illegal opcode (80004e36) at pc = 0x00000200 >
> Is there a limit to what I can declare "nums" as? Is there any other
> workaround so that I could increase the size of "nums" without the program
> crashing? Any suggestions would be appreciated. >
> Thank you very much,
>
> Gina Upperman >
>
> ----
> ----------------- > #include <stdio >
> struct word
>
> {
>
> char * word_string;
>
> double nums [10];
>
> short nums_length;
>
> }; >
> int readVector(double nums [], char * filename);
>
> void buildCodebook(struct word * codebook); >
> void main()
>
> {
>
> struct word * codebook;
>
> buildCodebook(codebook);
>
> } >
> int readVector(double nums [], char * filename)
>
> {
>
> FILE *f1;
>
> int index = 0;
>
> float num = 0.0; >
> f1 = fopen(filename, "r");
>
> if (f1==0)
>
> {
>
> printf("\n input error\n");
>
> return 0;
>
> } >
> fscanf(f1, " %f\n", &num);
>
> while ( num != 9999. )
>
> {
>
> nums[index]=num;
>
> index++;
>
> fscanf(f1, " %f\n", &num);
>
> } >
> fclose(f1);
>
> return index;
>
> } > void buildCodebook(struct word * codebook)
>
> {
>
> short i; >
> codebook[0].word_string = "C:\\test_file2.txt";
>
> codebook[1].word_string = "C:\\test_file.txt";
>
> codebook[2].word_string = "C:\\test_file2.txt"; >
> for(i = 0; i < 3; i++)
>
> {
>
> codebook[i].nums_length =
> readVector(codebook[i].nums, codebook[i].word_string);
>
> }
>
> }



Hi Gina,

As Matt and Krishna pointed out, the amount of stack has to be inctreased
and the pointer to the array of structures has to be initialized. Another
suggestion is to modify the structure itself, in order to increase the
size of arrays of doubles and allocate a space for file name array:

struct word
{
char word_string[100];
double nums [1000];
short nums_length;
};

This structure takes 100+1000*8+2 = 8102 bytes or 1FA6 hex (if char is a
1 byte (not unicode), double is an 8 byte and short is a 2 byte). The amount
of stack has to be increased accordingly. The structure can hold a file name
up to 99 characters and an array of 1000 64 bit double precision numbers.

void main()
{
struct word codebook[3];
buildCodebook (codebook);
}

The above segment data requires 3*8102 bytes more on the stack (5EF2 hex).

Regards,

Andrew

P.S. What was the program that doubles the equal sign "=" ? I receive
a daily digest and Krishna's email has been modified in the digest
# 404. In another line the equal sign has been promoted to "=>". Weird...

> Date: 19 Nov 2005 00:50:06 -0000
> From: code-comp@code...
> To: code-comp@code...
> Subject: [code-comp] Digest Number 403
>
> Date: Thu, 17 Nov 2005 13:23:00 -0600
> From: "Gina Upperman" <upperman@uppe...>
> Subject: Reading list of floats from a file
>
> Hi All -
>
> I am trying to run the following code to read in the lists of floats stored
> in different files. Right now, the code works as expected. However, if I
> try to increase the size of "nums" (ie: double nums [20]), the program
> crashes with the following error:
>
> Trouble running Target CPU: Illegal opcode (80004e36) at pc = 0x00000200
>
> Is there a limit to what I can declare "nums" as? Is there any other
> workaround so that I could increase the size of "nums" without the program
> crashing? Any suggestions would be appreciated.
>
> Thank you very much,
>
> Gina Upperman
>
> ----
>
> #include <stdio>
>
> struct word
> {
> char * word_string;
> double nums [10];
> short nums_length;
> };
>
> int readVector (double nums [], char * filename);
> void buildCodebook (struct word * codebook);
>
> void main()
> {
> struct word * codebook;
> buildCodebook (codebook);
> }
>
> int readVector(double nums [], char * filename)
> {
> FILE *f1;
> int index = 0;
> float num = 0.0;
>
> f1 = fopen (filename, "r");
>
> if (f1 == 0)
> {
> printf ("\n input error\n");
> return 0;
> }
>
> fscanf (f1, " %f\n", &num);
>
> while (num != 9999.0)
> {
> nums[index]=num;
> index++;
> fscanf(f1, " %f\n", &num);
> }
>
> fclose (f1);
> return index;
> }
>
> void buildCodebook(struct word * codebook)
> {
> short i;
>
> codebook[0].word_string = "C:\\test_file2.txt";
> codebook[1].word_string = "C:\\test_file.txt";
> codebook[2].word_string = "C:\\test_file2.txt";
>
> for (i = 0; i < 3; i++)
> {
> codebook[i].nums_length =
> readVector(codebook[i].nums, codebook[i].word_string);
>
> }
> }
>
> ________________________________________________________________________
> ________________________________________________________________________
>