String pooling not working?

Hello,

I was just messing around, and I've created this little program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main(int argc, char* argv[]) {

	char string1[] = "Hello";
	char string2[] = "Hello";

	const char* string3 = "Hello";
	const char* string4 = "Hello";

	std::cout << &string1 << std::endl;
	std::cout << &string2 << std::endl;
	std::cout << &string3 << std::endl;
	std::cout << &string4 << std::endl;

	std::cin.get();
	return 0;
}


The idea, was to show that when you compile with the /GF flag set, string pooling is enabled, and the const char*'s which point to string literals would point to the same, read-only memory location.
However, the addresses are still different.
What's the deal? Obviously, I'm compiling with /GF set. What am I missing here?
I'm using VC++2008 IDE if it makes a difference.
Last edited on
Maybe you have some other optimization flag conflicting with this? Also, string1 and string2 will always have different addresses from each other and from string3 and string4. The only candidates for seeing string pooling here are string3 and string4, but it's not guaranteed afaik.
Last edited on
Also, string1 and string2 will always have different addresses from each other and from string3 and string4.

Yes I know :). They're just there to demonstrate the difference between character arrays and string literals.
As far as I know, there are no conflicting flags set, I'll double check anyways.
but it's not guaranteed

That's interesting. I wonder under which conditions it is guaranteed.
There are no conditions under which it is guaranteed. There are plenty of conditions in which it might happen, but the compiler knows best and does what it wants.
Interesting. That's all I can ask for. Thanks L B!
Topic archived. No new replies allowed.