例如,如果我在主应用程序中声明一个函数,并通过一个动态加载的库(通过dlopen
在Linux LoadLibrary
下或在Windows下)使用gotten符号参数(通过dlsym
或GetProcAddress
分别)传递指向它的指针,并尝试调用该函数它会正常工作吗?
如果将指针从一个动态加载的库传递到另 我认为如果指针至少相对于应用程序而不是相对于模块/库,它应该工作.
另一个例子.我在一个应用程序中声明一个函数并将指针传递给另一个完全独立的应用程序(包括C和C++)(参数字符串或文件i/o - idk如何,只是一个想法)并尝试调用此函数,是否会工作呢?如果指针是绝对的,我可以期待它工作.也许它只是不起作用,因为系统不会因为安全而不喜欢这种交叉呼叫?
我将在我的菜单中使用这样的CSS表格:
.menu {text-decoration:underline;}
.menu a:link {text-decoration:none; color:#0202C0}
.menu a:active {text-decoration:none; color:#0202C0}
.menu a:visited {text-decoration:none; color:#0202C0}
.menu a:hover {text-decoration:underline; color:#0099FF}
Run Code Online (Sandbox Code Playgroud)
但在尝试将其应用于文档时
<span class="menu">
Some underlined text came here...
<a href="...">this text should not be underlined until mouse on!</a>
</span>
Run Code Online (Sandbox Code Playgroud)
我发现了意外的行为:链接文本总是保持下划线.我做错了什么?它可能取决于浏览器(我使用的是Mozilla Firefox 3.5.6,可能是IE 6.0正确显示)?如果是这样,我怎么能依赖CSS呢?我应该用什么来替代它?
(事实上,通常我很快就学会了新的编程语言,并且在编写基础之前从未遇到任何问题,直到我开始使用HTML和CSS.要么我与它不兼容,要么它的功能从未得到足够好的回忆.)
如何使用低级 POSIX 函数检查当前写入位置是否位于文件末尾?第一个想法是使用 lseek 和 fstat:
off_t sk;
struct stat st;
sk = lseek (f, 0, SEEK_CUR);
fstat (f, &st);
return st->st_size == sk;
Run Code Online (Sandbox Code Playgroud)
但是,是否st->st_size
反映了实际大小而不是磁盘文件大小,即不包括内核缓冲数据?
另一个想法是使用
off_t scur, send;
scur = lseek (f, 0, SEEK_CUR);
send = lseek (f, 0, SEEK_END);
lseek (f, scur, SEEK_START);
return scur == send;
Run Code Online (Sandbox Code Playgroud)
但这似乎不是快速且充分的方法。
此外,这两种方式似乎都是非原子的,因此如果有另一个进程附加到文件,则可以在检查当前偏移量后更改大小。
局部变量至少(最多)存在于函数内部.但是,块外的块范围变量会发生什么,但它具有相同的功能,我可以保留并使用它们的地址吗?这段代码有效吗?
#include <stdio.h>
int main()
{
char *f;
if (1)
{
char q[] = "123";
f = q;
}
printf ("%s\n", f);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
事实上gcc -ansi -pedantic
,valgrind和valgrind 都没有抱怨,但是我可以使用跨平台和交叉编译吗?在我看来没有,但是什么工具可以告诉我错误?
PS我应该用static
吗?它可能是适当的解决方案,但在我看来,它不是一个线程安全的?
测试程序是
#include <iostream>
using namespace std;
class A
{public:
A (): I(0) {cout << "default construcot" << endl; };
explicit A (int i): I(i) {cout << "another construcot" << endl; };
A (const A& a): I(a.I) {cout << "copy constructor" << endl; }
A& operator = (const A& a)
{cout << "assignment operator" << endl;
if (this == &a) return *this;
I = a.I;
return *this;
}
void show () {cout << I << endl; };
private:
int I;
}; …
Run Code Online (Sandbox Code Playgroud) 我们来看头文件 var.h
#include <iostream>
class var
{public:
var () {std::cout << "Creating var at " << this << std::endl; }
~var () {std::cout << "Deleting var at " << this << std::endl; }
};
Run Code Online (Sandbox Code Playgroud)
和两个源文件,第一个 lib.cpp
#include "var.h"
var A;
Run Code Online (Sandbox Code Playgroud)
第二个 app.cpp
#include "var.h"
var A;
int main ()
{return 0;
}
Run Code Online (Sandbox Code Playgroud)
那么,如果我试图编译它们
g++ -c app.cpp
g++ -c lib.cpp
g++ -o app app.o lib.o
Run Code Online (Sandbox Code Playgroud)
链接器返回多个定义的变量错误.但是,如果我将它编译到共享库+主应用程序
g++ -fPIC -c lib.cpp
g++ --shared -o liblib.so lib.o
g++ -fPIC -c app.cpp
g++ -o …
Run Code Online (Sandbox Code Playgroud) 我在寻找这样的员工.
有一个A类,它有一个B类型的对象作为它的成员.由于我希望B成为其他类的基类,我需要使用指针或对象的引用,而不是它的副本,以正确使用A中的B的虚方法.但是当我写这样的代码
class B
{public:
B(int _i = 1): i(_i) {};
~B()
{i = 0; // just to indicate existence of problem: here maybe something more dangerous, like delete [] operator, as well!
cout << "B destructed!\n";
};
virtual int GetI () const {return i;}; // for example
protected:
int i;
};
class A
{public:
A(const B& _b): b(_b) {}
void ShowI () {cout <<b.GetI()<<'\n';};
private:
const B& b;
};
Run Code Online (Sandbox Code Playgroud)
并以这种方式使用它
B b(1);
A a(b);
a.ShowI(); …
Run Code Online (Sandbox Code Playgroud) 大多数消息来源说,在c#中重载++和 - 运算符导致一次性重载postfix和prefix.但看起来他们的行为仍然不同.
class Counter
{
public Counter(int v = 0)
{
this.v = v;
}
public Counter(Counter c)
{
v = c.v;
}
public int GetValue() { return v; }
public static Counter operator ++(Counter c)
{
c.v++;
return new Counter(c);
}
private int v;
}
class Program
{
public static void Main()
{
Counter c1 = new Counter(1);
Counter c2 = c1++;
Counter c3 = ++c1;
c3++;
System.Console.WriteLine("c1 = {0}", c1.GetValue());
System.Console.WriteLine("c2 = {0}", c2.GetValue());
System.Console.WriteLine("c3 = {0}", …
Run Code Online (Sandbox Code Playgroud) 因为我还没有找到一种正确的方法来链接程序(在c ++中的writteln),如果我要在其中使用插件(链接器警告dlopen在运行时需要glibc共享库).此外,我无法为当前的大多数Linux分发构建动态链接的二进制文件,因此我可以仅分发源代码或尝试分发二进制文件以及我的系统中的共享库ldd
,例如:
libc.so.6
libdl.so.2
libgcc_s.so.1
libm.so.6
libstdc++.so.6
Run Code Online (Sandbox Code Playgroud)
因为我认为要求用户添加更容易 .bash_profile
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<path to program directory>
Run Code Online (Sandbox Code Playgroud)
而不是从头开始编译程序和插件.
所以,首先,我是否正确,我可以自由地重新分发这些库,因为它们是在GNU LGPL下获得许可的?
这样做是否有意义,程序是否会在大多数Linux发行版及其版本中正确运行?