是否宣布使用象正常阵列一样的点燃废物空间的形状?
char* foo[] = { "bar", "foobar"}
v
char foo[2][7] = {"foo", "foobar"}
AFAIK,第二例“废物”记忆是第一个指数,只需要4,但只需要7。 第一种情况是否相同?
是否宣布使用象正常阵列一样的点燃废物空间的形状?
char* foo[] = { "bar", "foobar"}
v
char foo[2][7] = {"foo", "foobar"}
AFAIK,第二例“废物”记忆是第一个指数,只需要4,但只需要7。 第一种情况是否相同?
无,有一系列点人,每一方位数只分配到其实际特性所需的 by数,加之无足迹。
但是,权衡的是,点人自己是记忆;通常在现代的64轨计算机上使用8台 by。
So
char* foo[] = { "bar", "foobar"};
would normally use 2*8 + (3+1) + (6+1) =
27 bytes, whereas
char foo[2][7] = {"foo", "foobar"};
uses 14.
因此,如果你知道你的所有扼杀都非常短,或者如果它们都非常接近同样长,那么像第二种做法那样的两维阵将使用较少的记忆。 如果时间长短不一,有些时间较长,则会少使用一系列点。
To begin with, one must appreciate that (correct) C language source code is the expression of an algorithm targeted for a somewhat vague, virtual machine. A VM, in this context, has no restrictions on storage, including, for instance, the size of the program s stack. Nor are constraints such as "completing execution before the end of the universe" a hinderance.
On the other hand, machine code is a collection of binary values used by a (finite!) particular real-world (i.e. "physical") computer. This machine IS constrained by its finite physicality and utility (eg. Bletchley Park s Bombe would be useless if they required more than a day-or-two to crack one day s Enigma settings).
The other (OP accepted) answer seems to be almost correct for the virtual machine interpretation.
However... that answer responds to a pointless concern about consuming more-or-fewer bytes of the unlimited storage available to a C program executing on a virtual machine.
Sadly, that answer also conflates a particular real-world instance (sizeof pointer_var == 8
(bytes)) with the unspecified size of a conceptual pointer of a purely conceptual virtual machine.
你们不需要知道大会来理解以下几点。 希望这些例子开始揭示你的问题的微妙之处。
2. 考虑将你的问题提一下,并具体译成大会的一种语文:
#include <stdio.h>
char foo[1][10] = { "foobar" }; // global/file scope initialisation
char *bar[1] = { "barfoo" };
int main( void ) {
puts( foo[0] );
puts( bar[0] );
return 0;
}
大会(加上一些补充意见):
foo:
.string "foobar" // 6+1 bytes...
.zero 3 // +3 padding bytes == [10]
.LC0:
.string "barfoo" // 6+1 bytes of data
bar:
.quad .LC0 // pointer (likely 8 bytes) allocated
main:
push rbp
mov rbp, rsp
mov edi, OFFSET FLAT:foo // compiler "knows" this address
call puts
mov rax, QWORD PTR bar[rip] // indirect access via pointer variable
mov rdi, rax
call puts
mov eax, 0
pop rbp
ret
如今,数据转向当地(快速)空间,并援引-O3选择:
#include <stdio.h>
int main( void ) {
char foo[1][10] = { "foobar" };
char *bar[1] = { "barfoo" };
puts( foo[0] );
puts( bar[0] );
return 0;
}
Assembly:
.LC0:
.string "barfoo" // 6+1 bytes of data as before
// there is no 8 byte pointer in the program s data segment
// and "foobar" is not in the executable s data segment
main:
movabs rax, 125762588864358
// 125762588864358(base10)
// == 0x00 00 72 61 62 6F 6F 66 (in HEX separated into 8 bytes)
// == r a b o o f (in ASCII)
// "foobar" is "stored" in 8 bytes of program code segment
sub rsp, 24
mov QWORD PTR [rsp+6], rax // Opaque chicanery!! Complicated, eh?
lea rdi, [rsp+6]
xor eax, eax
mov WORD PTR [rsp+14], ax
call puts
mov edi, OFFSET FLAT:.LC0 // the "pointer" is just an address in code
call puts
xor eax, eax
add rsp, 24
ret
For consideration: The 2nd example demonstrates how upto 8 bytes of "string" is loaded at the top the function (main()
).
Try this with a much longer string. (Code segment grows, but not data segment???)
What changes if foo[][];
is made const
or static
? What changes when foo
is indexed with a variable (eg: puts( foo[a] );
) whose run-time value the compiler cannot predict? How does the compiler adjust the emitted Assembly (normally unseen) so that the behaviour of the program fulfills the algorithm specified by the source code? Optimise for speed , or for size ?
How will compiler behave if the string (with its length) is in another source file (i.e. translation unit).
So many avenues to explore and understand!
就简而言之,这只不过是一 gl一 gl一hole一hole(在东方),你的问题应当引起。
祝福你的朋友。 你们将能够自己探讨和找到答案和宝贵的理解。 例外! 贷款不足。
For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...
最好、最小、最快、开放的来源、C/C++ 3d 提供方(在3ds max模型的支持下),而不是通用公平市价,
Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...
I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...
I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...
I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...
Is there anything other than DDD that will draw diagrams of my data structures like DDD does that runs on Linux? ddd is okay and runs, just kind of has an old klunky feeling to it, just wanted to ...
Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...