473,237 Members | 1,278 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,237 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 1725
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.