Thursday, March 31, 2011

C++ Pointer Snippet

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

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.

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.

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.

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

From stackoverflow
  • Looks correct to me, with one exception - when you delete arrays you need the following syntax:

    delete [] arrayName;
    
    ninj0rc : Many thanks for the help. I will make the change and get to work on the functions. Thanks again.
  • When you use

    int* i = new int;
    

    you pair it with

    delete i;
    

    and when you use

    int* i = new int [12];
    

    you pair it with

    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.

    ninj0rc : Thank you very much for the explanation. I did not want to start writing the functions until I was sure that I could...write them :D
  • You need to use delete [] instead of just delete.

  • It looks sound(logically). By the way - it's ok to run a program and have it fail. :-)

    ninj0rc : Yeah, it's just that I have basically zero experience with pointers. I've known about this site for some time, and it seemed the perfect time to pose a question.
  • Your code looks fine. I think you will like Java alot since pointers are a really hard subject to grasp and like you said Java has Garbage Collection! (Thank God!)

    ninj0rc : I think you're absolutely right :) However, as my professor says, it's important that I understand memory management...I just wish my C++ teacher had spent more time on the subject ><
    Lucas McCoy : Yeah my C++ teacher spent quite some time on the subject of pointers (about 2 weeks). I really came to appreciate that
    ninj0rc : It is certainly the most unintuitive concept I have experienced; however, it is incredibly interesting to say the least.
  • That's a well-asked question.

    I don't think I'd call it a "2-D array": it's an array of arrays, not a 2-D array.

    I think I'd call my index i or iColumn or iRow instead of nCount (your nRows and nColumns are logically const and used as fencepost/high-water-mark values, whereas your nCount is a loop index).

    For bonus points, think about how to test your solution. You can run it to see if it faults ... but the converse, i.e. not faulting, isn't proof that it's correct. What O/S are you running it on? If you're using Linux then a worthwhile test is to run your code using valgrind (because when you run it valgrind can automatically detect a lot of the ways in which it's possible to misuse memory).

0 comments:

Post a Comment