Hi,
Iam confused as to when is the memory freed in this program.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
union test{
char *i;
char *ch;
};
int main()
{
union test test;
test.ch = malloc(6);
strcpy(test.ch, "hello");
printf("test.i == %s test.ch == %s test.i == [%p] test.ch ==
[%p]\n",test.i,test .ch,test.i,test .ch);
free(test.i);
printf("test.i == %s test.ch == %s test.i == [%p] test.ch ==
[%p]\n",test.i,test .ch,test.i,test .ch);
free(test.ch);
printf("test.i == %s test.ch == %s test.i == [%p] test.ch ==
[%p]\n",test.i,test .ch,test.i,test .ch);
return 0;
}
And the output I get from my solaris box is :
test.i == hello test.ch == hello test.i == [209b8] test.ch == [209b8]
test.i == hello test.ch == hello test.i == [209b8] test.ch == [209b8]
test.i == hello test.ch == hello test.i == [209b8] test.ch == [209b8]
regards
rohitash
Nov 14 '05
10 1350
"Stephen L." <sd*********@ca st-com.net> wrote: Richard Bos wrote: "Stephen L." <sd*********@ca st-com.net> wrote:
rohit wrote: > printf("test.i == %s test.ch == %s test.i == [%p] test.ch == > [%p]\n",test.i,test .ch,test.i,test .ch); > free(test.ch);
You got lucky here. You've `free()'d the same pointer value _twice_. You've corrupted your heap. You don't know this. It invokes undefined behaviour; this _may_ mean corrupting his heap, but it may also mean ignoring the statement, crashing with a segmentation fault, or mailing his resume to ...
man malloc(3c) for details.
Your particular system's manual pages are immaterial. This is
comp.lang.c, not comp.lang.steph en-l-s-system.manpage. c. Just because
_your_ system defines that using free() on an already free()d pointer
corrupts "your heap", that doesn't mean you can assume all systems do.
Posters who have identified this behavioral aspect of `free()' have usually noted it by stating that their (next) `malloc()'/`free()' core dumps.
Note: _usually_.
"Undefined results will occur if the space assigned by malloc() is overrun or if some random number is passed to free()."
_Exactly_. _Undefined_ results. _Not_ "the heap will be corrupted".
It _doesn't_ say, "and BTW, the heap is fine."
Neither does it say "and the heap will be corrupted", but that is what
you claimed would happen. It _may_ happen. It _may_ also mail a love
letter to your supervisor.
page(s) which would bring me to any of the conclusions you've arrived at - why are posters trying to DEFINE undefined behavior?
I don't know. _You_ are the one trying to do so, by claiming that it
_will_ corrupt the heap. I merely pointed out that undefined behaviour
means that you don't even know that this is true.
But providing garbage answers like "mailing his resume to ..." really hurt the credibility of the group as a whole, IMHO.
I'm so sorry. Could you please provide a quote from the Standard (note:
_not_ your system-specific man pages) which shows me the error of my
ways, by stating that undefined behaviour may _not_ result in a resume
being mailed?
Richard This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Simon Elliott |
last post by:
I'd like to do something along these lines:
struct foo
{
int i1_;
int i2_;
};
struct bar
{
|
by: Neil Zanella |
last post by:
Hello,
I would like to know what the C standards (and in particular the C99
standard) have to say about union initializers with regards to the
following code snippet (which compiles fine under gcc 3.2.2 but
does not produce the expected results, the expected results
being the ones annotated in the comments in the code):
#include <stdlib.h>
|
by: ranjeet.gupta |
last post by:
Dear ALL
As we know that when we declare the union then we have the size of the
union which is the size of the highest data type as in the below case
the
size should be 4 (For my case and compiler), and it is,
what I conclude from the below code
union data_type {
|
by: tedu |
last post by:
does anyone know of a platform/compiler which will place union elements
to not overlap?
as in
union u {
int a;
long b;
size_t c;
};
in my limited experience, writing to any of (a, b, or c) will affect
the value read from any other. i understand this is UB, but i'm
|
by: wkaras |
last post by:
In my compiler, the following code generates an error:
union U
{ int i; double d; };
U u;
int *ip = &u.i;
U *up = static_cast<U *>(ip); // error
I have to change the cast to reinterpret_cast for the code
| |
by: Yevgen Muntyan |
last post by:
Hey,
Why is it legal to do
union U {unsigned char u; int a;};
union U u;
u.a = 1;
u.u;
I tried to find it in the standard, but I only found that
|
by: =?gb2312?B?zfWzrLey?= |
last post by:
Union un
{ int I;
char c;
}
main()
{
union un x;
x.c=10;
x.c=1;
|
by: Peter Olcott |
last post by:
Why can a union have a member with a copy constructor?
|
by: SRK |
last post by:
Hi,
I wanted to use an anonymous union within an structure something like
below -
struct Test
{
union
{
std::string user; //char user;
std::string role; //char role;
|
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: 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,...
| |
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: 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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |