Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > return type

Reply
Thread Tools

return type

 
 
starket
Guest
Posts: n/a
 
      05-26-2004
Hi folks,
I'm new to programming, please help,

char * b, q, *r;
b=getbuf();
q = *b;
r= anotherfunction(b);
/* we want to use ‘q' and ‘r' here*/
char * getbuf()
{
char buff[8];
/* unspecified, buff defined here *./
return (char *) buff;
}

1) what's in q?
2) is getbuf() a valid function? will it be executed? what it
returns?
3) How, exactly, could one get a second ‘char *' to use back from this
function or how can this function be modified to return a ‘char *'
(that is, it maintains the same return type) from the function, and an
additional ‘char *' value in one function call?

Many thanks,

starket
 
Reply With Quote
 
 
 
 
Petec
Guest
Posts: n/a
 
      05-26-2004
starket wrote:
> Hi folks,
> I'm new to programming, please help,
>
> char * b, q, *r;
> b=getbuf();
> q = *b;
> r= anotherfunction(b);
> /* we want to use 'q' and 'r' here*/
> char * getbuf()
> {
> char buff[8];
> /* unspecified, buff defined here *./
> return (char *) buff;
> }
>
> 1) what's in q?


Undefined.

> 2) is getbuf() a valid function? will it be executed? what it
> returns?


It's a valid function, but it returns an invalid pointer to automatic
memory.

> 3) How, exactly, could one get a second 'char *' to use back from this
> function or how can this function be modified to return a 'char *'
> (that is, it maintains the same return type) from the function, and an
> additional 'char *' value in one function call?


void getbuf(char* buf)
{
strcpy(buf, "hello, memory management!");
}

int main()
{
char buf[8];
getbuf(buf);
printf("%s\n", buf);
}

Is the generally accepted "safe" way to do it.

- Pete

>
> Many thanks,
>
> starket



 
Reply With Quote
 
 
 
 
ak
Guest
Posts: n/a
 
      05-26-2004
On 26 May 2004 06:24:20 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (starket) wrote:

>>Hi folks,
>>I'm new to programming, please help,
>>
>>char * b, q, *r;
>>b=getbuf();
>>q = *b;
>>r= anotherfunction(b);
>>/* we want to use ‘q' and ‘r' here*/
>>char * getbuf()
>>{
>> char buff[8];
>>/* unspecified, buff defined here *./
>> return (char *) buff;
>>}
>>
>>1) what's in q?


anything - due to invalid getbuf()

>>2) is getbuf() a valid function? will it be executed? what it
>>returns?


nope, it returns a ptr to an object placed in the stack of the
function, when it returns the ptr (buff) doesn't any longer
point to any valid data.

one way to make it valid is to declare your buff[] to static
then the ptr will be valid since it exists between calls to getbuf

char * getbuf()
{
static char buff[8];
...
return buff;
}


>>3) How, exactly, could one get a second ‘char *' to use back from this
>>function or how can this function be modified to return a ‘char *'
>>(that is, it maintains the same return type) from the function, and an
>>additional ‘char *' value in one function call?
>>


you mean getbuf(char * b2 )?

probably best in your to have both as arguments

getbuffers( char *b1, char *b2 )

where you allocate the buffers before calling the function

char b1[8], b2[8];

getbuffers(b1,b2)

and skip the return value altogether.

better is also to pass the buffer sizes to the function
to avoid memory overwrite

getbuffers( char *b1, int lenB1, char *b2, int lenB2 );


/ak

 
Reply With Quote
 
Tomasz Szulist
Guest
Posts: n/a
 
      05-26-2004
> void getbuf(char* buf)
> {
> strcpy(buf, "hello, memory management!");
> }
>
> int main()
> {
> char buf[8];
> getbuf(buf);
> printf("%s\n", buf);
> }
>
> Is the generally accepted "safe" way to do it.


I'm not sure it is "safe". This piece of code causes segmentation fault.
Guess, why?

Regards.
 
Reply With Quote
 
Petec
Guest
Posts: n/a
 
      05-26-2004
Tomasz Szulist wrote:
>> void getbuf(char* buf)
>> {
>> strcpy(buf, "hello, memory management!");
>> }
>>
>> int main()
>> {
>> char buf[8];
>> getbuf(buf);
>> printf("%s\n", buf);
>> }
>>
>> Is the generally accepted "safe" way to do it.

>
> I'm not sure it is "safe". This piece of code causes segmentation
> fault. Guess, why?
>
> Regards.


*sigh*
I didn't bother to check the buffer length.

Better code:

bool getbuf(char* buf, int buflen)
{
const char* src = "hello, memory management!";
if(strlen(src) < buflen)
strcpy(buf, src);
else
return false;
return true;
}

int main()
{
char buf0[8];
char buf1[64];

if(getbuf(buf0, sizeof buf0))
printf("buffer 0: %s\n", buf0);
else
printf("buffer 0 not large enough.\n");

if(getbuf(buf1, sizeof buf1))
printf("buffer 1: %s\n", buf1);
else
printf("buffer 1 not large enough.\n");

return 0;
}

- Pete


 
Reply With Quote
 
Michiel Salters
Guest
Posts: n/a
 
      05-27-2004
(E-Mail Removed) (starket) wrote in message news:<(E-Mail Removed) om>...
> Hi folks,
> I'm new to programming, please help,
>
> char * b, q, *r;
> b=getbuf();
> q = *b;
> r= anotherfunction(b);
> /* we want to use ?q' and ?r' here*/
> char * getbuf()
> {
> char buff[8];
> /* unspecified, buff defined here *./
> return (char *) buff;
> }


Don't. Throw away any C++ books that suggest such code. Here's
how it's done in C++:

#include <string>

std::string getbuf(); // must declare first

std::string b;
char q;

b = getbuf();
q = b[0]; // count from 0 in C++

std::string getbuf()
{
std::string buff;
//...
return buff;
}

Much safer, and easier. = and == don't work as expected when used on
char*, let alone + and +=.

Regards,
Michiel Salters
 
Reply With Quote
 
NIKSANA NIKSANA is offline
Junior Member
Join Date: Jul 2007
Posts: 1
 
      07-30-2007
This is part from one job!I apply for it, too... This is a skill test so YOU CHEATING!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why is return type in getfullspec().annotations named as "return"? andrew cooke Python 1 04-03-2011 01:02 AM
One type traits class (or something like that) to get the return type of a pointer to function Diego Martins C++ 5 09-05-2006 08:38 PM
Structure return type is an incomplete type. gk245 C Programming 2 05-06-2006 01:03 AM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
Return by value -- primitive type vs class type DaKoadMunky C++ 8 05-14-2004 09:46 PM



Advertisments