Thread: Re: [Dev-C++] malloc and free
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Daniel G. <sp...@ho...> - 2004-01-10 10:24:36
|
Seconded. You are printing what's at that memory location, and it seems the
OS hasn't dealt with the value at that memory location yet.
Here's a question though - can I do something like
int* SomeMemory = 0x001249FF;
printf("%d\n", SomeMemory);
To print whatever is at any memory location (on the stack/heap in this case,
I forget which starts around 0x0012~ in windows). Is this valid (dangerous I
am sure, but can I do this?).
Daniel
>From: OROSZI Balázs <or...@vp...>
>To: dev...@li...
>Subject: Re: [Dev-C++] malloc and free
>Date: Sat, 10 Jan 2004 11:09:48 +0100
>
>Siva Chandra wrote:
>>I used the follwoing piece of code as a test:
>>
>>int *a;
>>a = (int *)malloc(sizeof(int));
>>*a = 5;
>>free(a);
>>printf("%d\n", *a);
>>
>>This prints 0 in the linux environment and 5 in the
>>windows environment!!
>>
>>does anyone out there know whats happening??
>>the same happens with new and delete!!
>
>You should take the above printf() as "unpredictable". After you free or
>delete some allocated memory, it is the operating system's task to do
>whatever it want's with it. It seems, that Linux automatically fills freed
>memory with zeros, while Windows does not.
>
>Again, the above must be considered as unpredictable, so you shouldn't care
>about freed memory (variables): they are no longer yours, they were given
>back to the OS, so you theoretically "don't want" to do anything with them
>any further.
>
>--
>Greetings,
> Balázs
>
>
>
>-------------------------------------------------------
>This SF.net email is sponsored by: Perforce Software.
>Perforce is the Fast Software Configuration Management System offering
>advanced branching capabilities and atomic changes on 50+ platforms.
>Free Eval! http://www.perforce.com/perforce/loadprog.html
>_______________________________________________
>Dev-cpp-users mailing list
>Dev...@li...
>TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
>https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
_________________________________________________________________
Stay in touch with absent friends - get MSN Messenger
http://www.msn.co.uk/messenger
|
|
From: Gil <gil...@fr...> - 2004-01-10 23:12:47
|
free(ptr) cannot put ptr to NULL, simply because you don't pass the
address of ptr.
If you would that all you ptr that are freed, are set to NULL. Define a
fonction that do:
void freeN(void **ptr) {
free(*ptr);
ptr =3D NULL;
}
Date: Sat, 10 Jan 2004 17:43:05 +0100
From: =3D?ISO-8859-1?Q?OROSZI_Bal=3DE1zs?=3D <or...@vp...>
To: dev...@li...
Subject: Re: [Dev-C++] malloc and free
Siva Chandra wrote:
> firstly, thanx Daniel and Oroszi
"Bal=E1zs" - that's my firstname, written at second place (we Hungarians =
are "crazy" people - or maybe everyone else is, except us? :D
> for the descriptive
> replyies. I have always thought free(ptr) would put
> ptr to NULL.
I think you cannot even assume that anywhere. The docs of free() only=20
tell you, that the memory is freed. However, maybe it's a documentation=20
"bug", it doesn't state explicitly that free() is NOT GUARANTEED to put=20
NULL to the deallocated memory (probably because of performance
reasons).
--
Greetings,
Bal=E1zs
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.560 / Virus Database: 352 - Release Date: 08/01/2004
=20
|
|
From: Gil <gil...@fr...> - 2004-01-10 23:15:17
|
Oooppsss it's not correct ! Sorry.
Correction:
void freeN(void **ptr) {
free(*ptr);
(*ptr) = NULL;
}
-----Original Message-----
From: Gil [mailto:gil...@fr...]
Sent: dimanche 11 janvier 2004 00:13
To: 'dev...@li...'
Subject: Re: [Dev-C++] malloc and free
free(ptr) cannot put ptr to NULL, simply because you don't pass the
address of ptr.
If you would that all you ptr that are freed, are set to NULL. Define a
fonction that do:
void freeN(void **ptr) {
free(*ptr);
ptr = NULL;
}
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.560 / Virus Database: 352 - Release Date: 08/01/2004
|
|
From: Siva C. <siv...@ya...> - 2004-01-10 10:45:57
|
firstly, thanx Daniel and Oroszi for the descriptive
replyies. I have always thought free(ptr) would put
ptr to NULL.
> Here's a question though - can I do something like
> int* SomeMemory = 0x001249FF;
> printf("%d\n", SomeMemory);
>
> To print whatever is at any memory location (on the
> stack/heap in this case,
> I forget which starts around 0x0012~ in windows). Is
> this valid (dangerous I
> am sure, but can I do this?).
I have typed that bit of code and found that it is a
segementation fault(in Win2000)!
My explanation would be that u r trying to access what
is not meant for or what u said u would not access.
i.e the said memory location has neither been
allocated by new or malloc, nor has been declared
anywhere in the code.
regards,
Siva Chandra
__________________________________
Do you Yahoo!?
Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
http://hotjobs.sweepstakes.yahoo.com/signingbonus
|
|
From: <or...@vp...> - 2004-01-10 16:40:02
|
Siva Chandra wrote: > firstly, thanx Daniel and Oroszi "Balázs" - that's my firstname, written at second place (we Hungarians are "crazy" people - or maybe everyone else is, except us? :D > for the descriptive > replyies. I have always thought free(ptr) would put > ptr to NULL. I think you cannot even assume that anywhere. The docs of free() only tell you, that the memory is freed. However, maybe it's a documentation "bug", it doesn't state explicitly that free() is NOT GUARANTEED to put NULL to the deallocated memory (probably because of performance reasons). -- Greetings, Balázs |
|
From: Per W. <pw...@ia...> - 2004-01-10 12:44:06
|
The code is syntactically completely ok. However, you shouldn't take for
granted that you know what is stored on a specific location, unless you
have a document saying so.
In Windows, you could use IsBadReadPtr(ptr,size)/IsBadWritePtr(ptr,size)
to check if you have read access to the memory. Then you don't have to
hook exceptions or just loose the program because you try some fancy
memory tricks.
/Per W
On Sat, 10 Jan 2004, Daniel Glenfield wrote:
> Seconded. You are printing what's at that memory location, and it seems t=
he
> OS hasn't dealt with the value at that memory location yet.
>
> Here's a question though - can I do something like
> int* SomeMemory =3D 0x001249FF;
> printf("%d\n", SomeMemory);
>
> To print whatever is at any memory location (on the stack/heap in this ca=
se,
> I forget which starts around 0x0012~ in windows). Is this valid (dangerou=
s I
> am sure, but can I do this?).
>
> Daniel
>
>
> >From: OROSZI Bal=E1zs <or...@vp...>
> >To: dev...@li...
> >Subject: Re: [Dev-C++] malloc and free
> >Date: Sat, 10 Jan 2004 11:09:48 +0100
> >
> >Siva Chandra wrote:
> >>I used the follwoing piece of code as a test:
> >>
> >>int *a;
> >>a =3D (int *)malloc(sizeof(int));
> >>*a =3D 5;
> >>free(a);
> >>printf("%d\n", *a);
> >>
> >>This prints 0 in the linux environment and 5 in the
> >>windows environment!!
> >>
> >>does anyone out there know whats happening??
> >>the same happens with new and delete!!
> >
> >You should take the above printf() as "unpredictable". After you free or
> >delete some allocated memory, it is the operating system's task to do
> >whatever it want's with it. It seems, that Linux automatically fills fre=
ed
> >memory with zeros, while Windows does not.
> >
> >Again, the above must be considered as unpredictable, so you shouldn't c=
are
> >about freed memory (variables): they are no longer yours, they were give=
n
> >back to the OS, so you theoretically "don't want" to do anything with th=
em
> >any further.
> >
> >--
> >Greetings,
> > Bal=E1zs
> >
> >
> >
> >-------------------------------------------------------
> >This SF.net email is sponsored by: Perforce Software.
> >Perforce is the Fast Software Configuration Management System offering
> >advanced branching capabilities and atomic changes on 50+ platforms.
> >Free Eval! http://www.perforce.com/perforce/loadprog.html
> >_______________________________________________
> >Dev-cpp-users mailing list
> >Dev...@li...
> >TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> >https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
> _________________________________________________________________
> Stay in touch with absent friends - get MSN Messenger
> http://www.msn.co.uk/messenger
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Perforce Software.
> Perforce is the Fast Software Configuration Management System offering
> advanced branching capabilities and atomic changes on 50+ platforms.
> Free Eval! http://www.perforce.com/perforce/loadprog.html
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|