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

volatile malloc memory?

Dan
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?

I have some encryption programs that, depending on the
implementation/compiler, part of the code that clears memory at the end of
the program gets optomised away.

Jun 27 '08 #1
6 3861
Dan

"Dan" <vo***@sometwher.worldwrote in message
news:48**********************@news.optusnet.com.au ...
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?

I have some encryption programs that, depending on the
implementation/compiler, part of the code that clears memory at the end of
the program gets optomised away.
ps. i tried some obvious stuff like specifing the character pointed to as
volatile when assigning to it a value, but it still gets optomised away.
Jun 27 '08 #2
Dan schrieb:
ps. i tried some obvious stuff like specifing the character pointed to as
volatile when assigning to it a value, but it still gets optomised away.
Are you sure your code works correctly? I just tried this out:

#include <stdlib.h>

int main() {
char *x;
volatile char *y;

x = malloc(128);
x[0] = 0xaa;
x[0] = 0xbb;
x[0] = 0xcc;
x[0] = 0xdd;

y = malloc(128);
y[0] = 0xaa;
y[0] = 0xbb;
y[0] = 0xcc;
y[0] = 0xdd;

return 0;
}

Which compiles which gcc-4.something and -O3 to the expected result

4004f4: bf 80 00 00 00 mov $0x80,%edi
4004f9: e8 32 ff ff ff callq 400430 <malloc@plt>
4004fe: bf 80 00 00 00 mov $0x80,%edi
400503: c6 00 dd movb $0xdd,(%rax)
400506: e8 25 ff ff ff callq 400430 <malloc@plt>
40050b: c6 00 aa movb $0xaa,(%rax)
40050e: c6 00 bb movb $0xbb,(%rax)
400511: c6 00 cc movb $0xcc,(%rax)
400514: c6 00 dd movb $0xdd,(%rax)

Kind regards,
Johannes

--
"Wer etwas kritisiert muss es noch lange nicht selber besser können. Es
reicht zu wissen, daß andere es besser können und andere es auch
besser machen um einen Vergleich zu bringen." - Wolfgang Gerber
in de.sci.electronics <47***********************@news.freenet.de>
Jun 27 '08 #3
On 9 May 2008 at 9:25, Johannes Bauer wrote:
volatile char *y;
Possibly the OP had a char *volatile y, giving a volatile pointer to
char, rather than a pointer to volatile char?

Jun 27 '08 #4
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@plt>
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@plt>
0x0804841c <main+60>: mov DWORD PTR [esp],ebx
0x0804841f <main+63>: call 0x8048330 <free@plt>

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.

Jun 27 '08 #5
On May 9, 3:06 pm, some troll wrote:
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:
<possible gcc output>
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.
It shouldn't; That would make your program succeed even if malloc()
returned NULL, which with your current code is not the case.
Jun 27 '08 #6
On 9 May 2008 at 14:01, vi******@gmail.com wrote:
On May 9, 3:06 pm, Antoninus Twink wrote:
>#include <stdlib.h>
#include <string.h>

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

}
[snip]
>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.

It shouldn't; That would make your program succeed even if malloc()
returned NULL, which with your current code is not the case.
Define succeed.

Surely if malloc() returns NULL, then passing a null pointer to memset
will invoke undefined behavior, and "success" is one possible result of
undefined behavior. I know that if you believe the regulars here, then
formatting your hard disk and generating nasal demons are the most
likely consequences of undefined behavior, but out here in the real
world, there are other possibilities...

Jun 27 '08 #7

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

Similar topics

4
by: Andrew | last post by:
Section 17.4.3 of the ECMA-334 C# Language Specification says 1 When a field-declaration includes a volatile modifier, the fields introduced by that declaration are volatile fields. 2 For...
11
by: srinivas reddy | last post by:
Hi, Is there any chance that a program doesn' work properly even after a variable is declared as volatile? I remember somebody mentioning a scenario involving L1, L2 caches. Could anybody throw...
9
by: Tim Rentsch | last post by:
I have a question about what ANSI C allows/requires in a particular context related to 'volatile'. Consider the following: volatile int x; int x_remainder_arg( int y ){ return x % y; }
8
by: Tim Rentsch | last post by:
Here's another question related to 'volatile'. Consider the following: int x; void foo(){ int y; y = (volatile int) x;
8
by: Jason Curl | last post by:
I was reading a previous article about when to use volatiles. My program has a shared memory buffer that is being accessed by two threads. The threads are protected by an operating system...
14
by: Pierre | last post by:
Using the "volatile" keyword, creates a problem if I intend to use any of the interlocked APIs. The compiler generates an error if I use the following line, for example: ...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
8
by: CJ | last post by:
The following tries to make thread-safe code while avoiding the high cost of a lock. But is it kosher? // file or global scope level int X; volatile _Bool IsInitializedX=false;
10
by: S James S Stapleton | last post by:
Is volatile necessary anymore? I have a two-thread piece of code I've been testing to figure out what volatile does (fairly simple code, uses pthreads). I have an update thread (variables passed as...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.