为了这个问题,我将内存描述为一个简单的字节数组,我将讨论堆内存,因为它可以动态分配它.
让我们说我正在实例化一些类,并在堆上创建一个已经分配了一些内存的对象.然后,在创建对象之后,我分配了更多的内存(可能通过实例化另一个类).当然,这意味着使用new和delete关键字.
内存现在看起来像这样:
... byte byte my_object ... my_object byte byte ...
delete my_object;执行时到底发生了什么?是否所有其他记忆都向左移动sizeof(MyClass)?如果是这样,由谁?操作系统?那么当没有操作系统提供虚拟内存时会发生什么?
我正在用C++创建一个Vector2类作为模板,我想将+运算符定义为非成员友元函数,可以简单地添加两个向量.
这是我的Vector2模板类中的朋友声明:
template <class U>
friend Vector2<T> operator+(const Vector2<T> &lhs, const Vector2<T> &rhs);
Run Code Online (Sandbox Code Playgroud)
它包含在一个.hpp文件中,但实现在一个单独的.cpp文件中:
template <class T>
Vector2<T> operator+(const Vector2<T> &lhs, const Vector2<T> &rhs)
{
return Vector2<T>(lhs.x_ + rhs.x_, lhs.y_ + rhs.y_);
}
Run Code Online (Sandbox Code Playgroud)
这没有任何警告编译,但它似乎不起作用.
Vector2<int> v1(4, 3);
Vector2<int> v2(3, 4);
Vector2<int> v3 = v1 + v2;
Run Code Online (Sandbox Code Playgroud)
当我尝试编译上面的代码片段时,GCC抱怨:
prog.cpp: In function ‘int main(int, char**)’:
prog.cpp:26:28: error: no match for ‘operator+’ in ‘v1 + v2’
source/vector2.hpp:31:23: note: template<class U> Vector2<int> operator+(const Vector2<int>&, const Vector2<int>&)
source/vector2.hpp:31:23: note: …Run Code Online (Sandbox Code Playgroud) 根据这个答案,其中指出:
编译器知道int类型的大小,因此可以生成正确的汇编程序指令,该指令将在堆栈上保留足够的空间,以便让foo存在.
编译器需要知道函数将在堆栈上占用的大小才能实现它.
那么,为什么这段代码会编译?
int f(int n)
{
int x[n];
}
int main()
{
f(3);
f(5);
//etc
}
Run Code Online (Sandbox Code Playgroud)
x 是一个整数数组,但它的大小不是常量,它可以在调用函数的任何时候改变.
我在这里错过了什么?
void method(double *v)
void method(double v[5])
这两者有什么区别吗?
第二个是更具体的,因为它v被限制为5个元素的长度?
我的理解是,当类型实现的变量Drop超出范围时,将fn drop(&mut self)插入对函数的调用,并将新创建的可变引用传递给超出范围的变量.
但是,如果变量是不可变的,那怎么可能呢?并且可变地借用它是违法的?这是我正在谈论的一个例子:
fn main() {
let x = vec![1, 2, 3];
let y = &mut x;
}
Run Code Online (Sandbox Code Playgroud)
这会产生以下错误:无法将不可变局部变量借用x为可预期的变量.
类似的东西必须在x被丢弃时发生,因为drop期望一个可变的引用.
我正在开发一个程序,它应该在屏幕上绘制一个类似思维导图的对象网络,然后绘制对象之间的连接.线的宽度应代表连接的强度.连接随时间改变,但许多连接被错误地绘制.我100%确定我实际上改变了正确的连接,并且我只是画得很糟糕.
所以,这是我试图绘制它的方式,你能告诉我我做错了什么吗?我该怎么做呢?
for (o = 0; o < self.brain.objects.length; o++)
for (con = 0; con < self.brain.objects[o].connections.length; con++)
{
self.screen.lineWidth = Math.sqrt(self.brain.objects[o].connections[con].weight)*5*self.zoom;
self.screen.beginPath();
self.screen.moveTo((self.brain.objects[o].rect[0] - self.globalPos[0])*self.zoom + (self.brain.objects[o].rect[2]/2)*self.zoom, (self.brain.objects[o].rect[1] - self.globalPos[1] + self.brain.objects[o].rect[3]/2)*self.zoom);
self.screen.lineTo((self.brain.objects[o].connections[con].to.rect[0] - self.globalPos[0] + self.brain.objects[o].connections[con].to.rect[2]/2)*self.zoom, (self.brain.objects[o].connections[con].to.rect[1] - self.globalPos[1] + self.brain.objects[o].connections[con].to.rect[3]/2)*self.zoom);
self.screen.stroke();
}
Run Code Online (Sandbox Code Playgroud) 在原型语言中,对象基本上可以互相克隆.
所以,假设我们有一个构造函数:
Bla = function()
{
this.a = 1;
}
Run Code Online (Sandbox Code Playgroud)
我可以像这样创建该对象的新实例:x = new Bla();.现在,x.a返回1.
如果我要写Bla.prototype.b = 2,那么x.b会返回2.但是,为什么?如果x"克隆"了Bla,为什么我不能这么说Bla.b = 2,没有引用Bla.prototype,仍然可以获得相同的功能?这与this关键字有关吗?
我的任务是为搜索引擎创建一个简单的网络爬虫。现在,爬虫应该如何准确地映射网络?跟随他找到的第一个链接就不再返回,或者使用一些更高级的搜索方法,例如 BFS 或 DFS?
search-engine breadth-first-search web-crawler depth-first-search
根据此答案中给我的建议,我在重力模拟器中实现了龙格-库塔积分器。
然而,在我模拟太阳系一年后,位置仍然偏离了约110 000公里,这是不可接受的。
我的初始数据是由 NASA 的 HORIZONS 系统提供的。通过它,我获得了行星、冥王星、月球、火卫二和火卫一在特定时间点的位置和速度矢量。
这些矢量是三维的,然而,有些人告诉我,当行星在围绕太阳的板块中排列时,我可以忽略三维,所以我这样做了。我只是将 xy 坐标复制到我的文件中。
这是我改进的更新方法的代码:
"""
Measurement units:
[time] = s
[distance] = m
[mass] = kg
[velocity] = ms^-1
[acceleration] = ms^-2
"""
class Uni:
def Fg(self, b1, b2):
"""Returns the gravitational force acting between two bodies as a Vector2."""
a = abs(b1.position.x - b2.position.x) #Distance on the x axis
b = abs(b1.position.y - b2.position.y) #Distance on the y axis
r = math.sqrt(a*a + b*b)
fg = (self.G * b1.m …Run Code Online (Sandbox Code Playgroud) 我正在尝试将Python与Selenium结合使用来编写Firefox脚本,并在其网站上提供示例:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.send_keys("selenium")
elem.send_keys(Keys.RETURN)
assert "Google" in driver.title
driver.close()
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,它成功打开了一个新的Firefox窗口,但是什么也没有发生。浏览器反应灵敏,可以加载其他页面,但是Selenium出于某种原因只是拒绝与其合作。
当我退出Python脚本时,输出如下:
^CTraceback (most recent call last):
File "ha.py", line 5, in <module>
driver = webdriver.Firefox()
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 46, in __init__
self.binary, timeout),
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 46, in __init__
self.binary.launch_browser(self.profile)
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 44, in launch_browser
self._wait_until_connectable()
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 89, in _wait_until_connectable
time.sleep(1)
KeyboardInterrupt
Run Code Online (Sandbox Code Playgroud)
似乎它在函数中进入了无限循环_wait_until_connectable。
似乎是什么引起了这个问题,如何使Selenium使Firefox变得“可连接”?
我浏览了一下firefox_binary.py源代码,发现它在注释中提到了“扩展名”。我应该在Firefox中安装一些扩展以实现交互吗?
c++ ×4
arrays ×2
javascript ×2
memory ×2
oop ×2
pointers ×2
python ×2
algorithm ×1
astronomy ×1
browser ×1
c ×1
canvas ×1
class ×1
firefox ×1
function ×1
gcc ×1
html5 ×1
html5-canvas ×1
integer ×1
low-level ×1
ownership ×1
prototype ×1
runge-kutta ×1
rust ×1
selenium ×1
simulation ×1
templates ×1
types ×1
web-crawler ×1