I don't understand this assignment...

A

Anon Email

In the code below, what does this mean?

mine = (short *)0;


--------------
#include <iostream>

int main()
{
typedef short Example;

short *mine;
mine = (short *)0;
if(mine)
{
delete [] mine;
}
mine = new short[10];

for (int i = 0; i < 10; ++i)
mine = 0;

for (int i = 0; i < 10; ++i)
std::cout << mine
<< " short"
<< std::endl;

}
 
C

Cy Edmunds

Anon Email said:
In the code below, what does this mean?

mine = (short *)0;

That means to cast the zero to a short pointer and assign it to mine. The
cast is not necessary, by the way. There are many more mysteries in the code
that follows.
--------------
#include <iostream>

int main()
{
typedef short Example;

OK, but not used.
short *mine;
mine = (short *)0;
if(mine)

This means if mine is NOT 0. But of course we just set it to 0, so why
bother?
{
delete [] mine;
}
mine = new short[10];

The 7 lines above could have been written

short *mine = new short[10];

Of course it would have been better to avoid "new" altogether and just write

short mine[10];

That way we can't have a memory leak as we do in this code.
for (int i = 0; i < 10; ++i)
mine = 0;

for (int i = 0; i < 10; ++i)
std::cout << mine
<< " short"
<< std::endl;

}

------------

Cheers,

Deets


You called this an "assignment" ... did your instructor write this?
<shudder>
 
J

John Harrison

Anon Email said:
In the code below, what does this mean?

mine = (short *)0;

0 is one way of writing the null pointer. Have you heard of that? If not
look it up in your favourite C++ book.

The cast is unnecessary and stylistically wrong.

mine = 0;

is prefectly good.

john
 
D

David White

Cy Edmunds said:
You called this an "assignment" ... did your instructor write this?
<shudder>

This is an assignment regardless of who wrote it:
mine = (short *)0;

DW
 
A

Anon Email

I assign to myself assignments which investigate unusual C++
assignments.

Huh?

Seriously, I was considering the null pointer assignment discussed,
not a homework assignment! I probably unintentionally attracted a bit
of attention in this forum, as the title of my post sounds like a plea
for help with course work! (Sorry about that.) But I am in fact my own
instructor <shudder!> :)

Anyway, lets take the following code:

short *mine;
mine = (short *)0;
mine = new short[10];

So, in summary, it's possible to write these three lines like this?

short mine[10];

Cheers,

Deets

P.S. Where's the memory leak?
 
T

Thomas Matthews

Anon said:
I assign to myself assignments which investigate unusual C++
assignments.

Huh?

Seriously, I was considering the null pointer assignment discussed,
not a homework assignment! I probably unintentionally attracted a bit
of attention in this forum, as the title of my post sounds like a plea
for help with course work! (Sorry about that.) But I am in fact my own
instructor <shudder!> :)

Anyway, lets take the following code:

short *mine;
mine = (short *)0;
mine = new short[10];

So, in summary, it's possible to write these three lines like this?

short mine[10];

Cheers,

Deets

P.S. Where's the memory leak?

There is a memory leak if:
1. You allocate memory and never delete/free it.
2. The location of the allocated memory is lost:
short * mine;
short * yours;
mine = new short[20];
yours = new short[30];
mine = new short [10]; // this causes leak.
3. Variables declared without using dynamic
memory will be destroyed automatically by
the program; the programmer doesn't have to
remember to destroy them.
4. Using delete instead of delete[] will also
create leaks in memory.

There is a section about this in the C++ FAQ:
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html
One should consult the FAQ before posting.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
J

John Harrison

Anon Email said:
I assign to myself assignments which investigate unusual C++
assignments.

Huh?

Seriously, I was considering the null pointer assignment discussed,
not a homework assignment! I probably unintentionally attracted a bit
of attention in this forum, as the title of my post sounds like a plea
for help with course work! (Sorry about that.) But I am in fact my own
instructor <shudder!> :)

Anyway, lets take the following code:

short *mine;
mine = (short *)0;
mine = new short[10];

So, in summary, it's possible to write these three lines like this?

short mine[10];

The major difference between these two is the lifetime of the memory
allocated. In the first example the memory pointed to by mine is only
deallocated when you explicitly say delete[] using the address of the memory
allocated. In the second example the memory is deallocated when the scope
containing the declaration mine is exited.

{
short * mine1 = new short[10];
short mine2[10];
} // end of scope

At the point labelled end of scope mine1 and mine2 no longer exist, but the
memory that was pointed to by mine1 still exists but is no longer accessible
(a memory leak).
Cheers,

Deets

P.S. Where's the memory leak?

In the first example when you fail to use delete[], in the second example
there can be no memory leak.

john
 
K

Karl Heinz Buchegger

Anon said:
Anyway, lets take the following code:

short *mine;
mine = (short *)0;
mine = new short[10];

So, in summary, it's possible to write these three lines like this?

short mine[10];

They do different things, but in the end: yes you can.

The first does:
create a pointer, called mine.
assign the value 0 to that pointer.
allocate memory for 10 short integers and assign the starting address
of that memory to mine.

The second does:
create an array of 10 integers
P.S. Where's the memory leak?

Anything allocated with new has to be freed with delete.
Anything allocated with new[] has to be freed with delete [].

Now look in your posted example. You use new[], but where is
the corresponding delete[] ?

PS: I hope you use a good textbook.
 
A

Anon Email

Thanks guys. That has really helped.

To Karl: Yes, I'm using a good C++ book - Bjarne Stroustrup's "The C++
Programming Language". I normally only post when I've researched the
material on the Internet or in a book first. If I don't understand,
then I post. To be honest, I have difficulties reading - my eyes get
tired very easily. I also find that I understand topics better if I
talk about them - that's why I like this forum so much.

Anyway, thanks once more. I'll try to be more thorough in my research
before posting.

Cheers,

Deets
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top