使用向量,可以假设元素连续存储在内存中,允许范围[&vec [0],&vec [vec.capacity())用作普通数组.例如,
vector<char> buf;
buf.reserve(N);
int M = read(fd, &buf[0], N);
Run Code Online (Sandbox Code Playgroud)
但现在向量不知道它包含M个字节的数据,由read()外部添加.我知道vector :: resize()设置了大小,但它也清除了数据,因此在read()调用之后它不能用于更新大小.
是否有一种简单的方法可以将数据直接读入矢量并在之后更新大小?是的,我知道明显的解决方法,比如使用一个小数组作为临时读缓冲区,并使用vector :: insert()将它附加到向量的末尾:
char tmp[N];
int M = read(fd, tmp, N);
buf.insert(buf.end(), tmp, tmp + M)
Run Code Online (Sandbox Code Playgroud)
这是有效的(这就是我今天正在做的事情),但是如果我可以将数据直接放入向量中,那么在那里会有一个额外的复制操作,这是困扰我的.
那么,在外部添加数据时,是否有一种简单的方法来修改矢量大小?
我找到的每一篇文档(参考文献1到5)都讨论了如何使用共享的UNC路径设置符号服务器,然后将正确的设置提供给本地调试器实例(无论是_NT_SYMBOL_PATH还是Visual Studio IDE调试设置) ).
Microsoft提供了一个符号服务器(参考6),可通过http获取其公共符号存储.
我想为我自己的代码创建一个可通过http传输访问的符号服务器,而不是通过UNC文件共享.Mozilla人似乎已经这样做了(参考文献7),但它不再具有功能性.
到目前为止,是否有更好的参考可用于执行此任务?
参考
我正在调试我的程序,我注意到即使我已将几乎全部标记为注释,我所做的只是将双值推入向量,我有内存泄漏.我在c ++参考中读过api,但找不到任何东西.这是代码:
#include <vector>
#include <cstdlib>
#include <iostream>
#include "RegMatrix.h"
#include "Matrix.h"
using namespace std;
int main(void)
{
vector<double> v;
for (int i=0; i<9; i++)
{
v.push_back(i);
}
cout << endl;
exit(EXIT_SUCCESS);
}
Run Code Online (Sandbox Code Playgroud)
和valgrind的报告:
==9299== HEAP SUMMARY:
==9299== in use at exit: 128 bytes in 1 blocks
==9299== total heap usage: 5 allocs, 4 frees, 248 bytes allocated
==9299==
==9299== 128 bytes in 1 blocks are still reachable in loss record 1 of 1
==9299== at 0x402569A: operator new(unsigned int) …Run Code Online (Sandbox Code Playgroud) 我无法理解为什么addr被长时间类型化,然后用表达式补充..基本上涉及peekAddr计算的整行
void *addr;
char *peekAddr ;
peekAddr = (char *) ((long)addr & ~(sizeof(long) - 1 ) ) ;
peekWord = ptrace( PTRACE_PEEKDATA, pid, peekAddr, NULL ) ;
Run Code Online (Sandbox Code Playgroud) 这是我的功能.width和height变量是在此函数上定义的全局整数变量,其值为数百.
#define ORIGINAL_WIDTH 800;
#define ORIGINAL_HEIGHT 700;
void set_perspective(void) {
int view_width, view_height;
if (width < height) {
view_width = width;
view_height = (float) width * ORIGINAL_HEIGHT / ORIGINAL_WIDTH;
}
else {
view_width = (float) height * ORIGINAL_WIDTH / ORIGINAL_HEIGHT;
view_height = height;
}
}
Run Code Online (Sandbox Code Playgroud)
我的C++编译器注意到"错误C2143:语法错误:缺少';' 在'/'之前:
view_height = (float) width * ORIGINAL_HEIGHT / ORIGINAL_WIDTH;
and
view_width = (float) height * ORIGINAL_WIDTH / ORIGINAL_HEIGHT;
Run Code Online (Sandbox Code Playgroud)
这与铸造有关吗?为什么在某处丢失分号?感谢您的时间.
请帮忙,
问题:以下代码中的核心转储:
我有一个抽象类 SomeOtherClass,并从它派生了 SomeOtherClassImpl。
这是导致问题的代码:
class MyClass
{
public:
void someFunction()
{
myVector().push_back(someOtherClassDefault());
}
private:
static std::vector<SomeOtherClass const *> & myVector()
{
static std::vector<SomeOtherClass const *> theVector;
return theVector;
}
static SomeOtherClass const * someOtherClassDefault()
{
static SomeOtherClassImpl theDefault;
return &theDefault;
}
};
Run Code Online (Sandbox Code Playgroud)
我在其他翻译单元中有一些 MyClass 类型的静态变量。
这个问题很奇怪,因为程序退出时会发生分段错误。当然 theDefault 可以在 theVector 之前被释放,但是有什么区别呢?当 main 已经完成时,两者都被解除分配。
您的帮助将不胜感激。