相关疑难解决方法(0)

std :: array <>是否只保证堆栈的分配?

std::array<int,10>(没有我自己使用new)保证在堆栈中分配,而不是由C++ - Standard分配?

要清楚,我不是故意的new std::array<int, 10>.我主要想知道,如果允许标准库new在其实现中使用.

c++ arrays stack static-allocation dynamic-allocation

30
推荐指数
2
解决办法
9103
查看次数

Valgrind错误:在退出时使用:72,704字节C++初始化列表奇怪与char*

问题:

我有一个奇怪的问题,我没想到.我有一个名为Answers的类,在标题内是这样的:

class Answer
{
    char* aText;
    bool b_correct;
public:
    Answer():aText(0){;}  //default constructor
}
Run Code Online (Sandbox Code Playgroud)

主(测试)驱动程序代码是这样的:

int main(void) 
{

    static const unsigned int MAX_ANSWERS = 5;
    Answer answers[MAX_ANSWERS];
}
Run Code Online (Sandbox Code Playgroud)

我得到的(意外)怪异是发生了一个分配,我还没有在我的代码中使用任何新的东西.我猜测char*在初始化列表中调用它.

我使用valgrind来测试我的代码,我得到11个allocs和10个frees.当我删除初始化器时:aText(0),额外的alloc消失了.

我知道这是构造糟糕的代码.我正在学习如何用C++编写课程大纲.有人可以帮我理解内存的分配方式或初始化列表中发生的事情,以便调用新的内容吗?

我知道错误来自显示的代码.我知道额外的alloc正在发生当我编译并运行这个代码时.

Valgrind输出:

==12598== Memcheck, a memory error detector
==12598== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==12598== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==12598== Command: ./Answers
==12598== 
==12598== 
==12598== HEAP SUMMARY:
==12598==     in use at exit: 72,704 bytes in …
Run Code Online (Sandbox Code Playgroud)

c++ initializer-list initialization-list

7
推荐指数
1
解决办法
7035
查看次数