Board index » cppbuilder » help with kernel32.dll errors during file output

help with kernel32.dll errors during file output


2005-01-27 04:17:47 AM
cppbuilder85
Hello, I'm new and I hope I can get some help here.
I am having trouble getting my code to output data to a file. I have the Borland compiler installed on both an XP and ME machines, and both machines will give the same error. The code will execute fine, print my data to the screen, even create the file that I want it to, but it crashes when it tries to write the file. I ran the program on a mac without problem, it gave me my output file perfectly.
Since the program compiles just fine, I have to suspect that it is a problem with my executables and Windows more than anything else. However, if anyone has experienced a similar problem and knows how to fix it, I'd appreciate it.
The fault code is 0xc0000005 and is associated with kernel32.dll and other dlls.
Thanks, Mark
 
 

Re:help with kernel32.dll errors during file output

As it is written the reason for that fault is difficult or impossible to
answer.
There are many different ways to create, write to and close a disk file.
Please show some code and say what version of the compiler was used.
. Ed
Quote
Mark Squires wrote in message
news:41f7faeb$ XXXX@XXXXX.COM ...

Hello, I'm new and I hope I can get some help here.

I am having trouble getting my code to output data to a file. I
have the Borland compiler installed on both an XP and ME
machines, and both machines will give the same error. The
code will execute fine, print my data to the screen, even
create the file that I want it to, but it crashes when it tries to
write the file. I ran the program on a mac without problem, it
gave me my output file perfectly.

Since the program compiles just fine, I have to suspect that it
is a problem with my executables and Windows more than
anything else. However, if anyone has experienced a similar
problem and knows how to fix it, I'd appreciate it.

The fault code is 0xc0000005 and is associated with kernel32.dll
and other dlls.
 

Re:help with kernel32.dll errors during file output

I am using Borland C++ Compiler 5.5, the free compiler that they
are now offering. Here is the piece of code that writes my
file:
#define FILENAME "output.dat"
double density, c, k, tau;
int main (void)
{
/*File Pointer*/
FILE *out;
/*Declare and initialize variables*/
double *results;
.... (other functions here)
/* Print to file */
out = fopen(FILENAME, "w");
for (i = 0; i < 4*2*t*n; i += 4)
{
fprintf(out, "%lf\t", *(results + i));
fprintf(out, "%lf\t", *(results + i + 1));
fprintf(out, "%lf\t", *(results + i + 2));
fprintf(out, "%lf\n", *(results + i + 3));
}
printf("\nResults found in output.dat.\n\n");
fclose(out);
printf("\n");
Thanks for the help.
"Ed Mulroy [TeamB]" < XXXX@XXXXX.COM >wrote:
Quote
As it is written the reason for that fault is difficult or impossible to
answer.

There are many different ways to create, write to and close a disk file.
Please show some code and say what version of the compiler was used.

. Ed

>Mark Squires wrote in message
>news:41f7faeb$ XXXX@XXXXX.COM ...
>
>Hello, I'm new and I hope I can get some help here.
>
>I am having trouble getting my code to output data to a file. I
>have the Borland compiler installed on both an XP and ME
>machines, and both machines will give the same error. The
>code will execute fine, print my data to the screen, even
>create the file that I want it to, but it crashes when it tries to
>write the file. I ran the program on a mac without problem, it
>gave me my output file perfectly.
>
>Since the program compiles just fine, I have to suspect that it
>is a problem with my executables and Windows more than
>anything else. However, if anyone has experienced a similar
>problem and knows how to fix it, I'd appreciate it.
>
>The fault code is 0xc0000005 and is associated with kernel32.dll
>and other dlls.



 

{smallsort}

Re:help with kernel32.dll errors during file output

That code declares a pointer to double called 'results'. It writes the
values from results[0] through results[4*2*t*n - 1] to a file.
The allocation of memory and assignment of that allocation address to the
variable 'results' is not shown. The declaration types and values of the
variables 't' and 'n' which are in control of loop termination are not
shown. The assignments of values to elements in the array 'results' are not
shown.
My guess is that the program goes beyond the allocated end of the array
'results'. However the missing code insures that this can only be a guess.
. Ed
Quote
Mark Squires wrote in message
news:41f83693$ XXXX@XXXXX.COM ...

I am using Borland C++ Compiler 5.5, the free compiler that they
are now offering. Here is the piece of code that writes my
file:

#define FILENAME "output.dat"

double density, c, k, tau;

int main (void)

{
/*File Pointer*/
FILE *out;
/*Declare and initialize variables*/
double *results;
.... (other functions here)
/* Print to file */

out = fopen(FILENAME, "w");

for (i = 0; i < 4*2*t*n; i += 4)
{
fprintf(out, "%lf\t", *(results + i));
fprintf(out, "%lf\t", *(results + i + 1));
fprintf(out, "%lf\t", *(results + i + 2));
fprintf(out, "%lf\n", *(results + i + 3));
}

printf("\nResults found in output.dat.\n\n");
fclose(out);
printf("\n");
 

Re:help with kernel32.dll errors during file output

I'll post the whole code. I'm pretty sure that I allocated the
memory correctly, especially since I am able to run the
executable on a macintosh. I'll increase the memory allocated
anyway and see what happens.
Anyway, here is the whole program.
/****************************************************/
/* */
/* Hyperbolic Heat Conduction Solver */
/* By: Mark Squires */
/* 6 December 2004 */
/* */
/* Creates an array for hyperbolic heat conduction */
/* equation. Has initial condition of specified */
/* temperature and zero heat flux. */
/* Boundary conditions are specified temperature. */
/* */
/* Boundary and initial conditions may be changed */
/* easily by modifying code. */
/* User may define volumetric heat generation as */
/* function of position (x) and time (t) where */
/* indicated. */
/****************************************************/
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
double heat_gen (double x, double t);
#define FILENAME "output.dat"
double density, c, k, tau;
int main (void)
{
/*File Pointer*/
FILE *out;
/*Declare and initialize variables*/
double *results;
double x, x_new, width, delta_x, tmax, t_new, delta_t, time;
double gen, temp, flux, bound_zero, bound_width, deviation;
int i, j, n, t, count;
tau = .000000001;
printf("Enter Final Time and Delta t\n");
scanf("%lf %lf", &tmax, &delta_t);
printf("Enter Total Width and number of divisions (must be odd)\n");
scanf("%lf %d", &width, &n);
t = tmax/delta_t;
delta_x = 2*width/n;
n = .5*(n+1);
printf("delta x is %lf\n\n", delta_x);
printf("n is %d\n\n", n);
/*Allocate Memory*/
results = (double *)malloc(4*2*t*n*sizeof(double));
/*Physical Constants*/
printf("Enter Denstity (p) Thermal Conductivity (k) and Specific Heat (c)\n");
scanf("%lf %lf %lf", &density, &k, &c);
/*Establish Initial Condition and Boundary Condition*/
printf("Enter initial temperature\n");
scanf("%lf", &temp);
printf("Enter boundary temperature at x=0 and x=width\n");
scanf("%lf %lf", &bound_zero, &bound_width);
x_new = .5*delta_x;
for (i = 0; i < 4*n; i += 4)
{
x = x_new;
results[i] = time;
results[i+1] = x;
results[i+2] = temp;
results[i+3] = 0;
x_new = x + delta_x;
}
t_new = .5*delta_t;
for (j = 0; j < 2*t; j ++)
{
time = t_new;
count = pow(-1,j)*j;
printf("%d\t%d", j, count);
printf("\n");
if (count == j)
{
x_new = 0;
for (i = 4*n*(j+1); i < 4*n*(j+2); i += 4)
{
x = x_new;
results[i] = time;
results[i+1] = x;
if (x == 0)
{
results[i+2] = bound_zero;
results[i+3] = (1/(tau*delta_x + .25*delta_t*delta_x))*(-k*.5*delta_t*(results[i+2-4*n]) + (tau*.5*delta_x - .125*delta_t*delta_x)*(results[i+3-4*n]));
}
else
{
results[i+2] = (1/(density*c*delta_x))*((density*.5*c*delta_x)*(results[i+2-4*n] + results[i+2-4*(n+1)]) - .5*delta_t*(results[i+3-4*n]-results[i+3-4*(n+1)])+.25*delta_t*delta_x*(heat_gen(x,t) + .5*(heat_gen(x - .5*delta_x,t - .5*delta_t) + heat_gen(x + .5*delta_x,t - .5*delta_t))));
results[i+3] = (1/(tau*delta_x + .25*delta_t*delta_x))*(-k*.5*delta_t*(results[i+2-4*n] - results[i+2-4*(n+1)]) + (tau*.5*delta_x - .125*delta_t*delta_x)*(results[i+3-4*n] + results[i+3-4*(n+1)]));
}
x_new = x + delta_x;
}
}
else
{
x_new = .5*delta_x;
for (i = 4*n*(j+1); i < 4*n*(j+2); i += 4)
{
x = x_new;
deviation = ((x - width)/(width))*((x - width)/(width));
results[i] = time;
results[i+1] = x;
if (deviation <= 0.001)
{
results[i+2] = bound_width;
results[i+3] = (1/(tau*delta_x + .25*delta_t*delta_x))*(-k*.5*delta_t*(0 - results[i+2-4*n]) + (tau*.5*delta_x - .125*delta_t*delta_x)*(results[i+3-4*n]));
}
else
{
results[i+2] = (1/(density*c*delta_x))*((density*.5*c*delta_x)*(results[i+2-4*(n-1)] + results[i+2-4*(n)]) - .5*delta_t*(results[i+3-4*(n-1)]-results[i+3-4*(n)])+.25*delta_t*delta_x*(heat_gen(x,t) + .5*(heat_gen(x - .5*delta_x,t - .5*delta_t) + heat_gen(x + .5*delta_x,t - .5*delta_t))));
results[i+3] = (1/(tau*delta_x + .25*delta_t*delta_x))*(-k*.5*delta_t*(results[i+2-4*(n-1)] - results[i+2-4*(n)]) + (tau*.5*delta_x - .125*delta_t*delta_x)*(results[i+3-4*(n-1)] + results[i+3-4*(n)]));
}
x_new = x + delta_x;
}
}
t_new = time + .5*delta_t;
}
/* Print to file */
out = fopen(FILENAME, "w");
for (i = 0; i < 4*2*t*n; i += 4)
{
fprintf(out, "%lf\t", *(results + i));
fprintf(out, "%lf\t", *(results + i + 1));
fprintf(out, "%lf\t", *(results + i + 2));
fprintf(out, "%lf\n", *(results + i + 3));
}
printf("\nResults found in output.dat.\n\n");
fclose(out);
printf("\n");
/*print to screen*/
for (i = 0; i < 4*2*t*n; i += 4)
{
printf("%lf\t", *(results + i));
printf("%lf\t", *(results + i + 1));
printf("%lf\t", *(results + i + 2));
printf("%lf\n", *(results + i + 3));
}
printf("\n");
return (0);
}
/*Function Subroutines*/
double heat_gen (double x, double t)
{
double gen;
/*Enter heat generation as f(x,t) below*/
gen = .5*x*x*t;
return (gen);
}
"Ed Mulroy [TeamB]" < XXXX@XXXXX.COM >wrote:
Quote
That code declares a pointer to double called 'results'. It writes the
values from results[0] through results[4*2*t*n - 1] to a file.

The allocation of memory and assignment of that allocation address to the
variable 'results' is not shown. The declaration types and values of the
variables 't' and 'n' which are in control of loop termination are not
shown. The assignments of values to elements in the array 'results' are not
shown.

My guess is that the program goes beyond the allocated end of the array
'results'. However the missing code insures that this can only be a guess.

. Ed

>Mark Squires wrote in message
>news:41f83693$ XXXX@XXXXX.COM ...
>
>I am using Borland C++ Compiler 5.5, the free compiler that they
>are now offering. Here is the piece of code that writes my
>file:
>
>#define FILENAME "output.dat"
>
>double density, c, k, tau;
>
>int main (void)
>
>{
>/*File Pointer*/
>FILE *out;
>/*Declare and initialize variables*/
>double *results;
>.... (other functions here)
>/* Print to file */
>
>out = fopen(FILENAME, "w");
>
>for (i = 0; i < 4*2*t*n; i += 4)
>{
>fprintf(out, "%lf\t", *(results + i));
>fprintf(out, "%lf\t", *(results + i + 1));
>fprintf(out, "%lf\t", *(results + i + 2));
>fprintf(out, "%lf\n", *(results + i + 3));
>}
>
>printf("\nResults found in output.dat.\n\n");
>fclose(out);
>printf("\n");



 

Re:help with kernel32.dll errors during file output

"Mark Squires" < XXXX@XXXXX.COM >wrote:
Quote
out = fopen(FILENAME, "w");

for (i = 0; i < 4*2*t*n; i += 4)

{

fprintf(out, "%lf\t", *(results + i));
Why don't you check that the file is actually open? Because this program
may blow up, quite possibly nastily, if you pass a null pointer to
fprintf().
Alan Bellingham
--
ACCU Conference 2005 - 20-23 April, Randolph Hotel, Oxford, UK
 

Re:help with kernel32.dll errors during file output

As Mr Bellingham mentioned, that code does not check if the call to fopen
failed. That could be the source of the error you have been experiencing.
Side comments about that C code:
- Floating point literal numbers begin with a digit or sign and digit.
They do not begin with a period and if a compiler accepts a floating
point literal which begins with a period, consider it luck. Consider
changing '.5', '.125' and any others of that ilk to have an integral part
as in: '0.5' and '0.125'.
- An integer ('n') is read in and then used as a divisor. The prompt
says it is to be odd but no checking is done and no message is
issued to the user if it is wrong. Therefore the program can crash
in a manner where the user has no idea what went wrong. Consider
adding code to check the value is in an acceptable range and, if it
is not, issue a complaint message and end nicely instead of with
an operating system error.
- scanf is used for input but scanf. Consider instead using fgets
followed by sscanf and checking the return value for more robust
operation. A 'quicky' example to illustrate
---------------
printf("Enter Final Time and Delta t\n");
fgets(buf, sizeof(buf), stdin);
if (sscanf(buf, "%lf %lf", &tmax, &delta_t) != 2)
{
printf("Input error\n");
return EXIT_FAILURE;
}
---------------
- When you write an expression such as the one below you place
yourself at the mercy of the size of the available memory in the
floating point processor's stack. Depending upon the vintage of
the hardware in the machine where it is run the calculation is likely
to fail. It would be better to several calculations to intermediate
results and combine the intermediate results at the end.
---------------
results[i+2] = (1/(density*c*delta_x))*((density*.5*c*delta_x)*
(results[i+2-4*n] + results[i+2-4*(n+1)]) - .5*delta_t*
(results[i+3-4*n]-results[i+3-4*(n+1)])+.25*delta_t*delta_x*
(heat_gen(x,t) + .5*(heat_gen(x - .5*delta_x,t - .5*delta_t) +
heat_gen(x + .5*delta_x,t - .5*delta_t))));
---------------
- In a loop based on the integral counter 'j' the time spent in this
calculation is an extremely expensive to achieve the desired
result:
for (j = 0; j < 2 * t; j++)
{
count = pow(-1, j) * j;
Consider replacing the calculation of 'count' with something like
this:
for (j = 0; j < 2 * t; j++)
{
count = (j & 1) ? j : -j;
Also note that you are at the mercy of the compiler's optimizer to
remove the multiplication from the end of loop test. It might be
better to declare a variable, perhaps 't_times_2', whose value was
assigned '2 * t' earlier and then use it in the loop statement,
something like
this:
for (j = 0; j < t_times_2; j++)
. Ed
Quote
Mark Squires wrote in message
news:41f901d1$ XXXX@XXXXX.COM ...

I'll post the whole code. I'm pretty sure that I allocated the
memory correctly, especially since I am able to run the
executable on a macintosh. I'll increase the memory allocated
anyway and see what happens.

Anyway, here is the whole program.

...
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

double heat_gen (double x, double t);

#define FILENAME "output.dat"

double density, c, k, tau;

int main (void)

{
/*File Pointer*/
FILE *out;
/*Declare and initialize variables*/
double *results;

double x, x_new, width, delta_x, tmax, t_new, delta_t, time;
double gen, temp, flux, bound_zero, bound_width, deviation;
int i, j, n, t, count;

tau = .000000001;

printf("Enter Final Time and Delta t\n");
scanf("%lf %lf", &tmax, &delta_t);

printf("Enter Total Width and number of divisions (must be odd)\n");
scanf("%lf %d", &width, &n);

t = tmax/delta_t;
delta_x = 2*width/n;
n = .5*(n+1);

printf("delta x is %lf\n\n", delta_x);
printf("n is %d\n\n", n);

/*Allocate Memory*/

results = (double *)malloc(4*2*t*n*sizeof(double));

/*Physical Constants*/

printf("Enter Denstity (p) Thermal Conductivity (k) and Specific Heat
(c)\n");
scanf("%lf %lf %lf", &density, &k, &c);

/*Establish Initial Condition and Boundary Condition*/

printf("Enter initial temperature\n");
scanf("%lf", &temp);

printf("Enter boundary temperature at x=0 and x=width\n");
scanf("%lf %lf", &bound_zero, &bound_width);

x_new = .5*delta_x;

for (i = 0; i < 4*n; i += 4)
{

x = x_new;

results[i] = time;
results[i+1] = x;
results[i+2] = temp;
results[i+3] = 0;

x_new = x + delta_x;

}

t_new = .5*delta_t;
for (j = 0; j < 2*t; j ++)
{
time = t_new;
count = pow(-1,j)*j;

printf("%d\t%d", j, count);
printf("\n");

if (count == j)

{
x_new = 0;

for (i = 4*n*(j+1); i < 4*n*(j+2); i += 4)
{
x = x_new;

results[i] = time;
results[i+1] = x;

if (x == 0)

{

results[i+2] = bound_zero;

results[i+3] = (1/(tau*delta_x +
.25*delta_t*delta_x))*(-k*.5*delta_t*(results[i+2-4*n]) + (tau*.5*delta_x -
.125*delta_t*delta_x)*(results[i+3-4*n]));
Quote
}

else

{
results[i+2] =
(1/(density*c*delta_x))*((density*.5*c*delta_x)*(results[i+2-4*n] +
results[i+2-4*(n+1)]) -
.5*delta_t*(results[i+3-4*n]-results[i+3-4*(n+1)])+.25*delta_t*delta_x*(heat
_gen(x,t) + .5*(heat_gen(x - .5*delta_x,t - .5*delta_t) + heat_gen(x +
.5*delta_x,t - .5*delta_t))));
Quote

results[i+3] = (1/(tau*delta_x +
.25*delta_t*delta_x))*(-k*.5*delta_t*(results[i+2-4*n] -
results[i+2-4*(n+1)]) + (tau*.5*delta_x -
.125*delta_t*delta_x)*(results[i+3-4*n] + results[i+3-4*(n+1)]));
Quote
}

x_new = x + delta_x;

}
}

else

{
x_new = .5*delta_x;

for (i = 4*n*(j+1); i < 4*n*(j+2); i += 4)
{
x = x_new;
deviation = ((x - width)/(width))*((x - width)/(width));

results[i] = time;
results[i+1] = x;

if (deviation <= 0.001)

{

results[i+2] = bound_width;

results[i+3] = (1/(tau*delta_x + .25*delta_t*delta_x))*(-k*.5*delta_t*(0 -
results[i+2-4*n]) + (tau*.5*delta_x -
.125*delta_t*delta_x)*(results[i+3-4*n]));
Quote
}

else

{
results[i+2] =
(1/(density*c*delta_x))*((density*.5*c*delta_x)*(results[i+2-4*(n-1)] +
results[i+2-4*(n)]) -
.5*delta_t*(results[i+3-4*(n-1)]-results[i+3-4*(n)])+.25*delta_t*delta_x*(he
at_gen(x,t) + .5*(heat_gen(x - .5*delta_x,t - .5*delta_t) + heat_gen(x +
.5*delta_x,t - .5*delta_t))));
Quote

results[i+3] = (1/(tau*delta_x +
.25*delta_t*delta_x))*(-k*.5*delta_t*(results[i+2-4*(n-1)] -
results[i+2-4*(n)]) + (tau*.5*delta_x -
.125*delta_t*delta_x)*(results[i+3-4*(n-1)] + results[i+3-4*(n)]));
Quote
}

x_new = x + delta_x;

}
}

t_new = time + .5*delta_t;

}


/* Print to file */

out = fopen(FILENAME, "w");

for (i = 0; i < 4*2*t*n; i += 4)

{

fprintf(out, "%lf\t", *(results + i));
fprintf(out, "%lf\t", *(results + i + 1));
fprintf(out, "%lf\t", *(results + i + 2));
fprintf(out, "%lf\n", *(results + i + 3));

}


printf("\nResults found in output.dat.\n\n");
fclose(out);

printf("\n");



/*print to screen*/

for (i = 0; i < 4*2*t*n; i += 4)

{

printf("%lf\t", *(results + i));
printf("%lf\t", *(results + i + 1));
printf("%lf\t", *(results + i + 2));
printf("%lf\n", *(results + i + 3));

}

printf("\n");

return (0);

}
/*Function Subroutines*/

double heat_gen (double x, double t)

{
double gen;

/*Enter heat generation as f(x,t) below*/

gen = .5*x*x*t;

return (gen);

}





"Ed Mulroy [TeamB]" < XXXX@XXXXX.COM >wrote:
>That code declares a pointer to double called 'results'. It writes the
>values from results[0] through results[4*2*t*n - 1] to a file.
>
>The allocation of memory and assignment of that allocation address to the
>variable 'results' is not shown. The declaration types and values of the
>variables 't' and 'n' which are in control of loop termination are not
>shown. The assignments of values to elements in the array 'results' are
not
>shown.
>
>My guess is that the program goes beyond the allocated end of the array
>'results'. However the missing code insures that this can only be a
guess.
>
>. Ed
>
>>Mark Squires wrote in message
>>news:41f83693$ XXXX@XXXXX.COM ...
>>
>>I am using Borland C++ Compiler 5.5, the free compiler that they
>>are now offering. Here is the piece of code that writes my
>>file:
>>
>>#define FILENAME "output.dat"
>>
>>double density, c, k, tau;
>>
>>int main (void)
>>
>>{
>>/*File Pointer*/
>>FILE *out;
>>/*Declare and initialize variables*/
>>double *results;
>>.... (other functions here)
>>/* Print to file */
>>
>>out = fopen(FILENAME, "w");
>>
>>for (i = 0; i < 4*2*t*n; i += 4)
>>{
>>fprintf(out, "%lf\t", *(results + i));
>>fprintf(out, "%lf\t", *(results + i + 1));
>>fprintf(out, "%lf\t", *(results + i + 2));
>>fprintf(out, "%lf\n", *(results + i + 3));
>>}
>>
>>printf("\nResults found in output.dat.\n\n");
>>fclose(out);
>>printf("\n");
>
>
>

 

Re:help with kernel32.dll errors during file output

Well, I followed both of your suggestions. It turns out that
the file was indeed open, but that I needed to allocate more
memory as Ed suggested. Doing so solved my problem. I still
don't understand why though. I had a few other programs
that I had compiled and run on a UNIX machine with no problem,
(and the macintosh as well), yet when I run them on Windows I
have to allocate more memory or else I get the system failure?!?
I don't understand. Sorry to be such a dunderhead.
Thank you for your other suggestions. It seems that your
suggestion for the 'count' sequence is able to distinguish even
and odd numbers. Is that right? I couldn't find another way
to do it other than to raise a -1 to a power.
"Ed Mulroy [TeamB]" < XXXX@XXXXX.COM >wrote:
Quote
As Mr Bellingham mentioned, that code does not check if the call to fopen
failed. That could be the source of the error you have been experiencing.

Side comments about that C code:

- Floating point literal numbers begin with a digit or sign and digit.
They do not begin with a period and if a compiler accepts a floating
point literal which begins with a period, consider it luck. Consider
changing '.5', '.125' and any others of that ilk to have an integral part
as in: '0.5' and '0.125'.

- An integer ('n') is read in and then used as a divisor. The prompt
says it is to be odd but no checking is done and no message is
issued to the user if it is wrong. Therefore the program can crash
in a manner where the user has no idea what went wrong. Consider
adding code to check the value is in an acceptable range and, if it
is not, issue a complaint message and end nicely instead of with
an operating system error.

- scanf is used for input but scanf. Consider instead using fgets
followed by sscanf and checking the return value for more robust
operation. A 'quicky' example to illustrate

---------------
printf("Enter Final Time and Delta t\n");
fgets(buf, sizeof(buf), stdin);

if (sscanf(buf, "%lf %lf", &tmax, &delta_t) != 2)
{
printf("Input error\n");
return EXIT_FAILURE;
}
---------------

- When you write an expression such as the one below you place
yourself at the mercy of the size of the available memory in the
floating point processor's stack. Depending upon the vintage of
the hardware in the machine where it is run the calculation is likely
to fail. It would be better to several calculations to intermediate
results and combine the intermediate results at the end.

---------------
results[i+2] = (1/(density*c*delta_x))*((density*.5*c*delta_x)*
(results[i+2-4*n] + results[i+2-4*(n+1)]) - .5*delta_t*
(results[i+3-4*n]-results[i+3-4*(n+1)])+.25*delta_t*delta_x*
(heat_gen(x,t) + .5*(heat_gen(x - .5*delta_x,t - .5*delta_t) +
heat_gen(x + .5*delta_x,t - .5*delta_t))));
---------------

- In a loop based on the integral counter 'j' the time spent in this
calculation is an extremely expensive to achieve the desired
result:

for (j = 0; j < 2 * t; j++)
{
count = pow(-1, j) * j;

Consider replacing the calculation of 'count' with something like
this:
for (j = 0; j < 2 * t; j++)
{
count = (j & 1) ? j : -j;

Also note that you are at the mercy of the compiler's optimizer to
remove the multiplication from the end of loop test. It might be
better to declare a variable, perhaps 't_times_2', whose value was
assigned '2 * t' earlier and then use it in the loop statement,
something like
this:

for (j = 0; j < t_times_2; j++)

. Ed

>Mark Squires wrote in message
>news:41f901d1$ XXXX@XXXXX.COM ...
>
>I'll post the whole code. I'm pretty sure that I allocated the
>memory correctly, especially since I am able to run the
>executable on a macintosh. I'll increase the memory allocated
>anyway and see what happens.
>
>Anyway, here is the whole program.
>
>...
>#include <stdio.h>
>#include <math.h>
>#include <string.h>
>#include <stdlib.h>
>
>double heat_gen (double x, double t);
>
>#define FILENAME "output.dat"
>
>double density, c, k, tau;
>
>int main (void)
>
>{
>/*File Pointer*/
>FILE *out;
>/*Declare and initialize variables*/
>double *results;
>
>double x, x_new, width, delta_x, tmax, t_new, delta_t, time;
>double gen, temp, flux, bound_zero, bound_width, deviation;
>int i, j, n, t, count;
>
>tau = .000000001;
>
>printf("Enter Final Time and Delta t\n");
>scanf("%lf %lf", &tmax, &delta_t);
>
>printf("Enter Total Width and number of divisions (must be odd)\n");
>scanf("%lf %d", &width, &n);
>
>t = tmax/delta_t;
>delta_x = 2*width/n;
>n = .5*(n+1);
>
>printf("delta x is %lf\n\n", delta_x);
>printf("n is %d\n\n", n);
>
>/*Allocate Memory*/
>
>results = (double *)malloc(4*2*t*n*sizeof(double));
>
>/*Physical Constants*/
>
>printf("Enter Denstity (p) Thermal Conductivity (k) and Specific Heat
(c)\n");
>scanf("%lf %lf %lf", &density, &k, &c);
>
>/*Establish Initial Condition and Boundary Condition*/
>
>printf("Enter initial temperature\n");
>scanf("%lf", &temp);
>
>printf("Enter boundary temperature at x=0 and x=width\n");
>scanf("%lf %lf", &bound_zero, &bound_width);
>
>x_new = .5*delta_x;
>
>for (i = 0; i < 4*n; i += 4)
>{
>
>x = x_new;
>
>results[i] = time;
>results[i+1] = x;
>results[i+2] = temp;
>results[i+3] = 0;
>
>x_new = x + delta_x;
>
>}
>
>t_new = .5*delta_t;
>for (j = 0; j < 2*t; j ++)
>{
>time = t_new;
>count = pow(-1,j)*j;
>
>printf("%d\t%d", j, count);
>printf("\n");
>
>if (count == j)
>
>{
>x_new = 0;
>
>for (i = 4*n*(j+1); i < 4*n*(j+2); i += 4)
>{
>x = x_new;
>
>results[i] = time;
>results[i+1] = x;
>
>if (x == 0)
>
>{
>
>results[i+2] = bound_zero;
>
>results[i+3] = (1/(tau*delta_x +
.25*delta_t*delta_x))*(-k*.5*delta_t*(results[i+2-4*n]) + (tau*.5*delta_x -
.125*delta_t*delta_x)*(results[i+3-4*n]));
>}
>
>else
>
>{
>results[i+2] =
(1/(density*c*delta_x))*((density*.5*c*delta_x)*(results[i+2-4*n] +
results[i+2-4*(n+1)]) -
.5*delta_t*(results[i+3-4*n]-results[i+3-4*(n+1)])+.25*delta_t*delta_x*(heat
_gen(x,t) + .5*(heat_gen(x - .5*delta_x,t - .5*delta_t) + heat_gen(x +
.5*delta_x,t - .5*delta_t))));
>
>results[i+3] = (1/(tau*delta_x +
.25*delta_t*delta_x))*(-k*.5*delta_t*(results[i+2-4*n] -
results[i+2-4*(n+1)]) + (tau*.5*delta_x -
.125*delta_t*delta_x)*(results[i+3-4*n] + results[i+3-4*(n+1)]));
>}
>
>x_new = x + delta_x;
>
>}
>}
>
>else
>
>{
>x_new = .5*delta_x;
>
>for (i = 4*n*(j+1); i < 4*n*(j+2); i += 4)
>{
>x = x_new;
>deviation = ((x - width)/(width))*((x - width)/(width));
>
>results[i] = time;
>results[i+1] = x;
>
>if (deviation <= 0.001)
>
>{
>
>results[i+2] = bound_width;
>
>results[i+3] = (1/(tau*delta_x + .25*delta_t*delta_x))*(-k*.5*delta_t*(0 -
results[i+2-4*n]) + (tau*.5*delta_x -
.125*delta_t*delta_x)*(results[i+3-4*n]));
>}
>
>else
>
>{
>results[i+2] =
(1/(density*c*delta_x))*((density*.5*c*delta_x)*(results[i+2-4*(n-1)] +
results[i+2-4*(n)]) -
.5*delta_t*(results[i+3-4*(n-1)]-results[i+3-4*(n)])+.25*delta_t*delta_x*(he
at_gen(x,t) + .5*(heat_gen(x - .5*delta_x,t - .5*delta_t) + heat_gen(x +
.5*delta_x,t - .5*delta_t))));
>
>results[i+3] = (1/(tau*delta_x +
.25*delta_t*delta_x))*(-k*.5*delta_t*(results[i+2-4*(n-1)] -
results[i+2-4*(n)]) + (tau*.5*delta_x -
.125*delta_t*delta_x)*(results[i+3-4*(n-1)] + results[i+3-4*(n)]));
>}
>
>x_new = x + delta_x;
>
>}
>}
>
>t_new = time + .5*delta_t;
>
>}
>
>
>/* Print to file */
>
>out = fopen(FILENAME, "w");
>
>for (i = 0; i < 4*2*t*n; i += 4)
>
>{
>
>fprintf(out, "%lf\t", *(results + i));
>fprintf(out, "%lf\t", *(results + i + 1));
>fprintf(out, "%lf\t", *(results + i + 2));
>fprintf(out, "%lf\n", *(results + i + 3));
>
>}
>
>
>printf("\nResults found in output.dat.\n\n");
>fclose(out);
>
>printf("\n");
>
>
>
>/*print to screen*/
>
>for (i = 0; i < 4*2*t*n; i += 4)
>
>{
>
>printf("%lf\t", *(results + i));
>printf("%lf\t", *(results + i + 1));
>printf("%lf\t", *(results + i + 2));
>printf("%lf\n", *(results + i + 3));
>
>}
>
>printf("\n");
>
>return (0);
>
>}
>/*Function Subroutines*/
>
>double heat_gen (double x, double t)
>
>{
>double gen;
>
>/*Enter heat generation as f(x,t) below*/
>
>gen = .5*x*x*t;
>
>return (gen);
>
>}
>
>
>
>
>
>"Ed Mulroy [TeamB]" < XXXX@XXXXX.COM >wrote:
>>That code declares a pointer to double called 'results'. It writes the
>>values from results[0] through results[4*2*t*n - 1] to a file.
>>
>>The allocation of memory and assignment of that allocation address to the
>>variable 'results' is not shown. The declaration types and values of the
>>variables 't' and 'n' which are in control of loop termination are not
>>shown. The assignments of values to elements in the array 'results' are
not
>>shown.
>>
>>My guess is that the program goes beyond the allocated end of the array
>>'results'. However the missing code insures that this can only be a
guess.
>>
>>. Ed
>>
>>>Mark Squires wrote in message
>>>news:41f83693$ XXXX@XXXXX.COM ...
>>>
>>>I am using Borland C++ Compiler 5.5, the free compiler that they
>>>are now offering. Here is the piece of code that writes my
>>>file:
>>>
>>>#define FILENAME "output.dat"
>>>
>>>double density, c, k, tau;
>>>
>>>int main (void)
>>>
>>>{
>>>/*File Pointer*/
>>>FILE *out;
>>>/*Declare and initialize variables*/
>>>double *results;
>>>.... (other functions here)
>>>/* Print to file */
>>>
>>>out = fopen(FILENAME, "w");
>>>
>>>for (i = 0; i < 4*2*t*n; i += 4)
>>>{
>>>fprintf(out, "%lf\t", *(results + i));
>>>fprintf(out, "%lf\t", *(results + i + 1));
>>>fprintf(out, "%lf\t", *(results + i + 2));
>>>fprintf(out, "%lf\n", *(results + i + 3));
>>>}
>>>
>>>printf("\nResults found in output.dat.\n\n");
>>>fclose(out);
>>>printf("\n");
>>
>>
>>
>


 

Re:help with kernel32.dll errors during file output

Quote
... I needed to allocate more memory ...
...
... compiled and run on a UNIX machine ... and the
macintosh ... when I run them on Windows I have to
allocate more memory ...
When you make an allocation call it is common for the memory allocator to
anticipate the possiblity that more allocation calls will follow. Memory
allocation calls to an operating system are expensive in time and resources
so compilers commonly allocate large blocks from which they dole out
whatever pieces you request.
Also, different operating systems set up their detection mechanisms for
invalid memory access at different levels of coarseness, different sized
memory blocks.
Therefore the lack of a failure when writing past the end of an allocated
array does not guarantee that the access was legal. It also does not
guarantee that you would 'get away' with running such a program again
without complaint on the non-complaining machine.
The only way to guarantee no failure when accessing memory is to guarantee
that the address/index falls within memory that the program owns.
Quote
... run on a UNIX machine with no problem, (and the
macintosh as well), ...
Those two are equivalent. The OSX operating system on the Macintosh is
Unix. It is a grahical user interface mounted on the BSD version of Unix.
The processor hardware allows the operating system to set up trap points for
invalid access as multiples of a power of two value. In earlier versions of
Windows the coarseness of that detection was the same as that of Unix.
Later versions of Windows have decreased the size of those trap points and
Windows detects smaller incursions into illegal areas of memory.
Quote
... your suggestion for the 'count' sequence is able to
distinguish even and odd numbers. Is that right? I couldn't
find another way to do it other than to raise a -1 to a power. ...
0 0000
1 0001
2 0010
3 0011
4 0100
etc
In binary each bit counts as double the value of the one to its right. That
means all bits except the rightmost one are the result of multiplying by a
power of two so, except for the least significant or rightmost bit the
values represented by all bits are even.
The value of the number is the sum of the values of the bits. Therefore the
only non-power-of-two or only non-even part is the rightmost bit.
If the number is odd then the rightmost/least significant bit is one.
. Ed
Quote
Mark Squires wrote in message
news:41f9206b$ XXXX@XXXXX.COM ...

Well, I followed both of your suggestions. It turns out that
the file was indeed open, but that I needed to allocate more
memory as Ed suggested. Doing so solved my problem. I still
don't understand why though. I had a few other programs
that I had compiled and run on a UNIX machine with no problem,
(and the macintosh as well), yet when I run them on Windows I
have to allocate more memory or else I get the system failure?!?
I don't understand. Sorry to be such a dunderhead.

Thank you for your other suggestions. It seems that your
suggestion for the 'count' sequence is able to distinguish even
and odd numbers. Is that right? I couldn't find another way
to do it other than to raise a -1 to a power.
 

Re:help with kernel32.dll errors during file output

Mark Squires wrote:
Quote
Well, I followed both of your suggestions. It turns out that
the file was indeed open, but that I needed to allocate more
memory as Ed suggested. Doing so solved my problem. I still
You also never check the return value from the malloc for NULL.
Quote
don't understand why though. I had a few other programs
that I had compiled and run on a UNIX machine with no problem,
(and the macintosh as well), yet when I run them on Windows I
have to allocate more memory or else I get the system failure?!?
They might not be so picky about memory usage, or they might
overallocate for you, masking the problem.
Quote
I don't understand. Sorry to be such a dunderhead.

Thank you for your other suggestions. It seems that your
suggestion for the 'count' sequence is able to distinguish even
and odd numbers. Is that right? I couldn't find another way
to do it other than to raise a -1 to a power.
BOOL comes to mind.
bool plus = true;
for(whatever)
value = (bool=!bool)?secondtime:firsttime;
but Ed's is better.
Quote
>>int i, j, n, t, count;
>>results = (double *)malloc(4*2*t*n*sizeof(double));
//remark results[2t][n][4]
Quote
>>for (i = 0; i < 4*n; i += 4)
>>{
>>results[i] = time;
//remark results[ 0 ][ i ][ 0 ]
>>results[i+1] = x;
>>results[i+2] = temp;
>>results[i+3] = 0;
>>}
>>for (j = 0; j < 2*t; j ++)
>>{
>>count = pow(-1,j)*j;
>>if (count == j)
You mean if j is even/odd? You don't seem to use it elsewhere.
Might as well say
if( j & 1 )
Quote
>>{
>>for (i = 4*n*(j+1); i < 4*n*(j+2); i += 4)
>>{
>>results[i] = time;
//remark results[ j+1 ][ i ][ 0 ]
but, j goes to 2t, so it overflows the array.
if the j loop went by j+=2 instead of j++, it'd be ok
or malloc (2t+1)(4n)(double)
 

Re:help with kernel32.dll errors during file output

Mark Squires escreveu:
Quote
I'll post the whole code. I'm pretty sure that I allocated the
memory correctly, especially since I am able to run the
executable on a macintosh. I'll increase the memory allocated
anyway and see what happens.

Anyway, here is the whole program.

As you received a lot of suggestions, I would like to add another one
still not appearing in my news reader.
The comments come from Splint:
Quote


[snipped]
in function main:
Quote

if (x == 0)
Dangerous comparison involving double types: x == 0
Two real (float, double, or long double) values are compared directly
using a
C primitive. This may produce unexpected results since floating point
representations are inexact. Instead, compare the difference to
FLT_EPSILON
or DBL_EPSILON. (Use -realcompare to inhibit warning)
[snipped]
Quote

return (0);
Also, Splint complains about memory had not been freed here (you program
will do it automatically, but remember doing it in cases you're not exiting!