I have a summary of how a new keyword in Java creates a String("string"), 2 objects or just variable = "string" while creating 1 object. What is the memory allocation for String in Dart?
Thanks in advance for the answer.
It's complicated.
The Java String constructor is a copy-constructor. It creates a new object (new identity) with the same content.
The corresponding Dart operation would be String.fromCharCodes(otherString.codeUnits).
A Dart string literal, like "string" is a compile-time constant. It'll likely not require any memory allocation at runtime. It exists in the initial program before it starts running.
Doing string1 + string2 or "foo $v1 or $v2" will allocate new strings with the length of all the strings being concatenated.
Related
While experimenting with the zig syntax, I noticed the type expression of string literals is omitted in all examples. Which is totally fine, I'm not saying it shouldn't be.
const zig_string = "I am a string"; //it looks nice enough for sure and compiles fine ofcourse
However, because this type omission is a bit inconsistent* with other type declarations in zig, it can lead to beginners (like me) misinterpreting the actual type of string literals (which is fact quite rightfully complicated and 'different'). Anyway, after reading about the type of string literals being 'pointers to (utf-8 encoded) immutable (const), sentinel terminated arrays of u8 bytes' (yes?), with next to the hard coded length field, a terminator field like so: [<length>:0]. To check my own understanding, I thought it reasonable to try adding this type expression to the declaration, similar to how other arrays are conveniently declared, so with an underscore to infer the length, because who likes counting characters?
const string: *const [_:0]u8 = "jolly good"; //doesn't compile: unable to infer array size
But it didn't compile :(.
After dutifully counting characters and now specifying the length of my string however, it proudly compiled :)!
const string: *const [10:0]u8 = "jolly good"; //happily compiles
Which led me to my question:
Why is this length specification needed for string literals and not for other literals/arrays? - (And should this be so?)
Please correct my type description of string literals if I missed an important nuance.
I'd like to know to further deepen my understanding of the way strings are handled in zig.
*although there are more cases where the zig compiler can infer the type without it
Types never have _ in them.
"jolly good" is a string literal. *const [10:0]u8 is the type.
For "other literals/arrays":
const a = [_]u8{ 1, 2, 3 };
[_]u8{ 1, 2, 3 } is an array literal. The type is [3]u8 and it cannot be specified as [_]u8.
Look into slices. They offer a very convenient way to use strings and arrays.
I am confused by the following output:
local a = "string"
print(a.len) -- function: 0xc8a8f0
print(a.len(a)) -- 6
print(len(a))
--[[
/home/pi/test/wxlua/wxLua/ZeroBraneStudio/bin/linux/armhf/lua: /home/pi/Desktop/untitled.lua:4: attempt to call global 'len' (a nil value)
stack traceback:
/home/pi/Desktop/untitled.lua:4: in main chunk
[C]: ?
]]
What is the proper way to calculate a string length in Lua?
Thank you in advance,
You can use:
a = "string"
string.len(a)
Or:
a = "string"
a:len()
Or:
a = "string"
#a
EDIT: your original code is not idiomatic but is also working
> a = "string"
> a.len
function: 0000000065ba16e0
> a.len(a)
6
The string a is linked to a table (named metatable) containing all the methods, including len.
A method is just a function, taking the string as the first parameter.
function a.len (string) .... end
You can call this function, a.len("test") just like a normal function. Lua has a special syntax to make it easier to write. You can use this special syntax and write a:len(), it will be equivalent to a.len(a).
print(a.len) -- function: 0xc8a8f0
This prints a string representation of a.len which is a function value. All strings share a common metatable.
From Lua 5.4 Reference Manual: 6.4 String Manipulation:
The string library provides all its functions inside the table string.
It also sets a metatable for strings where the __index field points to
the string table. Therefore, you can use the string functions in
object-oriented style. For instance, string.byte(s,i) can be written
as s:byte(i).
So given that a is a string value, a.len actually refers to string.len
For the same reason
print(a.len(a))
is equivalent to print(string.len(a)) or print(a:len()). This time you called the function with argument a instead of printing its string representation so you print its return value which is the length of string a.
print(len(a))
on the other hand causes an error because you attempt to call a global nil value. len does not exist in your script. It has never been defined and is hence nil. Calling nil values doesn't make sense so Lua raises an error.
According to Lua 5.4 Reference Manual: 3.4.7 Length Operator
The length of a string is its number of bytes. (That is the usual
meaning of string length when each character is one byte.)
You can also call print(#a) to print a's length.
The length operator was introduced in Lua 5.1,
I know 4 different types how to define an empty String in Swift.
var newString = ""
var newString: String = ""
var newString: String = String()
var newString = String()
Which way is the most efficient way when it comes to how fast the code gets executed?
Is there even a different between them?
There's only really two different techniques here:
Using an empty string literal: var newString = ""
Calling empty String initializer: var newString = String()
The other two are just these two techniques, but with an explicit type annotation. Type annotations are a compile-time only aspect of your program, which exist only to communicate types to the compiler, with no impact at run-time. In this case, both variables would have been inferred to be String*, so these type annotations are redundant, and don't tell the compiler anything you didn't already know.
These two approaches should have the exact same run-time characteristics.
The compiler can intern string constants to prevent repetitions of the same string literals from being repeated in the final application binary. This actually wouldn't be relevant here, because Swift uses tagged pointers to store "small strings" (IDR what exactly constitutes "small"), meaning don't even require heap allocation.
In a sense, that means that having a short string like "abc" in your code is no different than 123. It's just a small value that's pushed onto the stack when you enter your function.
If you're coming from other languages, you might expect String() to result in a different instance on every call, but that isn't actually the case in Swift. There is no instance allocated at all, just an empty string as represented by the small-string storage, which is stored inline. The compiler should also be able to recognize that String() always results in the same value, which it should be able to inline, much like how Int() gets replaced with 0.
* Actually, if you define a type named StringLiteral in your module, the inferred type of "" will be your StringLiteral type rather than the default String.
I think question is quite self-explained. I want to know how can I convert memory address like this 0xc20803a000 to string type. Is it possible?
You can use fmt.Sprintf(), with %p (base 16 notation, with leading 0x)
myString := fmt.Sprintf("%p", yourPointer)
fmt.Sprintf() returns a string.
You can see several examples (of printing a memory pointer) in:
"How do I print the pointer value of a Go object? What does the pointer value mean?".
"Go by Example: String Formatting".
Replace in those examples Printf by Sprintf, and you have a string for you to use.
I'm using rubocop to basically clean up my messy code...
One of the errors were:
Use \ instead of + or << to concatenate those strings.
Why is this?
I can't find it in the Ruby documentation. Why would I use \ instead of + or <<?
In Ruby, literal strings are allocated as objects in memory when they are encountered. If you concatenate two string literals, as in
str = "foo" + "bar"
you will actually allocate three String objects: "foo", "bar" and the result of the concatenation (which is then referred to by str).
The same happens if you do:
"foo" << "bar"
In many cases, this is only a mild inefficiency which you should not worry too much about.
However, be aware that if you do that in a loop, and if the aggregate String grows large, you will allocate an ever larger String object at every iteration (you can avoid that by collecting the string parts in an Array, and then calling join when you are done; also, foo << 'bar' will modify foo in place, which is acceptable in a loop).
By using \ you do not concatenate intermediate objects, but instead effectively present the parser with one String literal (because \ simply continues the string on the next line).
The "foo" + "bar" + "baz" idiom is frequently used in Java, where strings are immutable, and literals are concatenated at compile time. (Every string literal in Java is only stored once and then reused throughout the code; Ruby does not do that.) Also, Java has no better way to continue strings over multiple lines, which is why Java programmers do it that way.
\ at the end of a line will stop the interpreter from treating the newline as end of statement. Therefore, you can make a multiline string without wearing the cost of concatenation or appending.