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 3 2523
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.
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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...
|
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
|
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
|
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?
| |
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...
|
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)..'
|
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).
|
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 &
...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |