C/C++之malloc/new分配struct结构体
作者:互联网
#include<iostream>
#include<stack>
#include<string.h>
using namespace std;
struct ListNode{
int m_nValue;
ListNode * m_pNext;
char buf[64];
};
int main(){
ListNode *node = nullptr;
//way 1:
//node = (ListNode*)malloc(sizeof(ListNode));
//way 2:
//node = new ListNode();
//way 3:
node = new ListNode;
node->m_nValue = 55;
memcpy(node->buf,"Hello World",11);
printf("m_nValue = %d, buf = %s\n",node->m_nValue,node->buf);
delete node;
//or free
//free(node);
return 0;
}
标签:node,malloc,ListNode,struct,nValue,C++,way,new,buf 来源: https://blog.csdn.net/u010164190/article/details/123218441