C/C++ practice question

J

J

Dear Advanced users,

Here are parts of a code sample and I need to find out a few things from
this.
Please tell me your input. I am supposed to get others help as well to make
the answers as accurate as possible.


char * p;

p=test();

char * test() {

char buf[8];

return (char *) buf;

}



1. If this is reasonable or not and why.

2. Would this work at all?

3. Why is this a good practice?

4. How, exactly, could one get a second ¡®char *¡¯ to use back from this
function? In other words, how can this function be modified to return a
¡®char *¡¯ from the function, and an additional ¡®char *¡¯ value in one
function call. Please make sure that your answer will work even if the size
of the char * desired is not known in the outside calling function.



Include statements in called and calling functions.



Thanks in advance..

Joon
 
R

Rolf Magnus

J said:
Dear Advanced users,

Here are parts of a code sample and I need to find out a few things
from this.
Please tell me your input. I am supposed to get others help as well to
make the answers as accurate as possible.


char * p;

p=test();

char * test() {

char buf[8];

return (char *) buf;

}



1. If this is reasonable or not and why.

No. buf will only exist as long as test() is running. As soon as that
function returns, buf ceases to exist. Also, the cast is superfluous
and should be removed. Unneeded casts often increase the risk of making
an error.
2. Would this work at all?
No.

3. Why is this a good practice?

What? Returning pointers to non-static local variables? That's not only
not good practice, it's even illegal in C++, or at least using the
returned pointer is.
4. How, exactly, could one get a second ¡®char *¡¯ to use back from
this function? In other words, how can this function be modified to
return a ¡®char *¡¯ from the function, and an additional ¡®char *¡¯
value in one function call.

I don't understand that.
Please make sure that your answer will work even if the size of the
char * desired is not known in the outside calling function.

The size of a char* is always known in a C++ program. It's a fixed
value. Probably, you meant the size of the buffer that it points to
(actually, it points to the first element of that buffer). Anyway, you
can only know that size outside of the function if you either pass that
size out too or provide some end marker in the buffer. In C style
strings, this is e.g. '\0'.
 
G

Gavin Deane

Sin said:
4. How, exactly, could one get a second ¡®char *¡¯ to use back from this
function? In other words, how can this function be modified to return a
¡®char *¡¯ from the function, and an additional ¡®char *¡¯ value in one
function call. Please make sure that your answer will work even if the size
of the char * desired is not known in the outside calling function.

Eumm. The question isn't exactly clear. One way to interpret this is that we
would want test to return distinct values which might, for example, be
compared later on. This is how it would be done in C++ :

char* p1= test();
char* p2= test();
// use p1 and p2 for whatever
delete p1;
delete p2;

char* test() {
return new char[8];
}


Another way to interpret this is that we want test to return TWO buffers at
the same time, in one function call. One way is to return a char * array. I
haven't used this syntax in over 5 years, so the code might be wrong... But
it'll give you the idea...

char* p[]= test();
// use p[0] and p[1] for whatever

You forgot

delete p[0];
delete p[1];

Which nicely illustrates the dangers of using new anywhere other than
in a constructor.
delete p[];

char*[] test() {
char *p[]= new char*[2];
p[0]= new char[8];
p[1]= new char[8];
return p;
}
 
K

Karl Heinz Buchegger

Eumm. The question isn't exactly clear. One way to interpret this is that we
would want test to return distinct values which might, for example, be
compared later on. This is how it would be done in C++ :

char* p1= test();
char* p2= test();
// use p1 and p2 for whatever
delete p1;

delete [] p1;
delete p2;

delete [] p2;
char* test() {
return new char[8];
}

Another way to interpret this is that we want test to return TWO buffers at
the same time, in one function call. One way is to return a char * array. I
haven't used this syntax in over 5 years, so the code might be wrong... But
it'll give you the idea...

char* p[]= test();
// use p[0] and p[1] for whatever
delete p[];

delete [] p[0];
delete [] p[1];
delete [] p;
char*[] test() {
char *p[]= new char*[2];
p[0]= new char[8];
p[1]= new char[8];
return p;
}

Another way would be to pass the values as parameters which can be modified.
Like this :

char* p1;
char* p2;
test(&p1, &p2);
// use p1 & p2 for whatever
delete p1;

delete [] p1;
delete p2;

delete [] p2;
void test(char **p1, char **p2) {
*p1= new char[8];
*p2= new char[8];
}

A nice demonstration, why returning dynamically allocated objects
is a bad idea most of the time. The caller has to know about
the details on how this allocation has been done.
 
K

Karl Heinz Buchegger

Gavin said:
char* p[]= test();
// use p[0] and p[1] for whatever

You forgot

delete p[0];
delete p[1];

Which nicely illustrates the dangers of using new anywhere other than
in a constructor.

:)

delete [] p[0];
delete [] p[1];

Even more arguments why one shouldn't do this.
 
S

Sin

You forgot
delete p[0];
delete p[1];

Which nicely illustrates the dangers of using new anywhere other than
in a constructor.

Right, sorry... This was my mistake, not just rusty memory... :)

Alex.
 

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
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top