By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,053 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,053 IT Pros & Developers. It's quick & easy.

memory access failure

P: n/a
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>::append(int) (lib/list.h:107)
==7888== by 0x80488B7: main (list.cpp:28)
==7888== by 0x4030D8AD: __libc_start_main (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>::append(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_main (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>::append(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________
Speicherzugriffsfehler
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(elements, sizeof(T))) == NULL) {
printf("\n!!!Error!!!\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
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Thomas Baier wrote:
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>::append(int) (lib/list.h:107)
==7888== by 0x80488B7: main (list.cpp:28)
==7888== by 0x4030D8AD: __libc_start_main (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>::append(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_main (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>::append(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________
Speicherzugriffsfehler
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(elements, sizeof(T))) == NULL) {
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.
printf("\n!!!Error!!!\n");
//Doesn't appear anytime

}
elements[length++] = newElement;
}

The code of my little program is:
main() There is no implicit `int' in C++, so this should be:

int 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);
}
}

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'pasted); 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.

Jul 19 '05 #2

P: n/a
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 Initialisierungsvarianten 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(elements, sizeof(T))) == NULL) {
printf("\n!!!Error!!!\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(int 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(elements, sizeof(T))) == NULL) {
printf("\n!!!Error!!!\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
Jul 19 '05 #3

P: n/a
Thomas Baier wrote:
snip
I do not understand why

template <typename T> void List <T>::append(T newElement)
{
if((elements = (T*) realloc(elements, sizeof(T))) == NULL) {
printf("\n!!!Error!!!\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?

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
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.