473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

basic question for pointer

If I have 2D array like:

int **p;

p = new int*[10];

for(int i=0;i<10;i++)
{
p[i] = new int[10];
}

To delete the p, should I use:

for(int i=0;i<10;i++)
{
delete p[i];
}
delete p;

or just:

delete p;

Feb 22 '06 #1
8 1442
On 2006-02-22, kathy <yq*****@yahoo.com> wrote:
If I have 2D array like:

int **p;

p = new int*[10];

for(int i=0;i<10;i++)
{
p[i] = new int[10];
}

To delete the p, should I use:

for(int i=0;i<10;i++)
{
delete p[i];
}
delete p;

or just:

delete p;


The answer is C, none of the above.

delete[] p;

--
Neil Cerutti
Scouts are saving aluminum cans, bottles, and other items to be
recycled. Proceeds will be used to cripple children. --Church
Bulletin Blooper
Feb 22 '06 #2

kathy wrote:
If I have 2D array like:

int **p;

p = new int*[10];

for(int i=0;i<10;i++)
{
p[i] = new int[10];
}
Every new must have a matching delete.
Every new [] must have a matching delete [].

The two are not the same thing. Mixing new with delete [] or mixing new
[] with delete (as you do) is undefined.
To delete the p, should I use:

for(int i=0;i<10;i++)
{
delete p[i];
No, it should be delete [] p[i]; because p[i] points to a dynamically
allocated *array*. The memory pointed to by p[i] was allocated with new
[], not with new.
}
delete p;
Similarly, this should be delete [] p;
or just:

delete p;


No, definitely not that.

That's all well and good for learning how to manage dynamically
allocated 2D arrays, but in real code, if you prefer
std::vector<std::vector<int> >, you'll have all the memory management
done for you, the vector will be much easier to pass around between
functions, and you get exception safety.

Gavin Deane

Feb 22 '06 #3

Neil Cerutti wrote:
On 2006-02-22, kathy <yq*****@yahoo.com> wrote:
If I have 2D array like:

int **p;

p = new int*[10];

for(int i=0;i<10;i++)
{
p[i] = new int[10];
}

To delete the p, should I use:

for(int i=0;i<10;i++)
{
delete p[i];
}
delete p;

or just:

delete p;


The answer is C, none of the above.

delete[] p;


No, it's answer D.

for(int i=0;i<10;i++)
{
delete [] p[i];
}
delete [] p;

Gavin Deane

Feb 22 '06 #4
On 2006-02-22, Gavin Deane <de*********@hotmail.com> wrote:

Neil Cerutti wrote:
On 2006-02-22, kathy <yq*****@yahoo.com> wrote:
> If I have 2D array like:
>
> int **p;
>
> p = new int*[10];
>
> for(int i=0;i<10;i++)
> {
> p[i] = new int[10];
> }
>
> To delete the p, should I use:
>
> for(int i=0;i<10;i++)
> {
> delete p[i];
> }
> delete p;
>
> or just:
>
> delete p;


The answer is C, none of the above.

delete[] p;


No, it's answer D.

for(int i=0;i<10;i++)
{
delete [] p[i];
}
delete [] p;


That's assuming she did more allocations more memory than was shown,
though. She left that part out, so I did, too. ;-)

--
Neil Cerutti
Feb 22 '06 #5

Neil Cerutti wrote:
On 2006-02-22, Gavin Deane <de*********@hotmail.com> wrote:

Neil Cerutti wrote:
The OP's allocation code:
> int **p;
>
> p = new int*[10];
>
> for(int i=0;i<10;i++)
> {
> p[i] = new int[10];
> }
Your deallocation suggestion:
delete[] p;

My deallocation suggestion:
for(int i=0;i<10;i++)
{
delete [] p[i];
}
delete [] p;


That's assuming she did more allocations more memory than was shown,
though. She left that part out, so I did, too. ;-)


I think my suggestion covers all the allocations in the OP's code and
no more, without making any assumptions.

Am I misunderstanding what you are saying?

Gavin Deane

Feb 22 '06 #6
On 2006-02-22, Gavin Deane <de*********@hotmail.com> wrote:

Neil Cerutti wrote:
On 2006-02-22, Gavin Deane <de*********@hotmail.com> wrote:
>
> Neil Cerutti wrote:
The OP's allocation code:
>> > int **p;
>> >
>> > p = new int*[10];
>> >
>> > for(int i=0;i<10;i++)
>> > {
>> > p[i] = new int[10];
>> > }
Your deallocation suggestion:
>> delete[] p;
My deallocation suggestion:
> for(int i=0;i<10;i++)
> {
> delete [] p[i];
> }
> delete [] p;


That's assuming she did more allocations more memory than was shown,
though. She left that part out, so I did, too. ;-)


I think my suggestion covers all the allocations in the OP's code and
no more, without making any assumptions.

Am I misunderstanding what you are saying?


No. I just spaced out. Thanks for the correction.
--
Neil Cerutti
Feb 22 '06 #7
kathy wrote:
[new/delete question]

You should always read the FAQ before posting on a news group.
Your question is answered in the FAQ for this group.

http://www.parashift.com/c++-faq-lite/

Basically, you need to keep in mind the rule:

If you use
p = new type;

then you give back the memory so.
delete p;

If you use
p = new type[n];

then you give back the memory so.
delete[] p;
Socks

Feb 22 '06 #8
kathy posted:
If I have 2D array like:

int **p;

p = new int*[10];

for(int i=0;i<10;i++)
{
p[i] = new int[10];
}

To delete the p, should I use:

for(int i=0;i<10;i++)
{
delete p[i];
}
delete p;

or just:

delete p;

This shows you haven't got a firm understanding of what's going on. I'll try
give you a hand.

Think of a 16-Bit unsigned integer. It looks like the following in memory:

0000 0000 0000 0000
Now think of an "int *". A pointer variable is just like any other variable
-- i.e. it stores data as bits in memory. An "int" variable stores an
itegral value in memory, while an "int*" variable store a memory address
value in memory. On a particular system, a memory address may be 16 bits. So
let's say you define a pointer variable as follows:

int* p = 0;

It will look like this in memory:

0000 0000 0000 0000

If you wanted to give it the memory address, 82, it would look like so in
memory:

0000 0000 0101 0010 (This is the binary value for 82)
If you define an array of pointers, like so:

int* p[3] = {0, 0, 0};
Then they'll be one after the other in memory like this:

----------1st------- ---------2nd-------- ---------3rd--------
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
Let's analyse your code.
Line 1: int **p;
Line 2:
Line 3: p = new int*[10];

Line 1 defines a pointer variable. A pointer variable stores a memory
address. Let's say it's 16 bits on this system. As of yet it has no value
stored in it.

Line 3 is an assignment statement. It takes what you have on the right and
stores it in the variable on the left. On the right hand side, you're
dynamically allocating ten "int *" variables. Think of "new" as a function
that returns a memory address; it returns the address of the memory which it
has allocated. The "new" statement will allocate ten "int"'s somewhere, like
so:

0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000

Then "new" will return the memory address of the first "int*". Let's say
that this memory address is 82. At the end of this assignment statement,
"p" has the value of 82.

When you call "delete", this memory will then be deallocated. All memory
which you allocate should eventually be deallocated, like so:

Line 1: int **p;

Line 2: p = new int*[10];

Line 3: delete [] p;
Note that the value of "p", i.e. 82, has been passed to "delete". Because of
this, "delete" knows which memory to deallocate.

Now let's look at your particular example:

int **p;

p = new int*[10];

for( int i=0; i<10; i++ )
{
p[i] = new int[10];
}
"p" will still have the value of 82. In the code above, you're storing a
memory address in each of the ten pointers. What memory address have you
given them... ? Memory addresses which correspond to newly allocated arrays
of ten integers. As we saw previously, you eventually have to pass the
memory addresses to "delete" so that the memory becomes deallocated. In your
loop, you allocate an array of ten integers. The loop runs 10 times, so
you've allocated ten arrays of ten integers. (Read the previous sentence
again). Therefore you've to call "delete" for each of the ten arrays which
you've allocated. Thus you have to do this:
int **p;

p = new int*[10]; //Allocate once

for( std::size_t i = 0 ; i < 10; ++i )
{
p[i] = new int[10];
//Allocate ten times
}

for( std::size_t i = 0 ; i < 10; ++i )
{
delete [] p[i];
//Deallocate ten times by passing
//it the memory addresses.
}

delete [] p; //Deallocate once
Just remember that a pointer variable stores a number which is a memory
address. If you don't keep note of this number, then you've nothing to pass
to "delete", and then you'll never be able to deallocate the memory you
allocated.

Hypothetically speaking, if you did the following:

int **p;

p = new int*[10]; //Allocate once

for( std::size_t i = 0 ; i < 10; ++i )
{
p[i] = new int[10];
//Allocate ten times
}

delete [] p; //Deallocate once
Then you will be deallocating the original array which you declared, but you
*won't* be deallocating the ten arrays which you declared with your loop.

-Tomás
Feb 22 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

41
by: Psykarrd | last post by:
I am trying to declare a string variable as an array of char's. the code looks like this. char name; then when i try to use the variable it dosn't work, however i am not sure you can use it...
1
by: Bob | last post by:
Try the following code class basic { public: virtual void one()=0; }; class derived1:public basic { public:
6
by: pauldepstein | last post by:
I am reading Grimshaw and Ortega's "C++ and Numerical Methods." They construct a vector class which contains the variable vec, a float* variable where the length of the array (number of...
7
by: Brian Keogh | last post by:
Hi, I'm a student learning TCP/IP networking at University. Although I understand all about TCP/IP Networking in Java I am expected to understand the basics of C with regard to these programs as...
18
by: steve | last post by:
I'm trying to create a structure of three pointers to doubles. For which I have: typedef struct { double *lst_t, *lst_vc, *lst_ic; } last_values; I then need to allocate space for...
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
6
by: kathy | last post by:
I have a pointer: MyClass *p = NULL; p = new MyClass(...); .... delete p; After delete p, does p equal NULL(it is in C++ standard?)? How to decide if p has been deleted?
13
by: usenet | last post by:
How and where can one find out about the basics of VB/Access2003 syntax? I am a died in the wool C/C++/Java Linux/Unix programmer and I am finding it difficult to understand the program format...
4
by: pauldepstein | last post by:
I don't quite understand some code that looks like this int zeronumber = 0; SomeMember.SomeFunction(&zeronumber); I'm almost sure that SomeFunction is a function defined on pointer-to- int,...
0
AHMEDYO
by: AHMEDYO | last post by:
Hi Every one... With this visual Basic 6.0 Code you can handle more event that visual basic Support as Mouse wheel and hover or you can control event before VB IDE Default Windows proc as...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.