string-pool

Implementing a “string pool” that is guaranteed not to move

落爺英雄遲暮 提交于 2019-11-29 06:41:17
I need a "string pool" object into which I can repeatedly insert a "sequence of chars" (I use this phrase to mean "string" without confusing it with std::string or a C string), obtain a pointer to the sequence, and be guaranteed that the pointer will not become invalidated if/when the pool needs to grow. Using a simple std::string as the pool won't work, because of the possibility for the string to be reallocated when it outgrows its initial capacity, thus invalidating all previous pointers into it. The pool will not grow without bound -- there are well-defined points at which I will call a

How does this Java code snippet work? (String pool and reflection) [duplicate]

情到浓时终转凉″ 提交于 2019-11-28 15:35:58
问题 This question already has an answer here: Is a Java string really immutable? 15 answers Java string pool coupled with reflection can produce some unimaginable result in Java: import java.lang.reflect.Field; class MessingWithString { public static void main (String[] args) { String str = "Mario"; toLuigi(str); System.out.println(str + " " + "Mario"); } public static void toLuigi(String original) { try { Field stringValue = String.class.getDeclaredField("value"); stringValue.setAccessible(true)

C optimisation of string literals

时光怂恿深爱的人放手 提交于 2019-11-26 13:46:35
just been inspecting the following in gdb: char *a[] = {"one","two","three","four"}; char *b[] = {"one","two","three","four"}; char *c[] = {"two","three","four","five"}; char *d[] = {"one","three","four","six"}; and i get the following: (gdb) p a $17 = {0x80961a4 "one", 0x80961a8 "two", 0x80961ac "three", 0x80961b2 "four"} (gdb) p b $18 = {0x80961a4 "one", 0x80961a8 "two", 0x80961ac "three", 0x80961b2 "four"} (gdb) p c $19 = {0x80961a8 "two", 0x80961ac "three", 0x80961b2 "four", 0x80961b7 "five"} (gdb) p d $20 = {0x80961a4 "one", 0x80961ac "three", 0x80961b2 "four", 0x80961bc "six"} I'm really

Questions about Java's String pool [duplicate]

喜夏-厌秋 提交于 2019-11-25 21:58:21
问题 This question already has an answer here: What is the difference between “text” and new String(“text”)? 11 answers Consider this code: String first = \"abc\"; String second = new String(\"abc\"); When using the new keyword, Java will create the abc String again right? Will this be stored on the regular heap or the String pool? How many String s will end in the String pool? 回答1: If you use the new keyword, a new String object will be created. Note that objects are always on the heap - the