473,473 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C/C++ practice question

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


Jul 19 '05 #1
5 5521
J wrote:
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'.

Jul 19 '05 #2
"Sin" <br****@hotmail.com> wrote in message news:<0_*******************@news20.bellglobal.com> ...
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;
}

Jul 19 '05 #3


Sin wrote:
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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #4


Gavin Deane wrote:


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.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #5
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.
Jul 19 '05 #6

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

Similar topics

4
by: Chris | last post by:
I have a question on whether or not this is good practice. I have a fairly complex web user control (a datalist embedded in a datalist with lots of controls) that I will call Usercontrol1 and a...
5
by: martin | last post by:
Hi, Is there any best practice guidelines or examples for threadsafe logging? I have to write an auditlogging class for a website. The auditlog will be stored in files on the local filesystem. A...
17
by: | last post by:
I have an app that retrieves data from an Access database. At the moment I have the SQL string as a Const in my app. I understand this is not best practice. I don't want the user to have access to...
4
by: Guy Noir | last post by:
Hello. Is there a pattern or best practice for the following scenario? I have a list of items I would like to compare. The number of items are decided at runtime. ObjectA, ObjectB,...
6
by: AlexT | last post by:
Folks Please bear with me - I'm not a real ASP pro... I need some advice about the following problem: I have "inherited" of a working ASP site which is hosted on a collocated IIS machine. ...
3
by: Ray | last post by:
OK, maybe I shoot a more general question to the group since there are so many great programmers here: how do you practice your craft? I do it in the following way: 1. Set aside 30 minutes to...
3
by: cbrown | last post by:
I am rebuilding an existing application that relies on an SQL DB. The app is a scheduling/employee management program. My question pertains to best practices in dotnet and database. I use a 3...
3
by: Alan Isaac | last post by:
This is a simple question about actual practice. I just want to know how you (yes you) are approaching this problem. The problem: What is the recommended packaging of demo scripts or test...
4
by: Frankie | last post by:
The .NET Framework provides us with built-in event handlers: System.EventHandler and the generic System.EventArgs<TEventArgs> It appears that those built-in event handler delegates are just a...
5
by: Frank Millman | last post by:
Hi all This is not strictly a Python question, but as I am writing in Python, and as I know there are some XML gurus on this list, I hope it is appropriate here. XML-schemas are used 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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.