Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: volatile ma...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 7 Topic 26108 of 27670
Post > Topic >>

Re: volatile malloc memory?

by Antoninus Twink <nospam@[EMAIL PROTECTED] > May 9, 2008 at 02:06 PM

On  9 May 2008 at  7:36, Dan wrote:
> What is the correct way to allocate memory that is defined as volatile
> so that all standard compilers will not optomise out writes to the
> memory that are never read again?

What compiler are you using? How are you writing to the memory?

Here's a simple program:

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

int main(void)
{
  char *x  = malloc(128);
  memset(x, 0, 128);
  free(x);
  return 0;
}

Here's the start of the code produced by gcc with -O3 optimization
level:

0x080483e0 <main+0>:    lea    ecx,[esp+0x4]
0x080483e4 <main+4>:    and    esp,0xfffffff0
0x080483e7 <main+7>:    push   DWORD PTR [ecx-0x4]
0x080483ea <main+10>:   push   ebp
0x080483eb <main+11>:   mov    ebp,esp
0x080483ed <main+13>:   sub    esp,0x18
0x080483f0 <main+16>:   mov    DWORD PTR [ebp-0x8],ecx
0x080483f3 <main+19>:   mov    DWORD PTR [ebp-0x4],ebx
0x080483f6 <main+22>:   mov    DWORD PTR [esp],0x80
0x080483fd <main+29>:   call   0x8048340 <malloc@[EMAIL PROTECTED]
>
0x08048402 <main+34>:   mov    DWORD PTR [esp+0x8],0x80
0x0804840a <main+42>:   mov    DWORD PTR [esp+0x4],0x0
0x08048412 <main+50>:   mov    ebx,eax
0x08048414 <main+52>:   mov    DWORD PTR [esp],eax
0x08048417 <main+55>:   call   0x8048310 <memset@[EMAIL PROTECTED]
>
0x0804841c <main+60>:   mov    DWORD PTR [esp],ebx
0x0804841f <main+63>:   call   0x8048330 <free@[EMAIL PROTECTED]
>

Notice that even on the highest optimization level, gcc doesn't remove
the call to memset, even with no volatile qualifiers around, and even
though the memory is immediately free()d afterwards.
 




 7 Posts in Topic:
volatile malloc memory?
"Dan" <voids  2008-05-09 17:36:01 
Re: volatile malloc memory?
"Dan" <voids  2008-05-09 17:41:04 
Re: volatile malloc memory?
Johannes Bauer <dfnson  2008-05-09 11:25:15 
Re: volatile malloc memory?
Antoninus Twink <nospa  2008-05-09 12:15:14 
Re: volatile malloc memory?
Antoninus Twink <nospa  2008-05-09 14:06:12 
Re: volatile malloc memory?
vippstar@[EMAIL PROTECTED  2008-05-09 07:01:55 
Re: volatile malloc memory?
Antoninus Twink <nospa  2008-05-09 20:32:34 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Oct 10 22:22:24 CDT 2008.