By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,906 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,906 IT Pros & Developers. It's quick & easy.

please solve my confussion

P: n/a
HP

Hi All
i have confussion regarding given problem
please help me out
4. What happens with the following program:
void main(){
myclass* pmc = new myclass;
pmc = 0;
delete pmc;}

5. What is the difference between the statements
short* p = new short(500);
And
short* p = new short[500];
6. How would you deallocate the memory for :
short* p = new short(500);
And
short* p = new short[500];

7. How is an array of 10 pointers to int allocated from heap

Oct 25 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
HP
is there no body who know these thing

Oct 25 '05 #2

P: n/a
On 24 Oct 2005 22:56:19 -0700, "HP" <hi********@rediffmail.com> wrote:

Hi All
i have confussion regarding given problem
please help me out
4. What happens with the following program:
void main(){
myclass* pmc = new myclass;
pmc = 0;
delete pmc;}

5. What is the difference between the statements
short* p = new short(500);
And
short* p = new short[500];
6. How would you deallocate the memory for :
short* p = new short(500);
And
short* p = new short[500];

7. How is an array of 10 pointers to int allocated from heap

I think this is homework. Try looking to agood C book, i.e.K&R, and
try to work the answers from it.
Oct 25 '05 #3

P: n/a
HP wrote:

is there no body who know these thing


Yes, there is.
But honestly, the problem is:
In another thread you described what you need to do, what your
assignment asks for. Except of one or two things, these assignment
isn't difficult and I would expect a programmer to code 90% of it
with easy. On the other hand, this assignment is extremely difficult
for a non programmer and your questions show, that you need to learn
a lot of things before you can even think about calling yourself
in a programmer. SO i guess what most of us fear is simply: To write
this application for you.

Otherwise:
Get yourself a book and start studying the basics. Programming, especially
C++ programming isn't something you can learn in a day or 2. Reserve a few
month for learning the basics, before you can even think about takling
your assignment problem.

Good luck.

--
Karl Heinz Buchegger
kb******@gascad.at
Oct 25 '05 #4

P: n/a
HP
Hi Zara
Its not a hoework, i m realy confuse in my project work.
i m not able to get answer of this
please help me

Oct 25 '05 #5

P: n/a
HP wrote:

Hi All
i have confussion regarding given problem
please help me out

4. What happens with the following program:
void main(){
myclass* pmc = new myclass;
pmc = 0;
delete pmc;}
you got a memory leak
(and btw: the return type of main is always int)

5. What is the difference between the statements
short* p = new short(500);
And
short* p = new short[500];

the first creates *one* int and intializes it with 500
the second creates an (unniitialzed) array of 500 int
6. How would you deallocate the memory for :
short* p = new short(500);
And
short* p = new short[500];
delete p;
delete [] p;

7. How is an array of 10 pointers to int allocated from heap


int** p = new int* [10];
Get a book on C++ and practice, practice, practice

BTW: if you need all of the above extensively, you are barking
up the wrong tree. Use std::vector, std::list, std::map or whatever
other container fits your bill. That's the C++ way to handle most
of the data structure problems in a program.

--
Karl Heinz Buchegger
kb******@gascad.at
Oct 25 '05 #6

P: n/a
HP
Thankx ZARA

Oct 25 '05 #7

P: n/a
"HP" <hi********@rediffmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...

Hi All
i have confussion regarding given problem
please help me out
If this is your homework (as I strongly suspect), then you're losing out by
not doing it yourself because you probably still won't understand it
afterwards. But since I'm feeling kind...

Stu

P.S. Get a book! :) Take a look at
http://www.accu.org/bookreviews/publ...nner_s_c__.htm to
give you some ideas about what's worth getting.
4. What happens with the following program:
void main(){
myclass* pmc = new myclass;
pmc = 0;
delete pmc;}
It doesn't compile (main always returns int, and myclass isn't declared).
Were the program, on the other hand:

class myclass
{
// something useful here
};

int main()
{
myclass *pmc = new myclass;
pmc = 0;
delete pmc;
return 0; // (ok, you could leave this line out as it happens...)
}

Then it would cause a memory leak, because you allocate a myclass object on
the heap, overwrite the pointer to it (by setting it to 0) and then try and
delete 0, which is a no-op. The object you allocated on the heap is still in
existence, you just don't have a pointer to it any more.
5. What is the difference between the statements
short* p = new short(500);
And
short* p = new short[500];
The first one allocates a single short on the heap, with value 500, and
stores its address in p.
The second one allocates an array of 500 shorts on the heap, and stores the
address of the first element of the array in p.
6. How would you deallocate the memory for :
short* p = new short(500);
And
short* p = new short[500];
delete p; for the first one
delete [] p; for the second one
7. How is an array of 10 pointers to int allocated from heap


int **p = new int*[10];
Oct 25 '05 #8

P: n/a

"Zara" <no***********@terra.es> wrote in message
news:5l********************************@4ax.com...
On 24 Oct 2005 22:56:19 -0700, "HP" <hi********@rediffmail.com> wrote:

Hi All
i have confussion regarding given problem
please help me out
4. What happens with the following program:
void main(){
myclass* pmc = new myclass;
pmc = 0;
delete pmc;}

5. What is the difference between the statements
short* p = new short(500);
And
short* p = new short[500];
6. How would you deallocate the memory for :
short* p = new short(500);
And
short* p = new short[500];

7. How is an array of 10 pointers to int allocated from heap

I think this is homework. Try looking to agood C book, i.e.K&R, and
try to work the answers from it.


It seems that you might need a book or two as well. :-)
Why do you recommend a C book for learning C++?

-Mike
Oct 25 '05 #9

P: n/a

Stuart Golodetz wrote:
"HP" <hi********@rediffmail.com> wrote in message
If this is your homework (as I strongly suspect), then you're losing out by
not doing it yourself because you probably still won't understand it
afterwards. But since I'm feeling kind...


"When a slackard asks comp.lang.c++ to do their homework for them,
answering their question is the worst thing for them. Please don't do
it!"

http://www.parashift.com/c++-faq-lit...t.html#faq-5.3

Oct 25 '05 #10

P: n/a
On Tue, 25 Oct 2005 17:23:35 GMT, "Mike Wahler"
<mk******@mkwahler.net> wrote:

"Zara" <no***********@terra.es> wrote in message
news:5l********************************@4ax.com.. .
On 24 Oct 2005 22:56:19 -0700, "HP" <hi********@rediffmail.com> wrote:

Hi All
i have confussion regarding given problem
please help me out
4. What happens with the following program:
void main(){
myclass* pmc = new myclass;
pmc = 0;
delete pmc;}

5. What is the difference between the statements
short* p = new short(500);
And
short* p = new short[500];
6. How would you deallocate the memory for :
short* p = new short(500);
And
short* p = new short[500];

7. How is an array of 10 pointers to int allocated from heap

I think this is homework. Try looking to agood C book, i.e.K&R, and
try to work the answers from it.


It seems that you might need a book or two as well. :-)
Why do you recommend a C book for learning C++?

-Mike


So sorry! As I read comp.lang.c and comp.lang.c++, I thought It was a
message from the former. Well then, mister OP, try reading K&M instead
of K&R. That is Koenig and Moo, Accelerated C++

Oct 26 '05 #11

P: n/a
<in*****@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...

Stuart Golodetz wrote:
"HP" <hi********@rediffmail.com> wrote in message
If this is your homework (as I strongly suspect), then you're losing out
by
not doing it yourself because you probably still won't understand it
afterwards. But since I'm feeling kind...


"When a slackard asks comp.lang.c++ to do their homework for them,
answering their question is the worst thing for them. Please don't do
it!"

http://www.parashift.com/c++-faq-lit...t.html#faq-5.3


Yes, I have read the FAQ, more than once :) Whilst in theory I shouldn't
have replied if I was following it to the letter, I happen to think it's the
case that there are only two possibilities when you answer this sort of
thing anyway:

(i) They actually read your answer and learn from it (in which case they've
learnt something - sometimes seeing the answer is a good way to learn, you
can't always do some questions on your own if you're having trouble with a
course and a bit of help from others often goes a long way)

(ii) They hand your answer in and get a good mark for it, but fail the final
exam because they don't get it (in which case your contribution hasn't
affected the final outcome, so you're in no more danger of working alongside
someone who doesn't know what they're doing than otherwise)

It's probably bad practice to answer people's exercises, but sometimes it
helps people learn. I'm not doing an assessed exam for them, I'm helping
them with a homework sheet. There's a difference, I think.

Cheers,
Stu
Oct 27 '05 #12

P: n/a

"Stuart Golodetz" <st*************@NnOeSwP.AoMx.ac.uk> wrote in message
news:dj**********@news.ox.ac.uk...
<in*****@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...

Stuart Golodetz wrote:
"HP" <hi********@rediffmail.com> wrote in message
If this is your homework (as I strongly suspect), then you're losing out
by
not doing it yourself because you probably still won't understand it
afterwards. But since I'm feeling kind...


"When a slackard asks comp.lang.c++ to do their homework for them,
answering their question is the worst thing for them. Please don't do
it!"

http://www.parashift.com/c++-faq-lit...t.html#faq-5.3


Yes, I have read the FAQ, more than once :) Whilst in theory I shouldn't
have replied if I was following it to the letter, I happen to think it's
the case that there are only two possibilities when you answer this sort
of thing anyway:

(i) They actually read your answer and learn from it (in which case
they've learnt something - sometimes seeing the answer is a good way to
learn, you can't always do some questions on your own if you're having
trouble with a course and a bit of help from others often goes a long way)

(ii) They hand your answer in and get a good mark for it, but fail the
final exam because they don't get it (in which case your contribution
hasn't affected the final outcome, so you're in no more danger of working
alongside someone who doesn't know what they're doing than otherwise)


Actually, you did effect the final outcome. Because if you don't answer
their question they may just decide to try to figure it out and actually
learn something, reguardless of the grade they get. By answering their
question without them using their own grey matter you may have made it so
they don't learn anything.
Oct 31 '05 #13

P: n/a
"Jim Langston" <ta*******@rocketmail.com> wrote in message
news:f0*****************@fe04.lga...

"Stuart Golodetz" <st*************@NnOeSwP.AoMx.ac.uk> wrote in message
news:dj**********@news.ox.ac.uk...
<in*****@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...

Stuart Golodetz wrote:
"HP" <hi********@rediffmail.com> wrote in message
If this is your homework (as I strongly suspect), then you're losing
out by
not doing it yourself because you probably still won't understand it
afterwards. But since I'm feeling kind...

"When a slackard asks comp.lang.c++ to do their homework for them,
answering their question is the worst thing for them. Please don't do
it!"

http://www.parashift.com/c++-faq-lit...t.html#faq-5.3


Yes, I have read the FAQ, more than once :) Whilst in theory I shouldn't
have replied if I was following it to the letter, I happen to think it's
the case that there are only two possibilities when you answer this sort
of thing anyway:

(i) They actually read your answer and learn from it (in which case
they've learnt something - sometimes seeing the answer is a good way to
learn, you can't always do some questions on your own if you're having
trouble with a course and a bit of help from others often goes a long
way)

(ii) They hand your answer in and get a good mark for it, but fail the
final exam because they don't get it (in which case your contribution
hasn't affected the final outcome, so you're in no more danger of working
alongside someone who doesn't know what they're doing than otherwise)


Actually, you did effect the final outcome. Because if you don't answer
their question they may just decide to try to figure it out and actually
learn something, reguardless of the grade they get. By answering their
question without them using their own grey matter you may have made it so
they don't learn anything.


Perhaps, yes. On the other hand, if they decide to try and figure it out and
fail, they may learn nothing, whereas if they try and understand an answer
they've been given they may learn something. The bottom line in my view is
that if they're serious about learning, you might as well help them, because
even if you have to give them the answer this time they'll read it and
understand it and be able to do it next time (we hope). If they don't try
and understand it and just hand it in, then so be it, since they probably
wouldn't have tried to figure it out on their own anyway if they're not
really interested in the answer.

On a more conciliatory note, I do appreciate why it's not considered a good
idea to give people answers to exercises. But being a student myself I know
that sometimes there's nothing like the answer to help you understand a
problem. If you're interested enough in what you're doing then getting the
answer to a problem you were stuck on can be really useful. Obviously if you
just want the answer so you can have some free time then you're not going to
get much out of being given the answer, but then again you're probably not
getting much out of your course so there's no real harm being done, in a
sense.

Whilst you can certainly say that giving some people the answer will mean
they won't think about a problem, it's also true that some people will still
think about it and, what's more, they'll have more to base their thinking
on. Sometimes you can spend ages banging your head against a brick wall if
you're not really sure what's going on and when you see the answer you can't
understand why you didn't get it before. Having the answer to a question
allows you to see if you can understand how to get from one to the other.
There are lots of different ways of learning, but I personally find worked
solutions tend to help me understand things. (Obviously you have to read
them to get anything out of the whole experience, but just because not
everyone does doesn't mean that providing solutions is necessarily a bad
idea.)

Anyway, this is beginning to get a little off-topic, so I think I'll say for
the record that I respect your viewpoint and other than that let's agree to
disagree because we're just going to go round in circles :) And because I've
just had the horrifying realisation that I've written several paragraphs on
this and I don't think either of us is interested enough in the outcome to
justify that sort of verbiage!

Regards,
Stu
Nov 1 '05 #14

P: n/a
Kev
"Stuart Golodetz" <st*************@NnOeSwP.AoMx.ac.uk> wrote in
news:dk**********@news.ox.ac.uk:
Whilst you can certainly say that giving some people the answer will
mean they won't think about a problem, it's also true that some people
will still think about it and, what's more, they'll have more to base
their thinking on.


Math textbooks often have the answers in the back of the book. Or at least
a good number of them. Nice to have. But doesnt do a whole lot when the
instructors want to see the work ;o)
Nov 1 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.