On Fri, Oct 07, 2005 at 07:06:21PM +0100, Felix Oxley wrote:
> 
> I was looking on the kernel-janitors site at the To Do list and found this 
> task below, which I decided to try my hand at. 
> (http://www.kerneljanitors.org/TODO)
> 
> > 1) The string form
> > 
> >         [const] char *foo = "blah";
> > 
> > creates two variables in the final assembly output, a static string, and
> > a char pointer to the static string.  The alternate string form
> > 
> >         [const] char foo[] = "blah";
> > 
> > is better because it declares a single variable.
> > ...
> 
> However, I was advised by a list member that this work was of uncertain 
> benefit:
> 
> > Ricardo Nabinger Sanchez wrote:
> >  
> > By May, 2005, I was working on this.  Alexey told me that besides what gcc
> > should do, I had to confirm that the change made was actually better.
> > 
> > In some cases, it was not.  I've made a testcase, and the results were
> > pretty odd (I was expecting something else):

Ricardo's tests look so strange because the strings aren't used.
This means that the static versions can be optimized away (or partly
optimized away, e.g. remove the pointer but not the string), but whether
this happens depends on optimization level and might change from release
to release.  Try comparing, e.g.

-----------------------
#include <unistd.h>
static char* msg = "Hello, world\n";
int main() {
  write(1, msg, sizeof(msg) - 1);
  return 0;
}
-----------------------

and

-----------------------
#include <unistd.h>
static char msg[] = "Hello, world\n";
int main() {
  write(1, msg, sizeof(msg) - 1);
  return 0;
}
-----------------------



Reply via email to