我的环境是Ubuntu 14 32位.
我分别写了三个名为main.c,foo.c和bar.c的c文件.
代码很简单.
第一个源代码是main.c
#include<stdio.h>
extern void foo();
int main(){
foo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
第二个源代码是foo.c
#include<stdio.h>
void foo(){
printf("Hi,I am foo.");
bar();
}
Run Code Online (Sandbox Code Playgroud)
最后一个是bar.c
#include<stdio.h>
void bar(){
printf("Hi,I am bar.");
}
Run Code Online (Sandbox Code Playgroud)
上面的所有文件都放在名为test的同一个文件夹中.
(它的绝对路径是/ home/jack/Desktop/test)
然后我发出命令:
$ gcc -fPIC -shared -Wl,-soname,libbar.so.1 -o libbar.so.1.0.0 bar.c
$ ln -s libbar.so.1.0.0 libbar.so
$ gcc -fPIC -shared -Wl,-soname,libfoo.so.1 -o libfoo.so.1.0.0 foo.c -lbar -L.
$ ln -s libfoo.so.1.0.0 libfoo.so
$ gcc -c main.c
$ ld -rpath /home/jack/Desktop/test -e main -o main main.o -L. -lfoo …Run Code Online (Sandbox Code Playgroud) 我试图获得数组大小,但我收到一个错误.我的代码是:
#include <iostream>
#include <array>
using namespace std;
int main ()
{
int myarray[5];
cout << "size of array: " << myarray.size() << endl;
cout << "sizeof array: " << sizeof(myarray) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
错误:在'myarray'中请求成员'size',这是非类型'int [5]'|
我对Linux非常熟悉(我已经使用它2年了,1年没用Windows了),我终于深入研究了内核编程,我正在开发一个项目.所以我的问题是:
我了解到,当对象超出范围时,就会调用析构函数,并且析构函数也会删除对象。好吧,但是这里发生了什么?
我显式调用析构函数,如果它删除了对象,那么为什么隐式调用析构函数?即使现在没有对象,因为它已经通过显式析构函数调用被删除。抱歉,我可能明确和隐含地错了,但请尝试理解我的问题。
#include <iostream>
using namespace std;
class A{
public:
A(){
cout << "Constructor" << endl;
}
~A(){
cout << "Destructor" << endl;
}
};
int main(){
A obj;
obj.~A();
cout << "End" << endl;
}
Run Code Online (Sandbox Code Playgroud)
现在
obj.~A();
Run Code Online (Sandbox Code Playgroud)
上面的行删除了该对象。那么为什么又要调用析构函数呢?尽管那里没有任何物体。
输出:
Constructor
Destructor
End
Destructor
Run Code Online (Sandbox Code Playgroud)