memory access failure

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Baier

    memory access failure

    Hi there,

    I'v got a little program generating coincidental lists. It crashes with
    memory access failure, but not always at the same time.

    If I start the programm using valgrind it always crashes after sorting the
    first list with the following messages:

    thomas@linux:~/Info/C++> valgrind list
    /usr/bin/valgrind: line 106: exec: list: not found
    thomas@linux:~/Info/C++> valgrind ./list
    ==7888== Memcheck, a.k.a. Valgrind, a memory error detector for x86-linux.
    ==7888== Copyright (C) 2002, and GNU GPL'd, by Julian Seward.
    ==7888== Using valgrind-1.9.5pre, a program instrumentation system for
    x86-linux.
    ==7888== Copyright (C) 2000-2002, and GNU GPL'd, by Julian Seward.
    ==7888== Estimated CPU clock rate is 1809 MHz
    ==7888== For more details, rerun with: -v
    ==7888==
    liste...ok //Ausgabe
    des Programms
    ==7888== Invalid write of size 4
    ==7888== at 0x8048A28: List<int>::appe nd(int) (lib/list.h:107)
    ==7888== by 0x80488B7: main (list.cpp:28)
    ==7888== by 0x4030D8AD: __libc_start_ma in (in /lib/libc.so.6)
    ==7888== by 0x8048710: (within /home/thomas/Info/C++/list)
    ==7888== Address 0x4112F060 is 0 bytes after a block of size 4 alloc'd
    ==7888== at 0x40162F43: malloc (in /usr/lib/valgrind/valgrind.so)
    ==7888== by 0x40163448: realloc (in /usr/lib/valgrind/valgrind.so)
    ==7888== by 0x80489F5: List<int>::appe nd(int) (lib/list.h:103)
    ==7888== by 0x80488B7: main (list.cpp:28)
    Append...ok
    ==7888==
    ==7888== Invalid read of size 4
    ==7888== at 0x8048A66: List<int>::out( ) (lib/list.h:113)
    ==7888== by 0x80488E1: main (list.cpp:30)
    ==7888== by 0x4030D8AD: __libc_start_ma in (in /lib/libc.so.6)
    ==7888== by 0x8048710: (within /home/thomas/Info/C++/list)
    ==7888== Address 0x4112F060 is 0 bytes after a block of size 4 alloc'd
    ==7888== at 0x40162F43: malloc (in /usr/lib/valgrind/valgrind.so)
    ==7888== by 0x40163448: realloc (in /usr/lib/valgrind/valgrind.so)
    ==7888== by 0x80489F5: List<int>::appe nd(int) (lib/list.h:103)
    ==7888== by 0x80488B7: main (list.cpp:28)
    98 55 0 10 7 17 97 85 75 87 9 24 4 36 28 76 58 45 37 47 62 60 82 43 62 87 48
    51 56 50 34 55 7 34 63 12 49 61 95 26 50 6 48 52 42 74 30 98 21 67 46 81 27
    29 25 89 15 71 39 71 23 74 26 28 7 89 40 57 50 36 81 98 40 28 51 81 4 79 80
    23 48 26 5 75 53 28 63 69 1 4 39 22 76 65 48 83 54 86 39 3
    0________
    Speicherzugriff sfehler


    The methods of the list class is in short:

    List <T>::List()
    {
    elements = NULL; //Array for saving the list elements
    length = 0;

    }
    void List <T>::append(T newElement)
    {
    if((elements = (T*) realloc(element s, sizeof(T))) == NULL) {
    printf("\n!!!Er ror!!!\n");
    //Doesn't appear anytime

    }
    elements[length++] = newElement;
    }

    The code of my little program is:


    main()
    {
    srand((unsigned ) time(NULL));
    for(int i=0;i<100;i++) {
    cout << "liste...";
    List <int> *a = new List<int>;
    cout << "ok\n";
    cout << "Append..." ;
    for(int j=0;j<100;j++)
    a->append(rand()% 99);
    cout << "ok\n";
    a->out();
    cout << "\n" << i << "________\n ";
    delete(a);
    }
    }


    Thanks for any help


    Thomas


  • Artie Gold

    #2
    Re: memory access failure

    Thomas Baier wrote:[color=blue]
    > Hi there,
    >
    > I'v got a little program generating coincidental lists. It crashes with
    > memory access failure, but not always at the same time.
    >
    > If I start the programm using valgrind it always crashes after sorting the
    > first list with the following messages:
    >
    > thomas@linux:~/Info/C++> valgrind list
    > /usr/bin/valgrind: line 106: exec: list: not found
    > thomas@linux:~/Info/C++> valgrind ./list
    > ==7888== Memcheck, a.k.a. Valgrind, a memory error detector for x86-linux.
    > ==7888== Copyright (C) 2002, and GNU GPL'd, by Julian Seward.
    > ==7888== Using valgrind-1.9.5pre, a program instrumentation system for
    > x86-linux.
    > ==7888== Copyright (C) 2000-2002, and GNU GPL'd, by Julian Seward.
    > ==7888== Estimated CPU clock rate is 1809 MHz
    > ==7888== For more details, rerun with: -v
    > ==7888==
    > liste...ok //Ausgabe
    > des Programms
    > ==7888== Invalid write of size 4
    > ==7888== at 0x8048A28: List<int>::appe nd(int) (lib/list.h:107)
    > ==7888== by 0x80488B7: main (list.cpp:28)
    > ==7888== by 0x4030D8AD: __libc_start_ma in (in /lib/libc.so.6)
    > ==7888== by 0x8048710: (within /home/thomas/Info/C++/list)
    > ==7888== Address 0x4112F060 is 0 bytes after a block of size 4 alloc'd
    > ==7888== at 0x40162F43: malloc (in /usr/lib/valgrind/valgrind.so)
    > ==7888== by 0x40163448: realloc (in /usr/lib/valgrind/valgrind.so)
    > ==7888== by 0x80489F5: List<int>::appe nd(int) (lib/list.h:103)
    > ==7888== by 0x80488B7: main (list.cpp:28)
    > Append...ok
    > ==7888==
    > ==7888== Invalid read of size 4
    > ==7888== at 0x8048A66: List<int>::out( ) (lib/list.h:113)
    > ==7888== by 0x80488E1: main (list.cpp:30)
    > ==7888== by 0x4030D8AD: __libc_start_ma in (in /lib/libc.so.6)
    > ==7888== by 0x8048710: (within /home/thomas/Info/C++/list)
    > ==7888== Address 0x4112F060 is 0 bytes after a block of size 4 alloc'd
    > ==7888== at 0x40162F43: malloc (in /usr/lib/valgrind/valgrind.so)
    > ==7888== by 0x40163448: realloc (in /usr/lib/valgrind/valgrind.so)
    > ==7888== by 0x80489F5: List<int>::appe nd(int) (lib/list.h:103)
    > ==7888== by 0x80488B7: main (list.cpp:28)
    > 98 55 0 10 7 17 97 85 75 87 9 24 4 36 28 76 58 45 37 47 62 60 82 43 62 87 48
    > 51 56 50 34 55 7 34 63 12 49 61 95 26 50 6 48 52 42 74 30 98 21 67 46 81 27
    > 29 25 89 15 71 39 71 23 74 26 28 7 89 40 57 50 36 81 98 40 28 51 81 4 79 80
    > 23 48 26 5 75 53 28 63 69 1 4 39 22 76 65 48 83 54 86 39 3
    > 0________
    > Speicherzugriff sfehler
    >
    >
    > The methods of the list class is in short:
    >
    > List <T>::List()
    > {
    > elements = NULL; //Array for saving the list elements
    > length = 0;
    >
    > }
    > void List <T>::append(T newElement)
    > {
    > if((elements = (T*) realloc(element s, sizeof(T))) == NULL) {[/color]

    This is not what you want to do.

    First of all, if realloc() returns NULL, you have a memory leak;
    you've lost the original value of `elements'.

    More to the point, though, is the fact that you've reallocated only
    enough memory for one object of type `T' -- not what you want at all
    -- hence the memory error.
    [color=blue]
    > printf("\n!!!Er ror!!!\n");
    > //Doesn't appear anytime
    >
    > }
    > elements[length++] = newElement;
    > }
    >
    > The code of my little program is:
    >
    >
    > main()[/color]
    There is no implicit `int' in C++, so this should be:

    int main()
    [color=blue]
    > {
    > srand((unsigned ) time(NULL));
    > for(int i=0;i<100;i++) {
    > cout << "liste...";
    > List <int> *a = new List<int>;
    > cout << "ok\n";
    > cout << "Append..." ;
    > for(int j=0;j<100;j++)
    > a->append(rand()% 99);
    > cout << "ok\n";
    > a->out();
    > cout << "\n" << i << "________\n ";
    > delete(a);
    > }
    > }
    >
    >[/color]
    You're also missing headers and there are neamespace problems, but I
    won't get into that here.

    In general, it would be much better if you included *real*,
    *compilable* code (copied'n'paste d); it would make it easier to
    correct whatever other problems you've got.

    HTH,
    --ag




    --
    Artie Gold -- Austin, Texas
    Oh, for the good old days of regular old SPAM.

    Comment

    • Thomas Baier

      #3
      Re: memory access failure - add

      So here is the compilable code (shorter version):

      list.h:

      template <typename T> class List
      {
      private:
      int length;

      protected:
      T* elements;

      public:
      List();

      /************
      * Methoden: *
      ************/

      void append(T newelement);
      void exchange(int first, int second); //Vertauschen
      void out(); //Liste ausgeben
      int getLength();

      };


      /*************** *************** *************** ***********
      * Konstruktoren: *
      * *
      * mehrere Initialisierung svarianten durch Überladen *
      *************** *************** *************** ***********/

      template <typename T> List <T>::List()
      {
      elements = NULL;
      length = 0;

      }


      /*************** *********
      * Methoden: *
      *************** *********/


      template <typename T> void List <T>::append(T newElement)
      {
      if((elements = (T*) realloc(element s, sizeof(T))) == NULL) {
      printf("\n!!!Er ror!!!\n");
      exit();

      }
      elements[length++] = newElement;
      }

      template <typename T> void List <T>::out()
      {
      for(int i=0; i<length; i++) {
      cout << elements[i] << " ";
      }

      }

      template <typename T> void List <T>::exchange(i nt first, int second)
      {
      int temp = elements[first];
      elements[first] = elements[second];
      elements[second] = temp;
      }

      template <typename T> int List <T>::getLength( )
      {
      return length;
      }


      _______________ _______________ _______________ _______________ _____________



      list.cpp



      #include "./lib/list.h"
      #include <sys/time.h>
      #include <stdlib.h>


      main()
      {
      srand((unsigned ) time(NULL));

      for(int i=0;i<100;i++) {
      List <int> *a = new List<int>;
      for(int j=0;j<100;j++)
      a->append(rand()% 99);
      }
      }

      _______________ _______________ _______________ _______________ _______________


      I do not understand why

      template <typename T> void List <T>::append(T newElement)
      {
      if((elements = (T*) realloc(element s, sizeof(T))) == NULL) {
      printf("\n!!!Er ror!!!\n");
      exit();

      }
      elements[length++] = newElement;
      }

      should not do what I want. I thought it reallocates memory and returns
      "Error" if impossible. If possible the new element is appended to the list.
      Where's my mistake?

      Thomas

      Comment

      • James Lothian

        #4
        Re: memory access failure - add

        Thomas Baier wrote:[color=blue]
        > snip
        > I do not understand why
        >
        > template <typename T> void List <T>::append(T newElement)
        > {
        > if((elements = (T*) realloc(element s, sizeof(T))) == NULL) {
        > printf("\n!!!Er ror!!!\n");
        > exit();
        >
        > }
        > elements[length++] = newElement;
        > }
        >
        > should not do what I want. I thought it reallocates memory and returns
        > "Error" if impossible. If possible the new element is appended to the list.
        > Where's my mistake?
        >[/color]
        Read the previous reply. You're calling realloc with the wrong arguments
        -- you're only allocating enough memory for one element *in total*,
        rather
        than the current size *plus one*.

        James

        Comment

        Working...