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;
}

上一篇:C# 字符与字符串


下一篇:Java_Collection集合