void*从内存的角度来看,将整数值转换为或者反之亦然?我的理解是void*一个未指定长度的内存块的地址.
这似乎就像比较苹果和橙子.
int myval = 5;
void* ptr = (void*)myval;
printf("%d",(int)ptr);
Run Code Online (Sandbox Code Playgroud)
我意识到我应该给出使用它的确切上下文.
int main(int argc, char* argv[]) {
long thread; /* Use long in case of a 64-bit system */
pthread_t* thread_handles;
/* Get number of threads from command line */
if (argc != 2) Usage(argv[0]);
thread_count = strtol(argv[1], NULL, 10);
if (thread_count <= 0 || thread_count > MAX_THREADS) Usage(argv[0]);
thread_handles = malloc (thread_count*sizeof(pthread_t));
for (thread = 0; thread < thread_count; thread++)
pthread_create(&thread_handles[thread], NULL, Hello, (void*) thread); …Run Code Online (Sandbox Code Playgroud) 这是一个代码片段,用于计算从http://felix.abecassis.me/2011/09/cpp-getting-started-with-sse/获取的浮点数组中的平方根值
void sse(float* a, int N)
{
// We assume N % 4 == 0.
int nb_iters = N / 4;
__m128* ptr = (__m128*)a;
for (int i = 0; i < nb_iters; ++i, ++ptr, a += 4){
_mm_store_ps(a, _mm_sqrt_ps(*ptr));
}
}
Run Code Online (Sandbox Code Playgroud)
当我解释这个代码时,我看到只使用了一个xmm(xmm0).我假设展开循环会给编译器一个提示,可以使用更多的xmms.我将代码修改为
void sse3(float* a, int N)
{
__m128* ptr = (__m128*)a;
for (int i = 0; i < N; i+=32){
_mm_store_ps(a + i, _mm_sqrt_ps(*ptr));
ptr++;
_mm_store_ps(a + i + 4, _mm_sqrt_ps(*ptr));
ptr++;
_mm_store_ps(a + i + …Run Code Online (Sandbox Code Playgroud) 我正试图建立一个突破性的游戏克隆Qt.我需要弄清楚QGraphicsItem我的球碰撞的类型.例如,如果我将球与墙碰撞,球就会反弹,如果它与砖碰撞,它必须反弹并摧毁砖.为了找出QGraphicsItem它是什么类型,我认为最好的方法可能是覆盖QGraphicsItem::type()(请告诉我,如果这不是正确的方法!).
在下面的代码中,brick.h我将"Brick"设置为Type 3.现在,值3看起来非常麻烦.我更愿意用'#define'声明一些东西
#include <QGraphicsItem>
//should this #define be here?
//#define BRICK_SPRITE 3
class Brick : public QGraphicsItem
{
public:
Brick(const QPixmap& p, QGraphicsScene *scene = 0);
virtual QRectF boundingRect() const;
virtual void paint( QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget );
QPainterPath shape() const;
enum { Type = 3 }; //enum {Type = BRICK_SPRITE}
int type() const { return Type; }
private:
QPixmap pixmap;
};
Run Code Online (Sandbox Code Playgroud)
放置声明'#define BRICK_SPRITE 3'的好位置在哪里?我在项目中有几个其他文件.我应该将所有定义放在单独的头文件中吗?
我的理解是,如果您希望数组对齐,则必须明确指定数组的对齐方式。
但是,我声明的浮点数组似乎总是与 16 字节对齐。
float *ptr1 = new float[1];
cout<<"ptr1: "<<ptr1<<endl;
float *ptr2 = new float[3];
cout<<"ptr2: "<<ptr2<<endl;
float arr1[7];
cout<<"arr1: "<<arr1<<endl;
float arr2[9] __attribute__((aligned(2)));
cout<<"arr2: "<<arr2<<endl;
Run Code Online (Sandbox Code Playgroud)
这是输出
ptr1: 0x13dc010
ptr2: 0x13dc030
arr1: 0x7fff874885c0
arr2: 0x7fff87488590
Run Code Online (Sandbox Code Playgroud)
是否有一个原因?我正在使用 gcc 4.6.3
但是,如果它是指向浮点位置或静态分配的指针,则我看不到它
static float arr3[9] __attribute__((aligned(2)));
cout<<"arr3: "<<arr3<<endl;
float *x;
cout<<"x: "<<x<<endl;
Run Code Online (Sandbox Code Playgroud)
输出:
arr3: 0x4030b2
x: 0x7fff8c7dd9e8
Run Code Online (Sandbox Code Playgroud)
此代码在 x64 上运行。
有没有办法迭代两个容器(一个跟随另一个),而不使用两个for循环.
我的意图是做这样的事情
vector<int> a{ 1,2,3 };
vector<int> b{ 4,5,6 };
auto it = a.begin();
auto end = b.end();
for (; it != end; ++it)
{
if (it == a.end())
{
it = b.begin();
}
// do something with *it
}
Run Code Online (Sandbox Code Playgroud)
打印
1 2 3 4 5 6
Run Code Online (Sandbox Code Playgroud)
(当然它不起作用.解释在这个答案中)
我不想写两个for循环并复制循环内的代码.有没有办法迭代a后跟b一个for循环?
我能想到的唯一的事情或者是拷贝/第二容器移动到第一或合并创建一个新的矢量a和b,然后遍历它.我也不想这样做,因为这意味着昂贵的复制操作.
经过大量的战斗,我在一个像A [2] [3]这样的声明中想出来,A是一个与int(*)[3]兼容的类型.我的理解是A可以用来指向前三个元素,A + 1指向下三个元素.有什么办法可以声明一个指向数组的指针数组.
ptr_array
+----+ +----+----+----+
| | ----> | | | |
| | | 24 | 25 | 26 |
+----+ +----+----+----+
| | | 44 | 45 | 46 |
| | ----> | | | |
+----+ +----+----+----+
Array of pointers two dimensional array of three integers
to array of 3
Run Code Online (Sandbox Code Playgroud)
我如何声明ptr_array,以便这样做
ptr_array[0] = A;
Run Code Online (Sandbox Code Playgroud)
和
ptr_array[1] = A + 1;
Run Code Online (Sandbox Code Playgroud)