C/C++ char pointer question

J

jesper

Don't bother. I can see what you mean. It would be much nicer
however to be factual instead of smug.

Naturally the pointer to the beginning of the memory area must
be preserved and delete and strcpy should work on that pointer, while
work
is being done by another pointer.

I have been participating in this group, for a couple of days now
and I must say this smugnes seems to be the prevailing attitude.
Some seem to really try to explain but many just seem to want to attack
errors.
And so what if this was someones homework? Really. If this person
really wants
to cheat on an exam it will come back and bite him later if he wants to
work in
softwaredevelopment. I can only hope he gets a job for a competing
firm.
 
D

Daniel T.

peter said:
void addparen(char * str)
{
char * out=new char[strlen(str)*3+1];
char *in=str;
while(*in)
{
*(out++) ='(';
*(out++) =*(in++);
*(out++) =')';
}
*out=0;
strcpy(str,out); // this assumes enough memory has been allocated for
the new string
delete [] out;
}

You certainly haven't tested this. There is a memory leak and a far
more severe bug in your program.

/Peter
Please point out the memory leak.
All memory allocated is freed. So... where?

As to the more severe bug, can you please tell me what you mean?

jesper, I posted a test harness earlier. Use it and see what happens.
 
R

roberts.noah

t-tocs said:
I tested the code and it works.
But the interviewer was not completely satisfy with my solution.
He mentioned something about segv error and memory leaks (can't
remember exactly what he said).

This occasionally happens in interviews. Some "lead" is given the job
of testing you for technical ability. Sometimes these leads are just
mediocre programmers that got put in charge at a small firm. They
often get stuck in a rut thinking that they know more than they really
do because they are the "lead" programmer. They ask stupid questions
and when your answer doesn't exactly fit what they would do they say it
is wrong...it's an ego thing.

To get a job you really want to impress the other people anyway. HR
will override tech quite often so if you show you are easy going and
would fit well in the company what the tech guy says doesn't have as
much weight. Sometimes this isn't true, but guaranteed if HR objects
you won't be getting the job no matter what. If tech strongly objects
then you are probably screwed, but a program that the tech thinks might
have bugs shouldn't result in that. If the guy expects you to write
bug free code on the spot like that they are an idiot and you don't
want to work for them anyway.
 
A

Andre Kostur

Don't bother. I can see what you mean. It would be much nicer
however to be factual instead of smug.

Teach a man to fish vs. giving the man a fish. Encourage people to
develop their own debugging skills vs. handing them the answer on a
silver platter.
Naturally the pointer to the beginning of the memory area must
be preserved and delete and strcpy should work on that pointer, while
work
is being done by another pointer.

If this was a "natural" thing to do, why wasn't it in the code? Since
it's not there, then presumably the person writing the code has made an
error.
I have been participating in this group, for a couple of days now
and I must say this smugnes seems to be the prevailing attitude.
Some seem to really try to explain but many just seem to want to attack
errors.

Um... what you perceive as "attacking errors" are really attention to
detail. Newsflash: programming requires a large amount of attention to
detail. Ever heard of an "off-by-one error"? "We" do not want people
to be getting wrong information, so "we" correct errors.
And so what if this was someones homework? Really. If this person
really wants
to cheat on an exam it will come back and bite him later if he wants to
work in
softwaredevelopment. I can only hope he gets a job for a competing
firm.

Two items:
1) How is this really helping the person in their studies if they are
simply handed the answer.
2) Working for a competing firm isn't the problem. Working in _my_ firm
is. I don't want to have to be forever cleaning up after other people
who don't understand what they're doing.
 
D

Daniel T.

Don't bother. I can see what you mean. It would be much nicer
however to be factual instead of smug.

I have been participating in this group, for a couple of days now
and I must say this smugnes seems to be the prevailing attitude.

It may be the medium. It's hard not to sound smug when the person you
are 'talking' to can't hear your tone of voice. We wanted to lead you to
the answer, not just give it to you, and that can come off as smugness
sometimes.

And so what if this was someones homework? Really. If this person
really wants
to cheat on an exam it will come back and bite him later if he wants to
work in
softwaredevelopment. I can only hope he gets a job for a competing
firm.

The problem is, he may get the job that you wanted. By all means, do his
homework, you may learn something in the process, but you don't have to
give him the answer. :) If you want to help him, don't give him the
answer, lead him to it. If you want to hurt him, give him the wrong
answer, or one so obfuscated that he can't learn anything from it.
 
J

jesper

Andre Kostur skrev:
Teach a man to fish vs. giving the man a fish. Encourage people to
develop their own debugging skills vs. handing them the answer on a
silver platter.

What made you think I don't "know how to fish" ? I didn't ask the
question.
If this was a "natural" thing to do, why wasn't it in the code? Since
it's not there, then presumably the person writing the code has made an
error.

Yes. But the person who asked the question would have been better
helped
by you pointing out what misstake I made. I did not ask the question
(I can't say this enough). You don't have to
coax me to learn about pointers. I know how to use them. Fluently.
Um... what you perceive as "attacking errors" are really attention to
detail. Newsflash: programming requires a large amount of attention to
detail. Ever heard of an "off-by-one error"? "We" do not want people
to be getting wrong information, so "we" correct errors.

Ever left out a ;? It's the same error. Basically.

If I had been even the slightest curious about how this is really done
I would not have posted an attempt to *answer* the question. The
problem was
and is rudimetary. Basic. I wrote the code blind because the solution
to the problem
was a simple matter of constructing a string, a task that can be
accomplished in
a myriad of ways. For example you could have used std::string like,
someone pointed
out, you could have constructed literals: {"(a)","(b)".. aso. Silly as
hell but it would work.


If you *had corrected* the error it would have been fine.
You didn't however. You pointed out the existance of one, good for you.
Two items:
1) How is this really helping the person in their studies if they are
simply handed the answer.
2) Working for a competing firm isn't the problem. Working in _my_ firm
is. I don't want to have to be forever cleaning up after other people
who don't understand what they're doing.

Exactly right. My point exactly. He wasn't helped. He fooled himself by
getting
someone to do it for him (IF this was infact homework).
If you find it troublesome to fix errors of beginners maybe you should
test
them before hiring them.. I don't know.... Might be a good idea.


As for the comment about abstraction levels (made by Alf I think for
some reason I can't browse the groups at this moment): It's seldom a
good idea to make your solution too
general. In a real project you will sooner or later drown in overhead.
Experience talking here.
 
J

jesper

Daniel T. skrev:
It may be the medium. It's hard not to sound smug when the person you
are 'talking' to can't hear your tone of voice. We wanted to lead you to
the answer, not just give it to you, and that can come off as smugness
sometimes.

The problem is, he may get the job that you wanted. By all means, do his
homework, you may learn something in the process, but you don't have to
give him the answer. :) If you want to help him, don't give him the
answer, lead him to it. If you want to hurt him, give him the wrong
answer, or one so obfuscated that he can't learn anything from it.
This is a good post. Thank you for talking sense. :) (no smugness
intended)

You are right. We might some day compete about the same job. However I
don't feel threatened by someone who can't construct a string without
asking
for help. And cynisism aside he may be telling us the truth and this is
not homework.
The solution he posted is valid. (Have not tested it in a compiler but
the theory is sound)
So maybe he just actually wanted to know if there is something fishy
about it that he
didn't know. Just maybe... (and if there is I am also intessted in
knowing what it is)
The framework I posted (yes with errors) is very similar to the
solution he
posted as his own. And thus my post, arriving after the fact (due to
real work
getting in the way before I could finish the post at the time i began
to write it)
, became absolutly redundant.
 
J

jesper

TB skrev:
(e-mail address removed) sade:

There is a purpose for the existence of

alt.test
OT:
oh my god... You are just too much.
Might do you some good to be a little more understanding and open to
the fact that not everyone has lived their entire life on the usenet.
I return in kind:
There is a purpose for the existence of your backspace key and all
those cancel buttons
in what ever application you posted that piece of horrendous attitude
from.
 
D

Daniel T.

Daniel T. skrev:

This is a good post. Thank you for talking sense. :) (no smugness
intended)

You are right. We might some day compete about the same job. However I
don't feel threatened by someone who can't construct a string without
asking
for help. And cynisism aside he may be telling us the truth and this is
not homework.
The solution he posted is valid. (Have not tested it in a compiler but
the theory is sound)
So maybe he just actually wanted to know if there is something fishy
about it that he
didn't know. Just maybe... (and if there is I am also intessted in
knowing what it is)
The framework I posted (yes with errors) is very similar to the
solution he
posted as his own. And thus my post, arriving after the fact (due to
real work
getting in the way before I could finish the post at the time i began
to write it)
, became absolutly redundant.

You inadvertently pointed out another great reason not to give the
answer. You may be wrong. ;-)

I try to never give an answer here unless I've actually tested it, too
embarrassing.
 
J

jesper

Daniel T. skrev:
You inadvertently pointed out another great reason not to give the
answer. You may be wrong. ;-)

I try to never give an answer here unless I've actually tested it, too
embarrassing.
hehe :) Yes.. But even a incorrect answer might be helpful if
delivered in a forum where the faults in such an answer is pointed out
and
corrected.
Really, having to be embarrased for being wrong? Is this good? If
instead of
belittleing (spelling? and don't I mean you) people who are wrong we
work
together to answer questions as put to us, in a friendly fashion,
maybe this is a better way?...

I feel sorry for this community if its members acctually has to, as you
state, be
embarrased for being wrong. Everyone is wrong sometimes.
And if anyone thinks I, am embarrased for being wrong? You are
misstaken. :)
I am often wrong. In many areas of life.

(I appologize for my poor spelling and my sometimes horrible english)
 

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

No members online now.

Forum statistics

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

Latest Threads

Top