473,406 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Strange Output, Help.

I know that this problem is concerned to GCC. But I hope somebody here
can tell me some detailed things about why.
The version of g++ I'm using is: g++ (GCC) 3.4.4 (mingw special).

The code is:
// BEGIN
#include<iostream>

int main(void)
{
int *pI1 = new int;
int *pI2 = new int;
int *pI3 = new int;
int *pI4 = new int;
int *pI5 = new int;
int *pI6 = new int;
int *pI7 = new int;

*pI1 = 43;
*pI2 = 56;
*pI3 = 78;
*pI4 = 98;
*pI5 = 99;
*pI6 = 123;
*pI7 = 123;

std::cout << *pI1 << std::endl;
std::cout << *pI2 << std::endl;
std::cout << *pI3 << std::endl;
std::cout << *pI4 << std::endl;
std::cout << *pI5 << std::endl;
std::cout << *pI6 << std::endl;
std::cout << *pI7 << std::endl;

delete pI1;
delete pI2;
delete pI3;
delete pI4;
delete pI5;
delete pI6;
delete pI7;

std::cout << *pI1 << std::endl;
std::cout << *pI2 << std::endl;
std::cout << *pI3 << std::endl;
std::cout << *pI4 << std::endl;
std::cout << *pI5 << std::endl;
std::cout << *pI6 << std::endl;
std::cout << *pI7 << std::endl;

return 0;
} // END

The output is:
43
56
78
98
99
123
123
0
4013536
4013640
3998472
123
123

Dec 30 '05 #1
10 1737
After being deleted, the first value(*pI1) is ok. But the others
puzzled me a lot.
I've also compiled it under VC6.0. The result is "right".
I'm sure this is compiler specific. I just want to know how g++ in GCC
treat this.
I also tried:
// BEGIN
char *str = new char[0];
str = "gaga";
std::cout << *str << std::endl
<< str << std::endl;
delete [] str;
std::cout << *str << std::endl
<< str << std::endl;
// END
The output is puzzled too!

Dec 30 '05 #2
On Thu, 29 Dec 2005 17:46:52 -0800, weichaoliu wrote:
I know that this problem is concerned to GCC. But I hope somebody here
can tell me some detailed things about why.
The version of g++ I'm using is: g++ (GCC) 3.4.4 (mingw special).


<snip offending code>

The problem isn't specific to GCC. The problem is in the code; it is
dereferencing pointers after they've been deleted. This is Undefined
Behavior, and anything can happen as a result (including "expected"
results).

The fix is Don't Do That.

- Jay

Dec 30 '05 #3
On Thu, 29 Dec 2005 17:55:45 -0800, weichaoliu wrote:
After being deleted, the first value(*pI1) is ok. But the others
puzzled me a lot.
I've also compiled it under VC6.0. The result is "right".
I'm sure this is compiler specific. I just want to know how g++ in GCC
treat this.
I also tried:
// BEGIN
char *str = new char[0];
str = "gaga";
std::cout << *str << std::endl
<< str << std::endl;
delete [] str;
std::cout << *str << std::endl
<< str << std::endl;
// END
The output is puzzled too!


Once you return memory to the heap, you no longer own it. The memory
allocation routines can do whatever they want with it.

Again, the behavior is not defined per the standard, and no good can come
of doing it.

Just say no. :)

- Jay

Dec 30 '05 #4
On Fri, 30 Dec 2005 02:04:47 +0000, Jay Nabonne wrote:
On Thu, 29 Dec 2005 17:55:45 -0800, weichaoliu wrote:
After being deleted, the first value(*pI1) is ok. But the others
puzzled me a lot.
I've also compiled it under VC6.0. The result is "right".
I'm sure this is compiler specific. I just want to know how g++ in GCC
treat this.
I also tried:
// BEGIN
char *str = new char[0];
str = "gaga";
std::cout << *str << std::endl
<< str << std::endl;
delete [] str;
std::cout << *str << std::endl
<< str << std::endl;
// END
The output is puzzled too!


Once you return memory to the heap, you no longer own it. The memory
allocation routines can do whatever they want with it.

Again, the behavior is not defined per the standard, and no good can come
of doing it.

Just say no. :)


Actually, consider this an alternate response to your first code.

This code exhibits a different problem. You're attempting to delete a
pointer you didn't allocate (you assign a different, non-allocated pointer
to "str"). That is yet another kind of undefined behavior. You're lucky it
didn't crash.

- Jay

Dec 30 '05 #5
On 29 Dec 2005 17:46:52 -0800 in comp.lang.c++, we********@gmail.com
wrote,
delete pI1; std::cout << *pI1 << std::endl;


After 'delete' the pointer is invalid! Do not try to use it!!

Dec 30 '05 #6
OKay, UNDEFINED BEHAVIOR.
So, I'm wondering about how GCC treats this kind of UNDEFINED BEHAVIOR.
I really do not like the answer ---- UNDEFINED BEHAVIOR. Can it solve
the problem? Or can it tell me the truth?
I'd always like to program under ISO/ANSI standards. Though ISO/ANSI
standards is perfect. I'm not working under such rules like ISO/ANSI
standards. I'm working in real world. ISO/ANSI standards are fantastic
on some aspects. ISO/ANSI standards are not everything! ISO/ANSI
standards come from real world!

If anybody wants to tell me that it's all about STANDARDS. Okay, I
appreciate your help.

But what I really want to know is what happened inside. Anybody know
that please tell me.

Thanks in advance!

/*
Becker
*/

Dec 30 '05 #7
we********@gmail.com wrote:
OKay, UNDEFINED BEHAVIOR.
So, I'm wondering about how GCC treats this kind of UNDEFINED BEHAVIOR.
I really do not like the answer ---- UNDEFINED BEHAVIOR. Can it solve
the problem? Or can it tell me the truth?
I'd always like to program under ISO/ANSI standards. Though ISO/ANSI
standards is perfect. I'm not working under such rules like ISO/ANSI
standards. I'm working in real world. ISO/ANSI standards are fantastic
on some aspects. ISO/ANSI standards are not everything! ISO/ANSI
standards come from real world!

If anybody wants to tell me that it's all about STANDARDS. Okay, I
appreciate your help.

But what I really want to know is what happened inside. Anybody know
that please tell me.


I'm guessing that once the allocated memory had been freed, the OS or
compiler-runtime then stored some information about the freed block
within the block (such as its size, or a pointer to the next free block
or whatever) which would then account for the changed contents at that
memory location.

But as others have pointed out, it does not really matter why the
memory contents may or may not have changed since the result of
executing this program does not fall within the behavior defined by the
standard. Therefore the observed behavior in this case cannot be relied
on, may not in fact be repeatable, in short - it's useless and
uninteresting.

Greg

Dec 30 '05 #8
we********@gmail.com wrote:
OKay, UNDEFINED BEHAVIOR.
So, I'm wondering about how GCC treats this kind of UNDEFINED BEHAVIOR.
I really do not like the answer ---- UNDEFINED BEHAVIOR. Can it solve
the problem? Or can it tell me the truth?
I'd always like to program under ISO/ANSI standards. Though ISO/ANSI
standards is perfect. I'm not working under such rules like ISO/ANSI
standards. I'm working in real world. ISO/ANSI standards are fantastic
on some aspects. ISO/ANSI standards are not everything! ISO/ANSI
standards come from real world!
Um... oh my god. Okay, listen carefully. In the Real World(tm), it is
a Very Bad Idea to attempt to use memory after you just deallocated it.
It's called undefined behavior because as soon as you delete the
memory, you are releasing your claim on it, and therefore can no longer
count on the guarantees that come with allocating it. "Undefined
behavior" is essentially a synonym for "random behavior," and writing
programs which behave randomly, especially with regard to reading and
writing memory, is neither safe nor interesting. That's why we avoid
it.

So, I don't know what you are referring to as "the problem" which you
would like solved. In this case, PEBKAC -- Problem Exists Between
Keyboard And Chair. That is to say, you shouldn't oughtta write code
like that, because it's meaningless and dangerous. The resolution to
the problem is not to delete the memory if you're still interested in
using it.
But what I really want to know is what happened inside. Anybody know
that please tell me.


A healthy curiosity is all well and good, but you're not going to get
particularly satisfying answers. The output you see when you read from
deallocated memory is unpredictable, and depends on the vagaries of the
runtime memory allocation system. Sometimes, it will just happen to
leave deallocated memory untouched for a while, which is the most
unfortunate circumstance because that makes it harder to track down the
problem. I suppose you could devise some kind of experiment to try and
learn about the behavior of the memory manager by trial and error,
but... why? Sure, it's neat. If you're really interested... it's open
source, so go read about how it works directly. My advice, though...
wait a bit on that if you want to actually comprehend it, and focus on
writing code that doesn't dump core at random intervals.

Cheers,
Luke

Dec 30 '05 #9
David Harmon wrote:
On 29 Dec 2005 17:46:52 -0800 in comp.lang.c++, we********@gmail.com
wrote,
delete pI1;

std::cout << *pI1 << std::endl;


After 'delete' the pointer is invalid! Do not try to use it!!


even
std::couit << (void*)pI1 ;
is undefined behavior.
Dec 30 '05 #10
Thanks, ALL :-)

/*
Becker
*/

Jan 1 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: LineVoltageHalogen | last post by:
Greetings All, I was hoping that someone out there has run into this issue before and can shed some light on it for me. I have a stored procedure that essentially does a mini ETL from a source...
10
by: greenflame | last post by:
I have the following function. It is supposed to multiply all the elements of a matrix by a specified factor. It tells me that on "line 58" that undefined is null or not an object. Here is the...
8
by: grundmann | last post by:
Hello, i got a strange compiler error. When compiling the following: // forward declarations typedef AvlTree<LineSegment,LineSegmentComperator> LSTree; void handleEventPoint (const...
3
by: Bill C. | last post by:
Hi, I've got a simple console app that just reads an XML file into a DataSet then prints out a description of each table in the DataSet, including column names and row values for each column. ...
6
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd...
6
by: leonecla | last post by:
Hi everybody, I'm facing a very very strange problem with a very very simple C program... My goal should be to write to a binary file some numbers (integers), each one represented as a sequence...
1
by: Martin Feuersteiner | last post by:
Dear Group I'm having a very weird problem. Any hints are greatly appreciated. I'm returning two values from a MS SQL Server 2000 stored procedure to my Webapplication and store them in...
5
by: Ian | last post by:
Hi everyone, I have found some bizarre (to me...!) behaviour of the Form_Activate function. I have a form which has a button control used to close the form and a subform with a datasheet view...
1
by: Paul | last post by:
Hi all, I've hit a strange problem with my ASP .NET site - occasionally when I go to a page, it will have part of another request's output mixed in with my output. This breaks the HTML on the...
5
by: ProgrammerMatt | last post by:
I searched for a similar error, and I didn't find any helpful references, so I'm trying not to post a redundant thread. I am programming for my summer job using Microsoft Visual C++ 6.0, and a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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,...
0
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...
0
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...
0
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,...
0
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...

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.