在mmap()手册页中:
它的原型是:
void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
Run Code Online (Sandbox Code Playgroud)
和描述:
The mmap() function asks to map 'length' bytes starting at offset 'offset'
from the file (or other object) specified by the file descriptor fd into
memory, preferably at address 'start'.
Run Code Online (Sandbox Code Playgroud)
最后,对于最后一个论点:
'offset' should be a multiple of the page size as returned by getpagesize(2).
Run Code Online (Sandbox Code Playgroud)
根据我的实践,offset必须是页面大小的倍数,例如,我的Linux上有4096,否则,mmap()会返回Invalid argument,offset是文件偏移量,为什么它必须是虚拟内存系统页面大小的倍数?
谢谢,
对于Perl子例程,如果传递0参数,我可以使用4个表单来调用它.但是如果传递一个或多个参数,有一个我不能使用的表单,请看下面:
sub name
{
print "hello\n";
}
# 4 forms to call
name;
&name;
name();
&name();
sub aname
{
print "@_\n";
}
aname "arg1", "arg2";
#&aname "arg1", "arg2"; # syntax error
aname("arg1", "arg2");
&aname("arg1", "arg2");
Run Code Online (Sandbox Code Playgroud)
错误输出是
String found where operator expected at tmp1.pl line 16, near "&aname "arg1""
(Missing operator before "arg1"?)
syntax error at tmp1.pl line 16, near "&aname "arg1""
Execution of tmp1.pl aborted due to compilation errors.
Run Code Online (Sandbox Code Playgroud)
有人可以从编译器的角度解释错误输出吗?我不明白为什么抱怨缺少操作员.
谢谢
请看下面的代码:
use strict;
use warnings;
print "subroutine is defined\n" if defined &myf;
myf();
sub myf
{
print "called myf\n";
}
undef &myf;
#myf();
print "now subroutine is defined\n" if defined &myf;
Run Code Online (Sandbox Code Playgroud)
输出是
subroutine is defined
called myf
Run Code Online (Sandbox Code Playgroud)
第一个print语句可以打印,这是否意味着解释器(或编译器?)看得更远并看到子例程定义?如果是这样,为什么它没有看到undef &myf;第二个print陈述?
谢谢
这是一个C结构的声明:
struct align
{
char c; //1 byte
short s;//2 bytes
};
Run Code Online (Sandbox Code Playgroud)
在我的环境中,sizeof(struct align)为4,填充1字节位于'char c'和'short s'之间.有人说这是因为"short"必须是2字节对齐,所以pading 1字节在'char c'之后.在32位机器上,我知道'int'最好是4字节对齐,以防止2个存储器读周期,因为在CPU和存储器之间的地址总线上发送的地址是4的倍数.但是'short'是2个字节,这个更少超过4个字节,因此其地址可以是4字节单元内的任何字节(最后一个字节除外).
4个地址的倍数 - > | 0 | 1 | 2 | 3 |
我的意思是,'short'可以从0,1或2开始.所有都可以通过1个读取周期检索,不必为0或2.在我的'struct align'情况下,'char c'可以为0 ,'short s'可能是1-2,填充可能是3.
为什么2字节长的"短"必须是2字节对齐的?
谢谢
更新我的环境:gcc版本4.4.7,i686,Intel
我对limits.h中的CHAR_BIT感到困惑.我已经阅读了一些文章,说宏的CHAR_BIT是为了便携性.要在代码中使用宏而不是像8这样的幻数,这是合理的.但limits.h来自glibc-headers,它的值固定为8.如果glibc-headers安装在一个字节超过8位(比如16位)的系统上,编译时错误是什么?'char'被分配8位还是16位?
当我在limits.h中将CHAR_BIT修改为9时,下面的代码仍然打印'8',怎么样?
#include <stdio.h>
#include <limits.h>
int
main(int argc, char **argv)
{
printf("%d\n", CHAR_BIT);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
以下是补充:我已阅读所有回复,但仍不清楚.在实践中,#include <limits.h>并使用CHAR_BIT,我可以遵守.但那是另一回事.在这里,我想知道为什么它会出现这种情况,首先它是glibc /usr/include/limits.h中的固定值'8',当那些具有1字节!= 8位的系统与glibc一起安装时会发生什么; 然后我发现值'8'甚至不是代码使用的实际值,所以'8'表示什么都没有?如果没有使用该值,为什么要将'8'放在那里?
谢谢,
我读这个岗位约time.startTimer声明和定义。
从答案中,time.startTimer的声明src/time/sleep.go
如下:
func startTimer(*runtimeTimer)
Run Code Online (Sandbox Code Playgroud)
其定义src/runtime/time.go如下:
func startTimer(t *timer) {
if raceenabled {
racerelease(unsafe.Pointer(t))
}
addtimer(t)
}
Run Code Online (Sandbox Code Playgroud)
因此,似乎可以在一个.go文件中声明一个函数,然后在另一个.go文件中实现该函数。我尝试了相同的方法,例如,在a.go中声明了一个函数并在b.go中实现了该函数,但是当时,它总是失败go run a.go。这是正确的方法吗?如何声明在另一个.go文件中实现的功能?或中没有import任何内容。Go如何做到?sleep.gotime.go
谢谢
我正在尝试以递归方式编写一个函数来搜索给定目录下的所有目录.
use strict;
use warnings;
sub printDir
{
my $root = shift;
opendir DIR, $root or die "can't open dir $root\n";
my $dir;
while ($dir = readdir DIR)
{
if (-d "$root/$dir" && "$dir" ne '.' && "$dir" ne '..')
{
print "$dir\n";
&printDir ("$root/$dir");
}
}
}
my $root = $ARGV[0];
printDir $root;
Run Code Online (Sandbox Code Playgroud)
如果目录层次结构如下:
A
/ \
B C
|
D
Run Code Online (Sandbox Code Playgroud)
搜索ABD后代码停止,不会搜索C. 代码有什么问题?
谢谢
这是一个Go语法问题,似乎是一个愚蠢的问题,但是我一直在检查Go语言规范,以找到一些正式的单词或定义来定义xxx类型是什么类型,例如,接口类型是什么类型?
例如,我看到这样的词:
接口类型的方法集是其接口。
要么
必须将嵌入字段指定为类型名称T或指向非接口类型名称* T 的指针,并且T本身可能不是指针类型。
要么
考虑具有两种方法的结构类型 T ...
type T struct {
a int
}
....
Run Code Online (Sandbox Code Playgroud)
类型文字像是struct {...}struct类型,Ain
type A struct {...}和Bin type B interface{...}呢?是A结构类型和B接口类型吗?
是的,从上面关于struct type的示例中T,我可以看出,给定类型是struct type或interface type的已定义类型(通过“ type”声明)也是struct或interface type。因此A是struct类型,B也是interface类型。但是,此规则的正式定义在哪里?
对于定义的类型,我只能找到与类型类别有关的以下内容:
类型定义使用与给定类型相同的基础类型和操作创建一个新的独特类型,并将标识符绑定到该类型。
因此,我的理解是,定义的类型是具有给定类型的新的,不同的类型,但是它们属于同一类型类别,例如接口类型或结构类型。仍然没有这样的定义。
perl ×3
go ×2
subroutine ×2
alignment ×1
bit ×1
c ×1
char ×1
declaration ×1
defined ×1
function ×1
glibc ×1
invocation ×1
mmap ×1
offset ×1
page-size ×1
recursion ×1
syntax ×1
undef ×1
while-loop ×1