473,663 Members | 2,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<iostre am>

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 1756
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********@gmai l.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********@gmai l.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********@gmai l.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********@gmai l.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

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

Similar topics

24
4734
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 OLTP DB to a the target Operational Data Store. This is still in development so both DB's reside on the same machine for convenience. The stored proc runs successfully from within Query analyzer and this holds true on the following platforms: XP...
10
1165
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 code: function scalematrix(input,factor) { if (input.length == undefined) { var output = copy1darr(input); for (var i=0;i<output.length;i++) { output *= factor; }
8
1825
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 EventPoint& , LSTree& , double&, std::list<IntersectionPoint>& );
3
4594
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. I'm getting some strange results depending the input XML file I use. I was wondering if somebody could help me understand what is going on or point me to a good reference. The code for my program looks like this:
6
2927
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 have better luck here. So here goes! My program ignores any command line arguments, or at least it's supposed to. However, when I pass any command line arguments to the program, the behaviour of one of the functions changes mysteriously. I have...
6
8522
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 of 32 bit. I made this stupid trial code: --------------------------------------------- FILE *fout;
1
1440
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 sessions. Like This: prm4 = cmd1.CreateParameter With prm4
5
3103
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 showing a list of jobs from the database. When the main form loses focus and the user clicks the 'Close' button, I kept receiving error 2585 (This action cannot be carried out whilst processing a form or report event). This was tracked down to...
1
1112
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 page, and potentially exposes the personal information of other users. I'm using ASP .NET 2.0 with Win 2K3. Response buffering is on. The site is precompiled, and the bin directory grows with each deployment, but I don't know whether that would...
5
1729
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 program is having a strange error with char pointers. The following code worked correctly: int testInt = 11; int* Ptr = &testInt; cout << *Ptr << endl << Ptr;
0
8345
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
8857
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
8768
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
8547
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
7368
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...
0
5655
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4181
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...
0
4348
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1999
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.