Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用内存区域作为堆栈空间?_C++_C_Linux_Memory_Stack - Fatal编程技术网

C++ 使用内存区域作为堆栈空间?

C++ 使用内存区域作为堆栈空间?,c++,c,linux,memory,stack,C++,C,Linux,Memory,Stack,在Linux中,是否可以启动一个进程(例如,使用execve),并使其使用特定的内存区域作为堆栈空间 背景: 我有一个C++程序和一个快速分配器,它给了我“快速内存”。我可以将它用于利用堆并在快速内存中创建它们的对象。好的但我也有很多变量存在于堆栈中。我怎样才能让他们也使用快速内存呢 想法:实现一个“程序包装器”,分配快速内存,然后启动实际的主程序,将指针传递到快速内存,程序将其用作堆栈。可能吗 [更新] pthread设置似乎可以工作。使用pthread,您可以使用辅助线程作为程序逻辑,并使用

在Linux中,是否可以启动一个进程(例如,使用
execve
),并使其使用特定的内存区域作为堆栈空间

背景:

<>我有一个C++程序和一个快速分配器,它给了我“快速内存”。我可以将它用于利用堆并在快速内存中创建它们的对象。好的但我也有很多变量存在于堆栈中。我怎样才能让他们也使用快速内存呢

想法:实现一个“程序包装器”,分配快速内存,然后启动实际的主程序,将指针传递到快速内存,程序将其用作堆栈。可能吗

[更新]


pthread设置似乎可以工作。

使用pthread,您可以使用辅助线程作为程序逻辑,并使用
pthread\u attr\u setstack()设置其堆栈地址。

名称
pthread_attr_setstack,pthread_attr_getstack-设置/获取堆栈
线程属性对象中的属性
提要
#包括
int pthread_attr_setstack(pthread_attr_t*attr,
void*stackaddr,size\u t stacksize);
描述
函数的作用是:设置堆栈地址和
引用的线程属性对象的堆栈大小属性
通过attr转换为stackaddr和stacksize中指定的值,
分别地这些属性指定位置和大小
创建的线程应使用的堆栈的
使用线程属性对象attr。
stackaddr应指向buf的最低可寻址字节
调用方分配的stacksize字节数。这个
分配的缓冲区的页面应该是可读的,并且
可写的。

我不明白的是,您希望通过这样做来提高性能(我认为“快速”内存的目的是提高性能)。

太棒了!不确定它是否适用于我的情况,我将尝试一下。您的问题:快速分配器是
cudaHostAlloc
返回用于加速内存传输到GPU的页面锁定内存。因此,如果这起作用,我可以加速堆栈变量的拷贝@弗兰克:有趣。让我们知道它是如何进行的。我真的不认为您的快速分配器可以比堆栈分配更快。通常,堆栈分配每个函数需要几个指令。或者你的意思是内存比系统中其他任何地方的内存都快?@DavidRodríguez dribeas后者!内存是最快的,而不是分配器在有两种不同类型RAM的情况下,您使用的是什么平台?您希望多长时间将堆栈对象(而不是它们引用的内存)传输到GPU?使用该分配器可以加快到GPU的传输速度,但会将页面锁定在内存中,这将对系统的其余部分产生影响(例如,对象的堆栈无法再交换)。是否确实要将其用于所有局部变量?我知道。这只是一个想法。问题是我有很多本地VAR需要转移。速度不是唯一的好处。您还可以获得异步传输。(这就是我想要的。)在我把它们复制到一个连续的缓冲区之前,我发现这个想法很好
NAME
       pthread_attr_setstack,  pthread_attr_getstack  -  set/get stack
       attributes in thread attributes object

SYNOPSIS
       #include <pthread.h>

       int pthread_attr_setstack(pthread_attr_t *attr,
                                 void *stackaddr, size_t stacksize);

DESCRIPTION
       The pthread_attr_setstack() function sets the stack address and
       stack  size attributes of the thread attributes object referred
       to by attr to the values specified in stackaddr and  stacksize,
       respectively.   These  attributes specify the location and size
       of the stack that should be used by a thread  that  is  created
       using the thread attributes object attr.

       stackaddr should point to the lowest addressable byte of a buf‐
       fer of stacksize bytes that was allocated by the  caller.   The
       pages  of  the  allocated  buffer  should  be both readable and
       writable.