Error message on running the program

  • Thread starter Pratyush Srivastava
  • Start date
P

Pratyush Srivastava

Hi

I am using MSVC-7 to run a C program. The build was successful, but on
running the *.exe file of the program I got this error message -
<<< The instruction at "0x00401033" referenced memory at
"0x00000001". The memory could not be "written". >>>

Can anyone help ?


Thanks
 
O

Old Wolf

I am using MSVC-7 to run a C program. The build was successful, but on
running the *.exe file of the program I got this error message -
<<<  The instruction at "0x00401033" referenced memory at
"0x00000001". The memory could not be "written". >>>

You have a bug in your program
 
M

Morris Keesan

Hi

I am using MSVC-7 to run a C program. The build was successful, but on
running the *.exe file of the program I got this error message -
<<< The instruction at "0x00401033" referenced memory at
"0x00000001". The memory could not be "written". >>>

Can anyone help ?

Yes. You're missing a semicolon on line 15 of your program.
In other words, show us your code, and we can help a lot more.

You have a variable or expression which contains the number 1, and
you're trying to use that expression as if it were a pointer.
 
F

Flash Gordon

Morris said:
Yes. You're missing a semicolon on line 15 of your program.
In other words, show us your code, and we can help a lot more.

You have a variable or expression which contains the number 1, and
you're trying to use that expression as if it were a pointer.

Or an uninitialised pointer, or a buffer overflow, or...

Oh, and the problem is on line 42!
 
P

Pratyush Srivastava

Yes. You're missing a semicolon on line 15 of your program.
In other words, show us your code, and we can help a lot more.

You have a variable or expression which contains the number 1, and
you're trying to use that expression as if it were a pointer.

Here is the code -

#include <stdio.h>
#include "minto.h"

void
main ()
{
appl_mps();
minto ("CAPbdgt", "-x -o3 -h -c -i -k -g -f");

}

unsigned
appl_mps (id, vcnt, ccnt, nzcnt, vobj, vlb, vub, vtype, csense, crhs,
vfirst, vind, vcoef,
vstorsz, vstore, vname, cstorsz, cstore, cname)
int id; /* identification of active minto */
int *vcnt; /* number of variables */
int *ccnt; /* number of constraints */
int *nzcnt; /* number of nonzero’s */
double **vobj; /* objective coefficients of the variables */
double **vlb; /* lower bounds on the variables */
double **vub; /* upper bounds on the variables */
char **vtype; /* types of the variables, i.e., ’C’, ’B’, or
’I’ */
char **csense; /* senses of the constraints, i.e., ’L’, ’E’,
or ’G’ */
double **crhs; /* right hand sides of the constraints */
int **vfirst; /* starting positions of the columns of the
variables */
int **vind; /* indices of the nonzero coefficients */
double **vcoef; /* nonzero coefficients */
int *vstorsz; /* total number of characters in the names of
the variables */
char **vstore; /* names of the variables */
char ***vname; /* starting positions of the names of the
variables */
int *cstorsz; /* total number of characters in the names of
the constraints */
char **cstore; /* names of the constraints */
char ***cname; /* starting positions of the names of the
constraints */
{
int i, j;
double *_vobj;
double *_vlb;
double *_vub;
char *_vtype;
char *_csense;
double *_crhs;
int *_vfirst;
int *_vind;
double *_vcoef;
char *_vstore;
char **_vname;
char *_cstore;
char **_cname;
*vcnt = 10;
*ccnt = 10;
*nzcnt = 10;

_vobj = (double *) calloc (*vcnt, sizeof (double));
_vlb = (double *) calloc (*vcnt, sizeof (double));
_vub = (double *) calloc (*vcnt, sizeof (double));
_vtype = (char *) calloc (*vcnt, sizeof (char));
_csense = (char *) calloc (*ccnt, sizeof (char));
_crhs = (double *) calloc (*ccnt, sizeof (double));
_vfirst = (int *) calloc (*vcnt+1, sizeof (int));
_vind = (int *) calloc (*nzcnt, sizeof (int));
_vcoef = (double *) calloc (*nzcnt, sizeof (double));
for (_vfirst[0] = 0, j = 0; j < *vcnt; j++) {
_vobj[j] = 1.0;
_vlb[j] = 0.0;
_vub[j] = 1.0;
_vtype[j] = 'B';
_vfirst[j+1] = j+1;
_vind[j] = j;
_vcoef[j] = 1.0;
}
for (i = 0; i < *ccnt; i++) {
_csense = 'L';
_crhs = 1.0;
}
*vstorsz = *vcnt;
*cstorsz = *ccnt;
_vstore = (char *) calloc (*vstorsz, sizeof (char));
_cstore = (char *) calloc (*cstorsz, sizeof (char));
_vname = (char **) calloc (*vcnt, sizeof (char *));
_cname = (char **) calloc (*ccnt, sizeof (char *));
for (j = 0; j < *vcnt; j++) {
_vstore[j] = '\0';
_vname[j] = &(_vstore[j]);
}
for (i = 0; i < *ccnt; i++) {
_cstore = '\0';
_cname = &(_cstore);
}
*vobj = _vobj;
*vlb = _vlb;
*vub = _vub;
*vtype = _vtype;
*csense = _csense;
*crhs = _crhs;
*vfirst = _vfirst;
*vind = _vind;
*vcoef = _vcoef;
*vstore = _vstore;
*vname = _vname;
*cstore = _cstore;
*cname = _cname;
return (NO);
}
 
M

Morris Keesan

On Wed, 02 Sep 2009 11:54:52 -0400, Pratyush Srivastava

This code is horrible. I'm not even going to start listing the details of
everything
that's wrong with it. But here's your most fundamental problem, which is
almost
certainly the cause of your error message:

appl_mps();
Here you call appl_mps with no arguments, invoking undefined behavior,
because
....
here you declare appl_mps (in old pre-1989 style) as taking a huge
number of arguments.
unsigned
appl_mps (id, vcnt, ccnt, nzcnt, vobj, vlb, vub, vtype, csense, crhs,
vfirst, vind, vcoef,
vstorsz, vstore, vname, cstorsz, cstore, cname)
int id; /* identification of active minto */

_vobj = (double *) calloc (*vcnt, sizeof (double));

And here you attempt to dereference the pointer argument vcnt,
but in your main function you didn't pass any value for vcnt.
What's probably happening is that the system is picking up
whatever garbage value happened to be on the stack, and using
that as the value of vcnt. From your earlier-posted message,
this appears to be the number 1.

Note: among many other major errors in your code: calling
calloc to allocate doubles. calloc allocates space which is
set to all-bits-zero. This is not guaranteed to represent
any meaningful value for floating point numbers, and could be
Not A Number.

Further note: appl_mps is (in an obsolete fashion) declared to
take arguments most of which are pointers. When you attempt to
fix main to pass those arguments, make sure that you pass
pointers which have valid addresses in them. I.e. don't do
something like

main()
{
int *p;
appl_mps( 42, p, ...

or
appl_mps( 42, 1, ...

I recommend recoding appl_mps in prototype fashion, and placing it
(or a prototyped forward declaration of it) *before* main() in your
code, so that the compiler can complain about incorrect calls such
as the one you currently have.
 
K

Keith Thompson

Pratyush Srivastava said:
Here is the code -

#include <stdio.h>
#include "minto.h"

We don't know what's in this non-standard header.
void
main ()

This needs to be "int main(void)".
{
appl_mps();

Here you call appl_mps() with no arguments. In C99, this is illegal
(a constraint violation). In C90, it assumes that appl_mps has no
parameters and returns a result of type int.

You should have a visible prototype for every function you call.
minto ("CAPbdgt", "-x -o3 -h -c -i -k -g -f");

We have no idea what this is supposed to do. Presumably the function
is declared in "minto.h".
}

unsigned
appl_mps (id, vcnt, ccnt, nzcnt, vobj, vlb, vub, vtype, csense, crhs,
vfirst, vind, vcoef,
vstorsz, vstore, vname, cstorsz, cstore, cname)
int id; /* identification of active minto */
int *vcnt; /* number of variables */
int *ccnt; /* number of constraints */
int *nzcnt; /* number of nonzeroÂ’s */
double **vobj; /* objective coefficients of the variables */
double **vlb; /* lower bounds on the variables */
double **vub; /* upper bounds on the variables */
char **vtype; /* types of the variables, i.e., Â’CÂ’, Â’BÂ’, or
Â’IÂ’ */
char **csense; /* senses of the constraints, i.e., Â’LÂ’, Â’EÂ’,
or Â’GÂ’ */
double **crhs; /* right hand sides of the constraints */
int **vfirst; /* starting positions of the columns of the
variables */
int **vind; /* indices of the nonzero coefficients */
double **vcoef; /* nonzero coefficients */
int *vstorsz; /* total number of characters in the names of
the variables */
char **vstore; /* names of the variables */
char ***vname; /* starting positions of the names of the
variables */
int *cstorsz; /* total number of characters in the names of
the constraints */
char **cstore; /* names of the constraints */
char ***cname; /* starting positions of the names of the
constraints */
{ [snip]
}

You've defined appl_mps as a function returning an unsigned int result
requiring 19 (!) parameters of various types. Calling it with no
arguments is an error. Since you didn't provide a prototype, the
compiler can't detect this error, so the behavior is undefined.

You should always use prototypes. For appl_mps, either define it
before you call it, or add a separate declaration/prototype before you
call it. The prototype should look something like this:

unsigned appl_mps(int id, int *vcnt, int *ccnt, /* etc. */ )

The style you're using, where parameter types are declared between the
')' and the '{', is obsolete.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top