我知道如何创建一个动态对象数组.
例如,类名是Stock.
Stock *stockArray[4];
for(int i = 0 ; i < 4;i++)
{
stockArray[i] = new Stock();
}
Run Code Online (Sandbox Code Playgroud)
如何将其更改为动态对象的动态数组?
我尝试了什么:
股票StockArrayPointer =新股票[4];
它不起作用,错误是"股票的价值**不能用于初始化股票类型的实体.
第二个问题是在创建动态对象的动态数组之后,访问数组中指针的语法是什么.
现在,我使用stockArray [i] = new Stock(); 这将如何改变?
需要一些指导......
来自C++ Primer 5 Edition的练习使我陷入困境,这就像
练习12.3:这个类是否需要const版本的push_back和 pop_back?如果是这样,请添加它们.如果没有,他们为什么不需要?(第458页)
以下是课程.成员的定义front和back省略以简化代码.
class StrBlob
{
public:
typedef std::vector<std::string>::size_type size_type;
StrBlob();
StrBlob(std::initializer_list<std::string> il);
size_type size() const { return data->size(); }
bool empty() const { return data->empty(); }
// add and remove elements
void push_back(const std::string &t) {data->push_back(t);}
void pop_back();
// element access
std::string& front();
std::string& back();
private:
std::shared_ptr<std::vector<std::string>> data;
// throws msg if data[i] isn't valid
void check(size_type i, const std::string &msg) const;
};
StrBlob::StrBlob(): data(make_shared<vector<string>>()) { …Run Code Online (Sandbox Code Playgroud) 我知道calloc会分配内存并向每个单元写入零,所以我的问题是:使用calloc或malloc与运行向其写入NULL的单元之间有区别吗?calloc的零是否等于NULL?
所以我们有一个构造函数可以根据传递给它的参数抛出异常,但是如果发生这种情况我们不知道如何删除对象.代码的重要部分:
try
{
GameBase *gameptr = GameBase::getGame(argc, argv);
if (gameptr == 0)
{
std::cout << "Correct usage: " << argv[PROGRAM_NAME] << " " << "TicTacToe" << std::endl;
return NO_GAME;
}
else
{
gameptr->play();
}
delete gameptr;
}
catch (error e)
{
if (e == INVALID_DIMENSION)
{
std::cout << "Win condition is larger than the length of the board." << std::endl;
return e;
}
}
catch (...)
{
std::cout << "An exception was caught (probably bad_alloc from new operator)" << std::endl; …Run Code Online (Sandbox Code Playgroud) 我不明白一件事.例如,我声明A类和B类是A的子类:
class A {
public:
int a;
}
class B : public A {
public:
int b;
}
Run Code Online (Sandbox Code Playgroud)
显然,如果我创建A或B的实例,它们在内存中的大小可以由类型确定.
A instanceA; // size of this will probably be the size of int (property a)
B instanceB; // size of this will probably be twice the size of int (properties a and b)
Run Code Online (Sandbox Code Playgroud)
但是如果我创建动态实例然后在以后释放它会怎样?
A * instanceAPointer = new A();
A * instanceBPointer = new B();
Run Code Online (Sandbox Code Playgroud)
这些是不同类的实例,但程序会将它们视为A类的实例.在使用它们时这很好但是如何释放它们呢?为了释放分配的内存,程序必须知道要释放的内存大小,对吧?
所以,如果我写
delete instanceAPointer;
delete isntanceBPointer;
Run Code Online (Sandbox Code Playgroud)
程序如何知道,有多少内存,从每个指针指向的地址开始,它应该是空闲的?因为很明显,对象具有不同的大小,但程序认为它们是A类型.
谢谢
考虑以下C代码:
#include <stdio.h>
#include<stdlib.h>
int main() {
int arrSize;
scanf("%d", &arrSize);
printf("%d\n",arrSize);
int *dynArr = (int *)malloc(sizeof(int)*arrSize);
int arr1[arrSize];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,arrSize是作为用户输入的数组的大小.以下观察结果是否正确:
i)dynArr是一个动态数组,它在运行时从堆部分分配内存.可以使用realloc函数修改dynArr的大小.
ⅱ).arr1也在运行时分配内存,但不是动态的,即它们的大小无法修改.内存是从堆栈或数据部分分配的.(不确定内存分配的堆或堆栈/数据部分以及原因).
如果我将operator delete定义为如下,并且如果对象构造函数抛出新表达式,我希望看到调用定义的运算符的结果delete:
#include <new>
#include <cstdlib>
#include <iostream>
void*
operator new(std::size_t s){
std::cout << "alloc " << std::endl;
return std::malloc(s);
}
void
operator delete(void* p) noexcept {
std::cout << "dealloc " << std::endl;
std::free(p);
}
void
operator delete(void* p,std::size_t) noexcept{
std::free(p);
std::cout << "dealloc s" << std::endl;
}
struct A{
A(int i){
if(i>0)
throw 10;
}
};
int main(int argc// will equal 10
,char* arg[])
{
for(int i=0;i<argc;++i)
auto p=new A{argc};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个程序刚输出alloc,为什么不调用operator delete?在标准[expr.new]中指定:
如果上述对象初始化的任何部分通过抛出异常终止并且可以找到合适的释放函数,则调用释放函数以释放构造对象的内存,之后异常继续在上下文中传播.新表达的.
我已经在以下代码上工作了一段时间,当我尝试为结构及其元素分配/取消分配内存时出现了问题。感谢您对问题的任何见解。查看错误消息后,我认为问题出在以下事实:我试图释放一个我没有适当分配内存的元素,但是在查看代码时这对我来说并不明显。我还尝试了一些代码,在这些代码中我没有为结构的每个元素分别分配内存,但是效果并不理想。
typedef struct {
char *cnet;
char *email;
char *fname;
char *lname;
char *tel;
} vcard;
vcard *vcard_new(char *cnet, char *email, char *fname, char *lname, char *tel)
{
vcard* new = (vcard*)malloc(sizeof(vcard));
printf("%lu\n", sizeof(new->tel) );
new->cnet = malloc(sizeof(new->cnet));
new->email = malloc(sizeof(new->email));
new->fname = malloc(sizeof(new->fname));
new->lname = malloc(sizeof(new->lname));
new->tel = malloc(sizeof(new->tel));
new->cnet = cnet;
new->email = email;
new->fname = fname;
new->lname = lname;
new->tel = tel;
return new;
}
/* vcard_free : free vcard and the strings it points to
*/ …Run Code Online (Sandbox Code Playgroud) 我写了一个简单的例子:
#include <iostream>
int main() {
void* byte1 = ::operator new(1);
void* byte2 = ::operator new(1);
void* byte3 = malloc(1);
std::cout << "byte1: " << byte1 << std::endl;
std::cout << "byte2: " << byte2 << std::endl;
std::cout << "byte3: " << byte3 << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
运行示例,我得到以下结果:
字节1:0x1f53e70
字节2:0x1f53e90
字节3:0x1f53eb0
每次我分配一个内存字节时,它总是16个字节对齐。为什么会这样?
我在GCC 5.4.0和GCC 7.4.0上测试了此代码,并得到了相同的结果。
c++ ×7
c ×3
pointers ×2
arrays ×1
calloc ×1
class ×1
const ×1
constructor ×1
inheritance ×1
memory ×1
memory-leaks ×1
new-operator ×1
null ×1
object ×1
oop ×1
unique-ptr ×1