473,686 Members | 2,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 19 '05 #1
3 2511
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>::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) {
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!!!Er ror!!!\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'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.

Jul 19 '05 #2
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
Jul 19 '05 #3
Thomas Baier wrote:
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?

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2080
by: kk | last post by:
Hi all, i didn't get output in the following code while compiling and executing with g++ version 3.2.3 it doesn't allocate memory to pointer varaible (x) in class B. and it gives correct output while executing in .Net and visual slick editor. why it didn't allocate memory? if anybody knows plz give reply. thanks in advance kk
16
2509
by: Jacob | last post by:
It is common practice (I've heard) to always catch allocation exceptions from "new". How is done in practice? What would be a common procedure (path of sequence) after such an event has occured? (For a multi-million LOC, GUI based application). It is also a common advice not to allocate objects with "new" unless absolutely required. The alternative is to keep objects on the stack, automatically allocated and deallocated by scope...
18
3698
by: jacob navia | last post by:
In C, we have read-only memory (const), read/write memory (normal data), and write only memory. Let's look at the third one in more detail. Write only memory is a piece of RAM that can only be written to, since its contents are undefined. The program is allocating a new piece of data, and the previous contents aren't relevant. This memory
66
3613
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if (function(socket, args) == -1) { perror("function"); exit(EXIT_FAILURE); } I feel that the ifs destroy the readability of my code. Would it be
20
2150
by: Udo A. Steinberg | last post by:
Hi all, In a ternary statement such as: x = (cond ? a : b); it is obviously guaranteed that "x" will be equal to "a" only if the condition "cond" holds. Assuming that "a" is a memory location, is it also guaranteed that "a" will not be accessed in memory if the condition does not hold? Or, in other words, is a compiler allowed to speculatively fetch a and b from memory and then assign one of them to x based on the condition?
17
9138
by: dtschoepe | last post by:
Hi, I have a homework project I am working on, so be forwarned, I'm new to C programming. But anyway, having some trouble with a memory allocation issue related to a char * that is a variable inside of a structure. I keep getting segmentation fault errors and I am having trouble understanding why. Here's the parts of the code in question... This is part of the .h file where the struct us defined...
0
1254
by: bob | last post by:
Hi, VS2005 Windows App. Datalayer talking to a local SQL Anywhere 9 Database using OleDb. App runs OK for about a week then crashes in the Datalayer. The crash occurs attempting to retrieve data via a SPROC. The reported error is 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt..' Also sometimes get: 'No error message available, result code: E_UNEXPECTED(0x8000FFFF)..'
6
3366
by: itsolution | last post by:
Hi folks, Could you shed some light on this issue? my program is running on Freebsd as a daemon. When user sends a request, it forks itself and lets its child process handles the request. And its main role is just to read a big xml file and save each object into its embedded DB(such as gdbm).
5
24722
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS (I am new to Windows so not sure). We are currently bouncing the instance to overcome this error. This generally happen at the end of business day only (So maybe memory might be getting used up?). We have already increased the statement heap & ...
0
8517
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9057
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8934
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8770
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8780
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7603
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6441
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4309
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2947
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.