小编use*_*692的帖子

链表:"node*head = new node"和"node*head"之间的区别

我正在创建一个由用户输入的大小为n的链接列表.当我刚刚初始化标题时,输出是完美的但是当我声明它时输出也有两个零附加.

对于size = 5如果我写node*head = new node; 输出是432100,如果我只写节点*头输出是43210.为什么?

/* I am creating a link list of size n entered by user
 * File:   main.cpp
 * Author: neha
 *
 * Created on February 2, 2014, 12:39 AM
 */

#include <cstdlib>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;

/*
 * 
 */
using namespace std;
struct node{
    int data;
    node* next;
};
node* head=new node; //<--------------here
void PushFront(int value) //Inserting nodes at front of link list
{
    node* newNode= new node; …
Run Code Online (Sandbox Code Playgroud)

c++

5
推荐指数
1
解决办法
1万
查看次数

你能解释一下代码吗?

在堆上创建的对象被分配给堆栈对象.它是某种复制浅拷贝吗?

#include <iostream>

using namespace std;

class Student
{
    private:
        char *name;
};

int main()
{
    Student *s = new Student();
    Student s1 = *s;
    Student s2;

    s2 = s1;
    delete s;

    // it will delete the object s and s1 and s2 are deleted when out of scope?
    //after the curly braces?
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++

-4
推荐指数
2
解决办法
94
查看次数

标签 统计

c++ ×2