473,320 Members | 1,921 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,320 software developers and data experts.

free() unexpected behaviour

Hi,

After deallocating dynamically allocated memory, I was still accessible to access the data present in that memory. Can u please help me out why this is happening. This happened with c++(new and delete) operators as well. What is the logic behind. Here is the program I tested in C.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{

char *ptr;
char *str="kranthi";
int len;

len=strlen(str);
ptr=(char *)malloc(len+1);

if(ptr==NULL)
printf("memory allocation failed");
else
{
strcpy(ptr, str);
printf("data is %s \n",ptr);
}

free(ptr);
printf("\n after freeing the memory ");
printf("data is %s \n",ptr);
return 0;

}
Oct 16 '06 #1
2 1465
Banfa
9,065 Expert Mod 8TB
After deallocating dynamically allocated memory, I was still accessible to access the data present in that memory. Can u please help me out why this is happening. This happened with c++(new and delete) operators as well. What is the logic behind. Here is the program I tested in C.
The real question is what are you doing trying to access memory you have deallocated. When you free (or delete) memory all you are doing is informing the underlying system that you are no longer using that memory and that it now has control of that memory and is free to do whatever it wants with it.

It is possible that just at that moment what the system decides to doing with that memory is nothing.

However by accessing that memory after freeing it you have invoked undefined behaviour and the system is at liberty to do anything it wishes. All bets are off from this point, you program may or may not work.
Oct 16 '06 #2
dtimes6
73
This is because of free returns the memory, but the data at the time is not cleared. It will change until the piece of memory is re-allocated, and used. That is why you have to give a initial number before using your variable. but the memory may returned to system and its not a good idea if you access it again.

Some of the C++ new and delete operation just call malloc and free.
Oct 16 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Gerhard Esterhuizen | last post by:
Hi, I am observing unexpected behaviour, in the form of a corrupted class member access, from a simple C++ program that accesses an attribute declared in a virtual base class via a chain of...
29
by: Hassan Iqbal | last post by:
hi, in the code below i find that i am able to access p even after i have freed it. not only that the previous values stored in p are accessible even after reallocation of memory to p. please...
10
by: sindica | last post by:
I am using DevC++ 4.0 lately, which uses Mingw port of GCC, on a WinXP. I am surprised to see the malloc behaviour which is not consistent with the documentation. See the program and its output...
86
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory...
9
by: Jeff Louie | last post by:
In C# (and C++/cli) the destructor will be called even if an exception is thrown in the constructor. IMHO, this is unexpected behavior that can lead to an invalid system state. So beware! ...
8
by: Steven D'Aprano | last post by:
I came across this unexpected behaviour of getattr for new style classes. Example: >>> class Parrot(object): .... thing = .... >>> getattr(Parrot, "thing") is Parrot.thing True >>>...
2
by: bb | last post by:
Hi, I am using gcc v4.0.2 on fedora core 4 (2.6.16). Any reason why the handler set thru' set_unexpected() never gets called in the following code? --------- Code ------------- #include...
33
by: Reddy | last post by:
Hello, Can someone clarify this? char *p = (char *) malloc (100*sizeof(char)); p=p+50; free(p); Does free clear all the 100 bytes of memory or only 50 as it is pointing to 51st byte of...
23
by: gu | last post by:
hi to all! after two days debugging my code, i've come to the point that the problem was caused by an unexpected behaviour of python. or by lack of some information about the program, of course!...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
1
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.