重新阅读这个问题:
C++中"new"和"malloc"和"calloc"有什么区别?
我检查了答案,但没有人回答这个问题:
有几个原因(我可以想到两个).
让最好的浮动到顶部.
我是 C++ 新手,我有一个问题。
假设您x使用以下代码声明了该变量:
MyClass *x = new MyClass();
Run Code Online (Sandbox Code Playgroud)
使用这个变量后,我不再需要它了。
在以下提议中,首选的行动方案是什么?有什么区别?
称呼free(x);
称呼x->~MyClass();
称呼MyClass::~MyClass(p);
称呼delete x;
有人可以帮助我理解这一点吗?
提前致谢。
目的:了解 C++17 引入std::aligned_alloc动态内存管理的动机。
问题:对于 C++ 中的内存分配,std::malloc由于在什么情况下使用 malloc 和/或 new?. 相反,new在低级代码中几乎总是鼓励使用表达式(例如参见注释)。
尽管有这种气馁,我想知道为什么 C++17 引入了std::aligned_alloc它看起来像std::malloc.
C ++ 17(及更高版本)中是否有任何内容new(或其他鼓励使用的等效实用程序,如果有)无法执行?
尝试:我只能找到以下讨论,这仍然与我的问题有很大关系:
我正在读这个问题:
在什么情况下我使用malloc vs new?
有人提出使用malloc的一个原因是你要免费使用.
我想知道:在C++中混合使用免费调用和构造函数初始化是否有效?
即
我能说......么:
my_type *ptr = new my_type;
free(my_type);
Run Code Online (Sandbox Code Playgroud)
这在某种程度上是无效的还是更糟糕的:
my_type *ptr = new my_type;
delete my_type;
Run Code Online (Sandbox Code Playgroud)
除了它不是c ++ ish的事实?
同样,你可以反其道而行之吗?你可以说
my_type *ptr = (my_type *)malloc(sizeof(my_type));
delete my_type;
Run Code Online (Sandbox Code Playgroud)
请合并,如果这是重复,我搜索但没有看到关于malloc/delete/new/free询问的问题.
我想分配2D数组,我正在考虑两种可能性(Arduio上的avr-gcc):
A:
int **arr = new int*[5];
for(int i = 0 ; i < 5 ; i++){
arr[i] = new int[10];
}
Run Code Online (Sandbox Code Playgroud)
B:
int **arr = malloc(5 * sizeof(int *));
for(int i = 0 ; i < 5 ; i++) {
arr [i] = malloc(10* sizeof(int))
}
Run Code Online (Sandbox Code Playgroud)
A和B之间有什么区别吗?编译器会在这两种情况下创建相同的字节代码(arv-gcc)吗?
我正在创建一个结构,其中的字段是堆上的unordered_map.当我使用new时,我可以毫无问题地添加它.但是使用calloc,我得到一个插入错误,因为桶大小为0.我调用reserve后工作正常.
那么,当在结构上调用calloc时,unordered_map构造函数不会运行吗?我很困惑为什么如果它在一个新版本的结构中,它似乎具有非零桶大小.除了召唤预备队之外,还有更好的方法吗?(在这种情况下我不能使用删除,所以我需要坚持使用calloc调用)
很多次,我在C++代码指针中看到了结构的分配;
struct Current{
int amp;
};
Current *cur = new Current();
Run Code Online (Sandbox Code Playgroud)
但是,"Current"是一个结构而不是类,但在C++中受支持.如何使用它来代替C类型分配,如下所示:
Current *cur = (Current*) malloc (sizeof(Current));
Run Code Online (Sandbox Code Playgroud)
这背后的基本概念是什么?我所知道的是,"结构"在C++中被认为是"类".
已知C++中struct-VS-class的概念.查询主要是关于具体的用例,因为两者都是允许的,但是应该优先考虑,为什么?
试图找出链接列表问题.遇到这个基本错误在createLinkList()中头部值不是"NULL".我在这里缺少什么诀窍.这是我的代码.
#include <iostream>
using namespace std;
void createLinkList(struct node**);
void showList();
void insertNode();
struct node{
int data;
struct node * next;
};
int main()
{
struct node* head = NULL;
createLinkList(&head);
cout<<"inside main function \t"<<head<<endl;
return 0;
}
void createLinkList(struct node **head){
int data;
struct node * new_node;
cout<<"creating Link List ..."<<endl;
cout<< "Enter the data to be inserted"<<endl;
cin >> data;
cout<<"inside createLinkList \t"<<head<<endl;
if (head == NULL){
new_node->data=data;
new_node->next=*head;
*head=new_node;
cout<<"Element Added at Head Position"<<endl;
}
else{
cout<<"Element …Run Code Online (Sandbox Code Playgroud) 如果我正在编写100%ANSI C但编译.cpp文件,编译器会自动"优化"malloc和免费调用new和delete吗?鉴于他们的差异,这甚至是否有意义?我不认为这是如何运作的,但我的一位朋友说这就是发生的事情.
我们的研究书中有一个关于对象函数的问题.在c ++中有一个代码,问题是我们要填补空白.代码如下
template <typename Arg, typename Ret>
class FuncObj {
public:
typedef Arg argType;
typedef Ret retType;
virtual Ret operator()(Arg) = 0;
};
class DivideBy : public FuncObj<int, double> {
protected:
int divisor;
public:
DivideBy(int d) {
this->divisor = d;
}
double operator()(int x) {
return x/((double)divisor);
}
};
class Truncate : public FuncObj<double, int> {
public:
int operator()(double x) {
return (int) x;
}
};
template < typename Ftype , typename Gtype >
class Compose : public FuncObj <typename …Run Code Online (Sandbox Code Playgroud) 我的问题可以简要显示为以下示例。
void func(int n){
char *p = (char*)malloc(n);
// some codes
memset(p,0,sizeof(name));
// free(p); // Commenting this line represents that I forget to release the allocated memory.
}
int main(){
// some codes
for (int i; i < Nl; i++){
func(100);
// How can I release the allocated memory of p outside of the func?
}
}
Run Code Online (Sandbox Code Playgroud)
我希望释放已分配的内存,该内存是在该函数之外的函数中分配的。
谢谢你。
我试图PtInRect在 Win32 中使用该函数,它接受一个POINT对象。我有 x 和 y 坐标,但我不知道如何POINT从这些坐标中创建一个。我尝试查找它,但只能找到此文档,其中不包含我想要的信息。
感谢您的帮助。
struct A
{
string st;
};
int main()
{
A *a = (A *)malloc(sizeof(A));
a->st = "print";
cout << a->st;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我使用这种方式并成功编译时,但之后在运行时出现异常。所以,我发现一件事是A *a = new A;代替A *a = (A *)malloc(sizeof(A));。做这种事情哪种方法更好而且不会出错?
运行时内存分配应该怎么做?
c++ ×13
malloc ×7
c ×3
free ×2
new-operator ×2
arduino ×1
avr-gcc ×1
c++11 ×1
c++17 ×1
calloc ×1
class ×1
constructor ×1
destructor ×1
inheritance ×1
linked-list ×1
memory-leaks ×1
pointers ×1
struct ×1
winapi ×1