Bug with access violation :(

Discussion in 'C++' started by Alex, Sep 13, 2005.

  1. Alex

    Alex Guest

    Hello

    Im working on project for my college, nevermind (but it urgent :((()...
    So I have this code example (in VS 6.0) in main class :



    REAL *Input[NUM_DATA];

    ....

    size[br] = r.getSize();

    Input[br] = new double [size[br]];

    for (i=0; i<size[br]/12; i++) {
    for (j =0; j< 12; j++){
    Input[br][i*12+j]=r.vector[j];

    }

    ....
    delete[] r.values;

    //
    size iz a static field witch contains a number of data in object r. Br
    is variable which values go from 0 to NUM_VALUES.

    when I execute my program, it crashes. I have tried to put breakpoints
    and Debug it and Ive found out that it crashes in line

    Input[br][i*12+j]=r.vector[j];

    when br comes to 3. Debug message is:

    Loaded 'ntdll.dll', no matching symbolic information found.
    Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic
    information found.
    Loaded 'C:\WINDOWS\system32\shimeng.dll', no matching symbolic
    information found.
    First-chance exception in NN.exe (NTDLL.DLL): 0xC0000005: Access
    Violation.
    First-chance exception in NN.exe: 0xC0000005: Access Violation.


    How can I fix this? I dont know if it matters, but size[br] values are
    very high / around 8500, so "new double[size[br]]" will have to
    allocate pretty much space in memory. Is there any limit for allocation
    of dynamic fields?
    (I doubt in line "Input[br] = new double [size[br]];" when br comes to
    3, when no allocation is possible, so it crashes in line I mentioned-
    when it wanted to put value from r into "nothing"
    But before I change whole code, I wanted to ask if Im wrong..pls answer
    me soon).

    Thank you a lot.

    Aleksandra
    Alex, Sep 13, 2005
    #1
    1. Advertising

  2. * Alex:
    >
    > Im working on project for my college, nevermind (but it urgent :((()...
    > So I have this code example (in VS 6.0) in main class :


    Consider upgrading to a more modern compiler, it will save you much work; you
    can download version 7.1 for free from Microsoft (Visual C++ Toolkit 2003).


    > REAL *Input[NUM_DATA];


    In C++, preferentially use symbolic constants, not macros.

    Also, consider using standard library classes, like

    std::vector< std::vector<double> > input( numData );

    ...

    assert( br < numData );
    int const size = r.getSize();
    input.at( br ).resize( size );
    for( int i = 0; i < size/12; ++i )
    {
    for( int j = 0; j < 12; ++j )
    {
    int const index = 12*i + j;
    input.at( br ).at( index ) = r.vector.at( j ); // ?
    }
    }

    instead of raw arrays and pointers and non-checking indexing and other
    low-level stuff like that.

    Using the above approach you'll get an error as soon as something is wrong,
    which makes it much easier to figure out the cause.


    > ...
    >
    > size[br] = r.getSize();
    >
    > Input[br] = new double [size[br]];
    >
    > for (i=0; i<size[br]/12; i++) {
    > for (j =0; j< 12; j++){
    > Input[br][i*12+j]=r.vector[j];
    >
    > }
    >
    > ...
    > delete[] r.values;


    > //
    > size iz a static field witch contains a number of data in object r.


    That may be part of your problem: unless there is ever only one object of that
    class, or all co-existing objects are the same size, a static field (there is
    only one shared one) is bound to have the wrong size for some such object.


    > Br
    > is variable which values go from 0 to NUM_VALUES.


    br should never be equal to NUM_DATA, always less; is that what "to" means?

    Have you confused two symbols NUM_DATA and NUM_VALUES?


    > when I execute my program, it crashes. I have tried to put breakpoints
    > and Debug it and Ive found out that it crashes in line


    > Input[br][i*12+j]=r.vector[j];
    >
    > ...
    > How can I fix this? I dont know if it matters, but size[br] values are
    > very high / around 8500, so "new double[size[br]]" will have to
    > allocate pretty much space in memory. Is there any limit for allocation
    > of dynamic fields?


    Not that low, no.


    > (I doubt in line "Input[br] = new double [size[br]];" when br comes to
    > 3, when no allocation is possible, so it crashes in line I mentioned-
    > when it wanted to put value from r into "nothing"
    > But before I change whole code, I wanted to ask if Im wrong..pls answer
    > me soon).


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Sep 13, 2005
    #2
    1. Advertising

  3. Alex

    Jim Langston Guest

    "Alex" <> wrote in message
    news:...
    > Hello
    >
    > Im working on project for my college, nevermind (but it urgent :((()...
    > So I have this code example (in VS 6.0) in main class :
    >
    >
    >
    > REAL *Input[NUM_DATA];
    >
    > ...
    >
    > size[br] = r.getSize();
    >
    > Input[br] = new double [size[br]];
    >
    > for (i=0; i<size[br]/12; i++) {
    > for (j =0; j< 12; j++){
    > Input[br][i*12+j]=r.vector[j];
    >
    > }
    >
    > ...
    > delete[] r.values;


    Let me see if I'm following this right.

    REAL *Input[NUM_DATA]; // Array of pointers NUM_DATA size
    Input[br] = new double [size[br]]; // Array of doubles size[br] size

    for ( i = 0; i < size[br]/12; i++ } // 0 to size[br] / 12 (-1)
    for (j = 0; j < 12; j++ ) // 0 to 11
    Input[br][i*12+j] = blah... / i*12 + j biggest being (size[br]-1) * 11
    + 11;

    Now. Are you positive that (size[br]-1) * 11 + 11 is smaller that NUM_DATA
    ?

    > //
    > size iz a static field witch contains a number of data in object r. Br
    > is variable which values go from 0 to NUM_VALUES.
    >
    > when I execute my program, it crashes. I have tried to put breakpoints
    > and Debug it and Ive found out that it crashes in line
    >
    > Input[br][i*12+j]=r.vector[j];
    >
    > when br comes to 3. Debug message is:


    when br comes to 3... I don't see you increasing br in the above code. You
    left it out.
    but it seems fairly obvious that [size[br]] > NUM_DATA
    >
    > Loaded 'ntdll.dll', no matching symbolic information found.
    > Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic
    > information found.
    > Loaded 'C:\WINDOWS\system32\shimeng.dll', no matching symbolic
    > information found.
    > First-chance exception in NN.exe (NTDLL.DLL): 0xC0000005: Access
    > Violation.
    > First-chance exception in NN.exe: 0xC0000005: Access Violation.


    Access violation generally means you're trying to access memory that doesn't
    "belong" to you.
    >
    >
    > How can I fix this? I dont know if it matters, but size[br] values are
    > very high / around 8500, so "new double[size[br]]" will have to
    > allocate pretty much space in memory. Is there any limit for allocation
    > of dynamic fields?
    > (I doubt in line "Input[br] = new double [size[br]];" when br comes to
    > 3, when no allocation is possible, so it crashes in line I mentioned-
    > when it wanted to put value from r into "nothing"
    > But before I change whole code, I wanted to ask if Im wrong..pls answer
    > me soon).
    >
    > Thank you a lot.
    >
    > Aleksandra
    >
    Jim Langston, Sep 13, 2005
    #3
  4. Alex

    Old Wolf Guest

    Alex wrote:
    >
    > Im working on project for my college, nevermind (but it urgent :((()...
    > So I have this code example (in VS 6.0) in main class :
    >
    > REAL *Input[NUM_DATA];
    > size[br] = r.getSize();
    > Input[br] = new double [size[br]];
    > for (i=0; i<size[br]/12; i++) {
    > for (j =0; j< 12; j++){
    > Input[br][i*12+j]=r.vector[j];
    > }
    > delete[] r.values;
    >
    > when I execute my program, it crashes. I have tried to put
    > breakpoints and Debug it and Ive found out that it crashes in line
    >
    > Input[br][i*12+j]=r.vector[j];


    Please post code that compiles. You haven't given enough
    information for people to make an informed guess at what's
    going wrong.
    Old Wolf, Sep 13, 2005
    #4
  5. Jim Langston wrote:
    > REAL *Input[NUM_DATA]; // Array of pointers NUM_DATA size
    > Input[br] = new double [size[br]]; // Array of doubles size[br] size
    >
    > for ( i = 0; i < size[br]/12; i++ } // 0 to size[br] / 12 (-1)
    > for (j = 0; j < 12; j++ ) // 0 to 11
    > Input[br][i*12+j] = blah... / i*12 + j biggest being (size[br]-1) * 11
    > + 11;


    (size[br]/12-1)*12 + 11 == size[br]-1 ?

    >
    > Now. Are you positive that (size[br]-1) * 11 + 11 is smaller that NUM_DATA
    > ?
    > [...]


    to OP: show complete code.
    Aleksey Loginov, Sep 14, 2005
    #5
  6. Alex

    Jim Langston Guest

    "Aleksey Loginov" <> wrote in message
    news:...
    >
    > Jim Langston wrote:
    >> REAL *Input[NUM_DATA]; // Array of pointers NUM_DATA size
    >> Input[br] = new double [size[br]]; // Array of doubles size[br] size
    >>
    >> for ( i = 0; i < size[br]/12; i++ } // 0 to size[br] / 12 (-1)
    >> for (j = 0; j < 12; j++ ) // 0 to 11
    >> Input[br][i*12+j] = blah... / i*12 + j biggest being (size[br]-1) *
    >> 11
    >> + 11;

    >
    > (size[br]/12-1)*12 + 11 == size[br]-1 ?


    Umm... let's see...

    for ( i = 0; i < size[br]/12; i++ } // 0 to size[br] / 12 (-1)
    for (j = 0; j < 12; j++ ) // 0 to 11
    Input[br][i*12+j] = blah... / i*12 + j biggest being (size[br]-1) * 11
    + 11;

    ((size[br] / 12) - 1) * 12 + 11;

    Hmm.. yeah, that doesn't resolve down to (size[br]-1) * 11 + 11 by any
    means.

    Gah.

    That's why it's best not to do this type of stuff this way.

    My old algebra teacher would kill me.
    Jim Langston, Sep 14, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news
    Replies:
    0
    Views:
    420
  2. F. Prefect
    Replies:
    5
    Views:
    1,034
    Steven Cheng[MSFT]
    Oct 10, 2004
  3. Bengt Richter

    time.mktime memory access violation bug

    Bengt Richter, Nov 18, 2003, in forum: Python
    Replies:
    6
    Views:
    682
    Bengt Richter
    Nov 21, 2003
  4. rakaur

    [BUG] cross-thread violation

    rakaur, Feb 16, 2006, in forum: Ruby
    Replies:
    4
    Views:
    207
    rakaur
    Feb 17, 2006
  5. Tim Hunter
    Replies:
    1
    Views:
    159
    Eric Hodel
    Jun 19, 2009
Loading...

Share This Page