C++访问类内的浮点指针 ->访问冲突

发布于 2024-12-28 10:53:45 字数 1127 浏览 3 评论 0原文

Visual C++,Microsoft Visual Studio 2010 Express:

从这个 malloc 访问可以工作:

float* block  = (float *)_aligned_malloc(32 * sizeof(float), CACHE_ALIGNMENT);
block[0] = (float)30;  // I work fine.

但是当它在这个类中时,它不起作用:

class Test
{
    private:
        //static const int numberOfElements = 1024;

    public:
        float* block1;
        float* block2;

    // Constructor
        Test::Test(int nElements)
        {
            float* block1 = (float *)_aligned_malloc(nElements * sizeof(float), CACHE_ALIGNMENT);
            float* block2 = (float *)_aligned_malloc(nElements * sizeof(float), CACHE_ALIGNMENT);
        }
    // Destructor
        Test::~Test(void) 
        {
            _aligned_free(block1);
            _aligned_free(block2);
        }
};

...

Test testClass = Test(32); 
testClass.block1[0] = (float)30;  // Access violation!

...

或者如果声明为指针,则相同的事情(这是我第一次尝试的方式):

Test* testClass = new Test(32);
testClass.block1[0] = (float)30;  // Access violation!

什么是当 float* 在类中定义时,我在访问值方面做错了?问题是别的吗?

Visual C++, Microsoft Visual Studio 2010 Express:

Access from this malloc works:

float* block  = (float *)_aligned_malloc(32 * sizeof(float), CACHE_ALIGNMENT);
block[0] = (float)30;  // I work fine.

But when it is inside this class, it does not work:

class Test
{
    private:
        //static const int numberOfElements = 1024;

    public:
        float* block1;
        float* block2;

    // Constructor
        Test::Test(int nElements)
        {
            float* block1 = (float *)_aligned_malloc(nElements * sizeof(float), CACHE_ALIGNMENT);
            float* block2 = (float *)_aligned_malloc(nElements * sizeof(float), CACHE_ALIGNMENT);
        }
    // Destructor
        Test::~Test(void) 
        {
            _aligned_free(block1);
            _aligned_free(block2);
        }
};

...

Test testClass = Test(32); 
testClass.block1[0] = (float)30;  // Access violation!

...

Or if declared as a pointer, the same thing (this is how I first tried it):

Test* testClass = new Test(32);
testClass.block1[0] = (float)30;  // Access violation!

What am I doing wrong in terms of accessing the values when the float* is define inside a class? Is the problem something else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

却一份温柔 2025-01-04 10:53:45

您分配给构造函数的局部变量,而不是成员变量。尝试类似的操作

    Test::Test(int nElements)
    {
        block1 = (float *)_aligned_malloc(nElements * sizeof(float), CACHE_ALIGNMENT);
        block2 = (float *)_aligned_malloc(nElements * sizeof(float), CACHE_ALIGNMENT);
    }

并检查值以确保分配成功。

You assign to the variables local to constructor, not member variables. Try something like

    Test::Test(int nElements)
    {
        block1 = (float *)_aligned_malloc(nElements * sizeof(float), CACHE_ALIGNMENT);
        block2 = (float *)_aligned_malloc(nElements * sizeof(float), CACHE_ALIGNMENT);
    }

and check the values to make sure allocation succeeded.

寄居人 2025-01-04 10:53:45

构造函数局部变量 block1block2 的声明隐藏了成员变量 block1block2

使用g++,您可以使用-Wshadow选项获得警告。对于 Visual Studio,恐怕没有选项可以在变量遮蔽另一个变量时发出警告。

The declarations of the variables block1 and block2 local to the constructor shadow the member variables block1 and block2.

With g++ you can get a warning for this with -Wshadow option. With Visual Studio I'm afraid there is no option to warn when a variable shadows another one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文