我有一些奇怪的自修改代码,但它的根本是一个非常简单的问题:我希望能够执行一个jmp(或一个call),然后从该任意点抛出异常并让它被try/catch捕获包含jmp/的块call.
但是当我这样做时(在gcc 4.4.1 x86_64中),terminate()如果从try/catch外部抛出异常,异常会导致异常.我真的没有看到这与从一些偏远的库中抛出异常有什么不同,但显然是因为它不起作用.
如何执行jmp或call仍然将异常抛回原始的try/catch?为什么这个try/catch不会继续处理这些异常,就像正常调用函数一样?
代码:
#include <iostream>
#include <stdexcept>
using namespace std;
void thrower()
{
cout << "Inside thrower" << endl;
throw runtime_error("some exception");
}
int main()
{
cout << "Top of main" << endl;
try {
asm volatile (
"jmp *%0" // same thing happens with a call instead of a jmp
:
: "r"((long)thrower)
:
);
} catch (exception &e) {
cout << …Run Code Online (Sandbox Code Playgroud) 我试图std::basic_string< char, char_traits<char>, allocator<char> >通过<string>标题定义一个完整的特化,其中typedef'd(以g ++为单位).
问题是,如果我<string>首先包含,g ++会将typedef视为实例化basic_string并给我错误.如果我先做专业,那么我没有问题.
我应该能够在<string>包含后定义我的专业化.我该怎么办才能做到这一点?
我的代码:
#include <bits/localefwd.h>
//#include <string> // <- uncommenting this line causes compilation to fail
namespace std {
template<>
class basic_string< char, char_traits<char>, allocator<char> >
{
public:
int blah() { return 42; }
size_t size() { return 0; }
const char *c_str() { return ""; }
void reserve(int) {}
void clear() {}
};
}
#include <string>
#include <iostream>
int main() {
std::cout …Run Code Online (Sandbox Code Playgroud) 我知道我必须使用:rdtsc.测量的函数是确定性的,但结果远非可重复(我从运行到运行得到5%的振荡).可能的原因是:
你知道其他原因吗?如何消除它们?
有人告诉我设计一个2种变体的画笔程序,一个使用大量空间和小cpu,另一个反之亦然.
这个想法(因为我被告知 - 所以不确定)是以某种方式保存屏幕快照而不是保存XOR地图(我不知道它意味着什么)代表绘画之间的差异.
有人可以建议一种方式或添加相关材料的链接?
比方说,我有以下代码(纯例):
class a {
int * p;
public:
a() {
p = new int;
}
~a() {
delete p;
}
};
a * returnnew() {
a retval;
return(&retval);
}
int main() {
a * foo = returnnew();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在returnnew()中,在函数返回后(当retval超出范围时)会对retval进行破坏吗?或者它会在我返回地址后禁用自动销毁,我可以说删除foo; 在main()的末尾?或者,以类似的方式(伪代码):
void foo(void* arg) {
bar = (a*)arg;
//do stuff
exit_thread();
}
int main() {
while(true) {
a asdf;
create_thread(foo, (void*)&asdf);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
析构函数会去哪里?在哪里我要说删除?或者这是未定义的行为?唯一可行的解决方案是使用STL引用计数指针吗?这将如何实施?
谢谢 - 我已经使用了C++一段时间但从未遇到过这种情况,并且不想创建内存泄漏.
下面的程序在父类和子类中包含两个show()函数,但是第一个show()函数接受FLOAT参数,第二个show()函数接受INT参数.
.如果我通过传递float参数调用show(10.1234)函数,它应该调用类A的show(float a)函数,但它调用类B的show(int b).
#include<iostream>
using namespace std;
class A{
float a;
public:
void show(float a)
{
this->a = a;
cout<<"\n A's show() function called : "<<this->a<<endl;
}
};
class B : public A{
int b;
public:
void show(int b)
{
this->b = b;
cout<<"\n B's show() function called : "<<this->b<<endl;
}
};
int main()
{
float i=10.1234;
B Bobject;
Bobject.show((float) i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
B's show() function called : 10
Run Code Online (Sandbox Code Playgroud)
预期产量:
A's show() function called : 10.1234
Run Code Online (Sandbox Code Playgroud)
为什么G …
你好我有这个问题:我想有一个矢量作为类成员.这对我来说可能更容易,我为此道歉.
std::vector<int> *myVector;还是std::vector<int> myVector?它是否正确?
if(myCondition)
{
if(!myVector) //is this correct?
myVector = new std::vector<int>(); //is this correct? on this i have a error
}
Run Code Online (Sandbox Code Playgroud) 我的代码中有一个垃圾数据缓冲区,我将其传递给写入它的函数.我不需要这个垃圾缓冲区的数据.由于我需要在多个地方使用这样的垃圾数据缓冲区,但从不从它们读取,我到处使用相同的垃圾缓冲区.
当一些虚假代码从垃圾数据缓冲区中读取时,这将是致命的,或者,例如,当我使用垃圾数据缓冲区时,我应该使用适当的专用缓冲区.
理想情况下,我想让伪代码快速崩溃,因此我在调用写入函数后对数据进行加扰,因此没有人可以使用(并依赖)垃圾数据.
现在我有了一个想法,我可以将未初始化的数据复制到垃圾数据而不是加扰它,因此valgrind可以从垃圾缓冲区中找到所有读取.
我当前的解决方案是new []一些字节,并将未初始化的内容复制到垃圾缓冲区,然后删除[]未初始化的数据.由于未初始化的数据在大多数情况下都是0(不幸的是),因此我将其与模式进行异或.对于这么简单的事情,这一切似乎都很复杂.
有没有人想出更好的解决方案?
尝试弄清楚如何将构造函数与继承的类一起使用。我知道这是非常错误的,我已经编写 C++ 大约三天了,但无论如何这是我的代码:
clientData.h,两个类,ClientData扩展Entity:
#pragma once
class Entity
{
public:
int x, y, width, height, leftX, rightX, topY, bottomY;
Entity(int x, int y, int width, int height);
~Entity();
};
class ClientData : public Entity
{
public:
ClientData();
~ClientData();
};
Run Code Online (Sandbox Code Playgroud)
和clientData.cpp,其中包含函数:
#include <iostream>
#include "clientData.h"
using namespace std;
Entity::Entity(int x, int y, int width, int height)
{
this->x = x;
this->y = y;
this->width = width;
this->height = height;
this->leftX = x - (width/2);
this->rightX = x + (width/2);
this->topY = …Run Code Online (Sandbox Code Playgroud) 我想在每次循环运行时打印以打印文本,但我的程序只在最后一次循环运行时打印它.我的代码:
while (shots>i){
printf ("%s", message);
i++;
sleep(1);
}
Run Code Online (Sandbox Code Playgroud)
最后,我只是在一瞬间获得所有输出.
我有很多字符串要在php中处理.我想"修复"它们作为标题案例(使用ucwords(strtolower($str))),但前提是它们都是大写或全部小写.如果它们已经是混合的情况,我只是将它们保留原样.
检查这个的最快方法是什么?似乎for通过字符串将是一个相当缓慢的方式来解决它.
这就是我所拥有的,我认为这将太慢:
function fixCase($str)
{
$uc = 0;
$lc = 0;
for($i=0;$i<strlen($str);$i++)
{
if ($str[$i] >= 'a' && $str[$i] <= 'z')
$lc++;
else if ($str[$i] >= 'A' && $str[$i] <= 'Z')
$uc++;
}
if ($uc == 0 || $lc == 0)
{
return ucwords(strtolower($str));
}
}
Run Code Online (Sandbox Code Playgroud) 我改变了代码风格,我希望它能帮助 http://www.cs.bell-labs.com/cm/cs/pearls/spacemod.cpp
#include <iostream>
using namespace std;
#define MEASURE(T, text)
{ \
int i;
cout<<text<<"\t"; \
cout<<sizeof(T)<<"\t"; \
int lastp=0;\
for ( i=0;i<11;i++){ \
T *p=new T; \
int thisp=(int)p; \
if (lastp!=0) cout<<" "<<thisp-lastp; \
lastp=thisp; \
} \
cout<<"n";\
} \
using namespace std;
template <class T>
void measure(char *text)
{
cout<<"measure"<<text<<"\t";
cout<<sizeof(T)<<"\n";
}
int main(){
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它只有一个错误
1>c:\users\david\documents\visual studio 2010\projects\measure\measure.cpp(5): error C2447: '{' : missing function header (old-style formal list?)
Run Code Online (Sandbox Code Playgroud) c++ ×9
aggregation ×1
assembly ×1
benchmarking ×1
c ×1
dealloc ×1
destructor ×1
exception ×1
g++ ×1
gdi ×1
graphics ×1
heap ×1
inheritance ×1
memory-leaks ×1
optimization ×1
performance ×1
php ×1
polymorphism ×1
printf ×1
stl ×1
templates ×1
valgrind ×1
vector ×1