tags:

views:

217

answers:

4

Readed bog of Ulrich Drepper and come across 2 entries that looks like conficting.

In the first one (string in global space) Ulrich states that the string should be defines as:

const char _pcre_ucp_names[] = "blabla";

while already in second one (string in function) he argues it should be declared as:

static const char _pcre_ucp_names[] = "blabla";

Can you explain what is the better name to declate a string?

UDP:

First of all I removed C++ tag - this question is valid for C as well for C++. So I don't think answers which explain what static means in class/funtion/file scope is relevant.

Read the articles before answering. The articles deal about memory usage - where the actual data is stored (in .rodata or in .data section), do the string should be relocated (if we're talking about unix/linux shared objects), is it possible to change the string or not.

UDP2 In first one it's said that for global variable following form:

(1) const char *a = "...";

is less good than

(2) const char a[] = "..."

Why? I always thought that (1) is better, since (2) actually replicate the string we assign it, while (1) only points to string we assign.

+1  A: 

Declaring it static means (if at global, file level) that it won't be visible outside this translation unit, or (if inside a scope) that it will retain its value between executions of the scope. It has nothing to do with the "constness" of the data.

unwind
A: 

Is it for use at a global (file) level or within a class or within a function ? The meaning of static differs ..

For a file level: It depends on the scope you want (either global or limited to the file). No other difference.

For a class: It's best with the static if you're not gonna change it. Because a const can still be redefined on the constructor so it will have to allocate space for a pointer inside the class itself if it's not static. If it is static then no need for a pointer in each class.

For a function: Doesn't really change anything important I think. In the non static case, a pointer will be allocated on the stack and initialized to point in .rodata at each function call. In the other case, it's more like a global variable but with limited scope.

246tNt
For functions, the meaning depends on whether the function is a class member or not. Non-member static functions are similar to file-scoped static variables: they're only visible in the file where they're defined. Static member functions can be called without having an object to reference (SomeClass::staticMember() vs. someObject.nonStaticMember()).
LnxPrgr3
+6  A: 

It depends—if you need the string to be visible to other source files in a project, you can't declare it static. If you only need to access it from the file where it's defined, then you probably want to use static.

The blog post you mention was talking about something different, though:

#include <stdio.h>
#include <string.h>
int main(void)
{
  const char s[] = "hello"; /* Notice this variable is inside a function */
  strcpy (s, "bye");
  puts (s);
  return 0;
}

In that case, static means something different: this creates a variable that persists across multiple calls to the same function. His other example showed a global variable, outside of a function.

EDIT:

To clarify, since you edited your question, the reason you don't want to use const char *a = "string" is you create an extra writable pointer. This means that, while you can't change the characters of the string, you can still make the pointer point to an entirely different string. See below:

const char *hello = "hello";

int main( int argc , char const *argv[] )
{
    hello = "goodbye";
    puts(hello);
    return 0;
}

That example compiles and runs. If hello is supposed to be constant, this is surely not what you want. Of course, you can also get around this by writing this:

const char * const hello = "hello";

You still have two variables where you only needed one though -- hello is a pointer to a string constant, where if it's an array there isn't that extra pointer in the way.

LnxPrgr3
+1 for static. But what about global variable ("first one") - why const char a[] i= ".." is prefered to const char a* = ".."?
dimba
Because `const char *a = "string"` leaves you with an extra, unnecessary pointer. Every time you read that variable, you have to go through the pointer -- remember that a `const char *` can still be pointed to a different string. `const char a[] = "string"` avoids creating that extra pointer, saving space and time.
LnxPrgr3
+1  A: 

While this is indeed a const string, it's neither a pointer nor a const pointer nor is the second one a declaration.

Both define (and initialize) a constant array of characters.

The only difference is that the first one will be visible and accessible from other translation units (proper declarations assumed), while the second one won't.

Note that, in C++, instead of making variables and constants static, you could put them into an unnamed namespace. Then, too, they are inaccessible from other translation units.

sbi