好吧,所以我和其他人有些分歧,我希望有人比我们中的任何一个人都更了解c ++可以解决这个问题.假设我们在函数内部的某个代码块(对于tilemap引擎):
void loadTiles()
{
Tile* tile = new Tile();
Level->addTile(x, y, tile); //x and y are just arbitrary ints.
/* when addTile is called, it fills the values of the chunk of memory pointed to by tile to the predefined chunk of memory created in the Level object. */
//Then, to remove the dangling pointer safely,
tile = NULL;
} //Then the actual memory pointed to by tile is deallocated here.
Run Code Online (Sandbox Code Playgroud)
Level类有一个名为map [] []的2D Tile数组,它的addTile函数看起来完全像这样:
void Level::addTile(int x, int …Run Code Online (Sandbox Code Playgroud) 我正在尝试为项目做这样的事......
template <class N>
class MyClass
{
float properties[N];
};
无论如何在C++中实现这一点?
可能重复:
NSString分配和初始化
我想知道为什么有些对象不需要初始化并分配内存.我读到这个:为什么在使用objective-c之前不需要初始化某些对象?他们说该方法已date初始化并为其分配了内存today.但是,如果我刚刚写了NSString *str = @"Hello";它仍然初始化和分配?
memory memory-management allocation initialization objective-c
在C++中进行内存管理时,我有点新鲜.我读到如果你用new关键字创建一个类,你必须要delete对象释放它的内存.我还读到了在栈上创建的原始类型,例如int,char和bool,这意味着当它们超出范围时会被删除.
但是用new关键字创建的原始类型呢?我需要明确打电话delete吗?这些是在类上创建的吗?或者因为它们是原始的,它们仍然在堆栈上创建?
我问,因为我正在LPTSTR使用new关键字分配一个,但是我担心如果我不调用delete那个内存永远不会被释放.这是我的代码,评论中有一个简单的问题:
#include <Windows.h>
#include <tchar.h>
#include <string>
#ifdef _UNICODE
typedef std::wstring Str;
#else // ANSI
typedef std::string Str;
#endif
Str GetWndStr(HWND hwnd) {
const int length = GetWindowTextLength(hwnd);
if (length != 0) {
LPTSTR buffer = new TCHAR[length + 1]; // Allocation of string
GetWindowText(hwnd, buffer, length + 1);
Str text(buffer);
delete buffer; // <--- Is this line necessary?
return …Run Code Online (Sandbox Code Playgroud) 我有一个关于在c ++中分配的问题.我有这个代码:
vector<unsigned char> &v = *new vector<unsigned char>();
Run Code Online (Sandbox Code Playgroud)
现在的问题是,取消引用对象并将其直接分配给引用通常是一个好主意吗?
在我看来,这使得使用对象变得更容易,因为现在你可以这样做:
v.push_back('a');
v[0];
Run Code Online (Sandbox Code Playgroud)
代替
v->push_back('a');
(*v)[0];
Run Code Online (Sandbox Code Playgroud)
最后,我能做到
delete &v;
Run Code Online (Sandbox Code Playgroud)
释放我的堆
仅仅因为(相同)好的答案的数量:我知道我可以只使用堆栈变量但在我的情况下,我需要它在堆上!但是使用堆或堆栈变量的问题是另一个问题.
所以我保持这个例子简单,特别是没有问我是否应该分配变量.
我是一名新的C开发人员.我已经用Java编程了3年了.我注意到我必须用C做手动内存分配.
但是,当涉及到指针和内存分配时,我仍然不明确.我会说出我理解的内容,如果有人对我要说的话有任何评论,请指正.
所以我要说:
int *a;
Run Code Online (Sandbox Code Playgroud)
所以这样做会创建一个名为a的指针,指向一个整数值.
int *a = address;
Run Code Online (Sandbox Code Playgroud)
如果我打印地址,我会得到地址.
假设我想要价值,我做我们所谓的解除引用和
int *a = *value;
Run Code Online (Sandbox Code Playgroud)
如果我在这种情况下打印值,我会得到存储在内存中的实际数字.
我注意到在我使用的电子书中,有时候,我们在堆中分配内存.总是这样吗?每次我需要使用一个字符串的指针,例如使用char*,我必须使用alloc()和sizeof()分别根据预期类型的大小分配内存?
我试图了解如果一个类创建另一个类,如何回馈内存.
我有
Clas A;
Run Code Online (Sandbox Code Playgroud)
然后另一个为A类分配内存的类:
class B{
private:
A* data;
public:
// Allocating new memory
B (){
A* data = new A();
//giving memory back
~B(){
delete data; };
};
Run Code Online (Sandbox Code Playgroud)
当我在main函数中执行代码时,它就崩溃了.怎么了?我在这里有点迷失.谢谢.
我想有一个数组的长度取决于我的模板的参数,但我不断得到"预期的常量表达式"错误.
enum MyEnum
{
FIRST,
OTHER
};
template<MyEnum e>
struct MyTemplate
{
static const int arrSize;
int myArr[arrSize]; // error C2057: expected constant expression
// int myArr[e == FIRST ? 4 : 10]; // works, but isn't very readable...
};
template<>
const int MyTemplate<FIRST>::arrSize = 4;
template<>
const int MyTemplate<OTHER>::arrSize = 10;
Run Code Online (Sandbox Code Playgroud)
我必须使用的编译器不支持constexpr,或任何其他C++ 11功能,我也不能将数组大小作为模板参数传递.
编辑:我也一定不能用new.
谢谢
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
struct product{
string productName;
float price;
};
int main()
{
struct product *article;
int n=2; // n represent here the number of products
article= (product*) malloc(n * sizeof(product));
for(int i=0;i<n;i++)
{
cin >> article[i].productName; // <=> (article+i)->productName;
cin >> article[i].price;
}
for(int i=0;i<n;i++)
{
cout << article[i].productName <<"\t" << article[i].price << "\n";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么这是错误的,因为当我试图运行它时,我得到了一个分段错误.我使用GDB调试器来查看导致问题的行,这是导致此问题的行:
cin >> article[i].productName;
Run Code Online (Sandbox Code Playgroud)
为什么?这困扰了我好几天......
我在x轴上有n个点.在程序开始时,我正在分配x npoints.例如x = new double[npoints];
在模拟期间npoints可能会有所不同 如果npoints增加我想增加分配的内存.此外,如果npoints减少我想删除减少的内存.