Menu

Memory error

Wridmuld
2008-08-14
2012-09-26
  • Wridmuld

    Wridmuld - 2008-08-14

    Hi.
    I have compiled and run the code below, but once I run it, it generates an error:

    The instruction at "0x77c1d4ba" referenced memory at "0x00000000". The memory could not be "read".

    It seems it is referencing memory in a way I don't understand.
    Does anybody understand why this is? There are no compile errors, and I have not knowledge enough to find the problem.

    Can anybody help me with this?
    Any help would be greatly appreciated.

    Here is the code:

    include <iomanip>

    include <complex>

    include <strstream>

    include <stdlib.h>

    include <math.h>

    typedef std::complex<double> cplx;

    void plot_init(void);
    void plot_end(void);
    void plot(cplx);
    cplx crand(void);
    int brand(void);

    const int STEPS = 20000,SCALE = 100,ORIGIN_X = 300,ORIGIN_Y = 600;
    const double POINT_SIZE = 0.5;

    int main(int argc,char *argv[])
    {

    cplx c(strtod(argv[1],0),strtod(argv[2],0)),z = crand();
    plot_init();
    for (int i = 0; i &lt; STEPS; i++)
    {
        z = sqrt(z-c);
        if (brand())
        z = -z;
        if (i &gt; 50)
        plot(z);
    }
    plot_end();
    

    }

    cplx crand(void)
    {
    return cplx(((double)rand())/RAND_MAX,((double)rand())/RAND_MAX);
    }

    int brand(void)
    {
    return rand() % 2;
    }

    void plot_init(void)
    {
    cout << "%!PS" << endl << "/p { " << POINT_SIZE << " 0 360 arc fill } def" << endl;
    cout << ORIGIN_X << " " << ORIGIN_Y << " translate" << endl;
    }

    void plot_end(void)
    {
    cout << "showpage" << endl;
    }

    void plot(cplx z)
    {
    z *= SCALE;
    cout << real(z) << " " << imag(z) << " p" << endl;
    }

     
    • cpns

      cpns - 2008-08-14

      Another perhaps trivial point, in C++ rather than:

      > void plot_end(void)

      you need only write:

      void plot_end()

      In C an empty parameter list menas that the function may take any parameters, so you specify void to explicitly state that no parameters are allowed. In C++ however, an empty parameter list is implicitly void, so generally it is omitted. A minor point, but it does remove some clutter from the code.

      Also the following:

      include <iomanip>

      include <strstream>

      include <stdlib.h>

      Are entirely redundant in this code. You use nothing that they declare. However you might want teh last one so that you can put:

      system("pause") ;

      at the end of main() if you actually want to see the output before the console window is closed.

      Clifford

       
    • Wridmuld

      Wridmuld - 2008-08-14

      Sorry - forgot to include the compile log.
      Here it comes:

      Building resource file...
      Linking files :
      C:\DEV-C_~1\Bin\g++ "c:\dev-c++\projects\fractalplot2\untitled1.o" -o c:\dev-c_~1\projects\fracta~2\Fracta~1.exe C:\DEV-C_~1\projects\FRACTA~2\rsrc.o -s -IC:\DEV-C_~1\Include\ -IC:\DEV-C_~1\Include\G__~1 -IC:\DEV-C_~1\Include\ -LC:\DEV-C_~1\Lib\ -BC:\DEV-C_~1\Bin\

       
    • cpns

      cpns - 2008-08-14

      That is not a log for a complete build. What version of Dev-C++ are you using, I don;t think that code would compile in 4.9.9.2 (using MinGW/GCC 3.4). In VC++ I get the following errors:

      e:\projects\testcode\sandbox\main.cpp(46) : error C2065: 'cout' : undeclared identifier
      e:\projects\testcode\sandbox\main.cpp(46) : error C2065: 'endl' : undeclared identifier
      e:\projects\testcode\sandbox\main.cpp(46) : error C2065: 'endl' : undeclared identifier
      e:\projects\testcode\sandbox\main.cpp(47) : error C2065: 'cout' : undeclared identifier
      e:\projects\testcode\sandbox\main.cpp(47) : error C2065: 'endl' : undeclared identifier
      e:\projects\testcode\sandbox\main.cpp(52) : error C2065: 'cout' : undeclared identifier
      e:\projects\testcode\sandbox\main.cpp(52) : error C2065: 'endl' : undeclared identifier
      e:\projects\testcode\sandbox\main.cpp(58) : error C2065: 'cout' : undeclared identifier
      e:\projects\testcode\sandbox\main.cpp(58) : error C2065: 'endl' : undeclared identifier

      These symbols are in the std:: namespace. Only very old versions of GCC place them in the global namespace. But you have not included <iostream> either so I am astounded that it compiles at all!

      The code needs:

      include <iostream>

      using std::cout ;
      using std::endl ;

      just to compile and link in an ISO compliant C++ compiler.

      When I ran it in VC++, it failed on the first line:

      > cplx c(strtod(argv[1],0),strtod(argv[2],0)),z = crand()

      The code requires command line parameters. What should they be? It took a while for me to see the wood for the trees here, but that line is in fact two declarations:

      cplx c(strtod(argv[1],0),strtod(argv[2],0)) ;
      cplx z = crand();
      

      and it is the first of these that is failing in my case. It is never a good idea to string declarations in one statement, especially when they are this complex - it does not aid readability, and the debugger is line oriented oriented, so it is not easy to tell which declaration failed.

      Anyhow I set the command line arguments arbitrarily to "1.0 2.0". and the code ran without problems.

      My guess is that you did not set the command line arguments, which seems odd if you wrote the code you would know that you needed to supply them. If you did not write teh code, you do need to understand its requirements before you try to use it. Note that a simple test for (argc != 3) could trap this error. You would have got the fault when you referenced an argv element that did not exist.

      If you did supply them, please tell us what they are so that we can at least reproduce your fault.

      Clifford

       
    • cpns

      cpns - 2008-08-14

      > That is not a log for a complete build.

      Sorry, I meant to expand on that point. The log only shows the linker stage, because the object file is up-to-date. To show a full log, you need to do a rebuild-all.

      More interesting perhaps is your log's use of MS-DOS compatible folder names. Why is it doing that I wonder.

      Also there is a bug in Dev-C++ that means that projects in c:\dev-cpp\ or sub folders of that can fail to build for no apparent reason. I would move your projects file to c:\ if I were you.` When teh problem occurs, that seems to solve it. Weird but true!

      Clifford

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.