如何在C ++中删除addres指针 [英] How to delete addres pointer in C++

查看:158
本文介绍了如何在C ++中删除addres指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白如何删除地址指针。



但在我的脚本中删除时错误(coba);



例如:

输入:

2 3

2 5

2 6

1 2

3

1 2

输出:

值= 5; < br $>
(5个deletting)

值= 6;



soo(* coba).nilai = 3,6 ..

5是删除..

但我不明白怎么做..

helpp



我尝试过:



i do not understand how to delete addres pointer.

but in my script eror when delete (coba);

example :
Input :
2 3
2 5
2 6
1 2
3
1 2
Output :
Value =5;
(5 deletting)
Value =6;

soo (*coba).nilai = 3 , 6..
5 is delete..
but i do not understand how to do it..
helpp

What I have tried:

#include <iostream>
#include <vector>
 
using namespace std;
 
struct test
{
  int next;
  int previous;
  int value = 0;
};

int main()
{
	test *coba, testt;
	coba = &testt;
	int z;
	int l = 0;
	int perintah;
	int y = 0;
	while(true)
	{
		cout << "Input : ";
		cin >> l;
		if(l == 2)
		{
			cin >> (*coba).value;
			*(++coba);
			y++;
		}
		if(l == 1)
		{
			cin >> z;
			for(int i = 0; i < y-z+1; i++)
			{
				(--coba);
			}
			cout << "Value = " << (*coba).value << endl;
			cout << "1.next / 2.previous / 3.erase : ";
			cin >> perintah;
			if(perintah == 1)
			{
				(++coba);
				testt.next = (*coba).value;
				cout << "Next = " << testt.next << endl;
				for(int i = 0; i < y-z; i++)
				{
					(++coba);
				}
			}
			else if(perintah == 2)
			{
				(--coba);
				testt.previous = (*coba).value;
				cout << testt.previous;
				for(int i = 0; i < y-z+2; i++)
				{
					(++coba);
				}
			}
			else if(perintah == 3)
			{
				delete (&coba);
			}
		}
	}
  return 0;
}

推荐答案

正如我昨天告诉你的:带指针的错误C ++结构 [ ^ ]这些不是指针。

As I told you yesterday: Error C++ struct with pointer[^] THOSE AREN'T POINTERS.
struct test
{
  int next;
  int previous;
  int value = 0;
};

next 以前的不能声明为 int - 它们需要top指针值,最好是指向实际目标类型的指针:

next and previous cannot be declared as int - they need top be pointer values and preferably pointers to the actual destination type:

struct test
{
  struct test *next;
  struct test *previous;
  int value = 0;
};

如果你不这样做,那么你会遇到问题。整数是32位值(在现代编译器中),但在64位系统上,指针是64位值:因此,当您设置指向地址的指针时,您需要一个具有足够空间来存储整个64位值的目标,如果你没有那个,那么有些东西会被覆盖......



再加上你的下一个代码就是垃圾!

If you don't, then you will get problems. Integers are 32 bit values (in a modern compiler), but on a 64 bit system pointers are 64 bit values: so when you set a pointer to an address you need a destination that has enough space to store an entire 64 bit value, if you don;t have that, then something is going to get overwritten...

To add to that, your "next" code is total garbage!

test *coba, testt;
	coba = &testt;
...
	(++coba);
	testt.next = (*coba).value;

不,你不能这样做 - 你正在使用你的避难所你分配了使用++的呜呜声,所以你将再次开始破坏内存。最后一行的NAd?您希望该怎么做?



停止猜测。阅读课程笔记,开始思考。希望和祈祷不是一个可行的设计策略......

No, you cant just do that - you are using memory you haven't allocated whine you use ++, so you will again start corrupting memory. NAd that last line? What the heck do you expect that to do?

Stop guessing. Read your course notes, and start thinking. Hope and pray is not a viable design strategy...


这篇关于如何在C ++中删除addres指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆