C ++指针片段 [英] C++ Pointer Snippet

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

问题描述

问候大家。这是我的第一个问题这里stackoverflow所以请忍受与我。

Greetings everyone. This is my first question here at stackoverflow so please bear with me.

我的编程类这个学期是Java;上一学期是C ++。我的Java老师感觉(合理地,我认为),我们的学生理解内存管理的机制是非常重要的。

My programming class this semester is Java; last semester was C++. My Java teacher feels (justifiably, I think) that it is very important for we students to understand the mechanics of memory management.

由于Java具有自动垃圾回收功能,因此他认为我们必须在C ++中编写一个非常基本的程序来创建一个二维数组指针。具体来说,我们首先创建一个指针数组;第一个数组中的每个指针都应该引用自己的整数数组。然后我们必须释放与这两个数组相关联的内存。

Since Java has automatic garbage collection, he has seen fit to give us an assignment in which we must write a very basic program in C++ that creates a two-dimensional array using pointers. Specifically, we are to first create an array of pointers; each pointer in the first array should reference its own array of integers. Then we must deallocate the memory associated with the two arrays.

这个赋值应该包含两个函数:一个用于分配二维数组,另一个用于解除分配。我只是想确保下面的代码在逻辑上声音之前,我继续。

This assignment is supposed to consist of two functions: one to allocate the 2-D array, and a second to deallocate. I just want to make sure that the following code is logically sound before I proceed.

我相信这一切看起来很简单,但结论是,我的C ++老师花了两天的指针。虽然我有点理解的机制,我在执行上很无能。话虽如此,下面是一个非常粗糙的开始。这是逻辑上健全吗?我完全脱离了标记?我最深切地感谢任何帮助。

I'm sure this all seems very simple, but the hitch is that my C++ teacher spent all of two days on pointers. While I somewhat understand the mechanics, I am pretty clueless on implementation. That being said, the following is a very rough start. Is this logically sound? Am I completely off the mark? My most profound thanks in advance for any help.

编辑:我创建了一个新的问题与更新的代码。您可以点击此处查看。

I have created a new question with updated code. You can view it by clicking Here.

推荐答案

使用时

int* i = new int;

您与

delete i;

,当您使用

int* i = new int [12];

您与

delete [] i;

如果在新的中有括号,则在删除中应该有方括号。否则,你的程序看起来对我合理,除了你没有实现你的教授想要你的两个功能。

If there are brackets in the new, there should be brackets in the delete. Otherwise, your program looks reasonable to me, except that you haven't implemented those two functions that your prof wants you to.

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

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