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

return type

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

Jul 22 '05 #2

P: n/a
ak
On 26 May 2004 06:24:20 -0700, st*****@yahoo.com (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

Jul 22 '05 #3

P: n/a
> 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.
Jul 22 '05 #4

P: n/a
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
Jul 22 '05 #5

P: n/a
st*****@yahoo.com (starket) wrote in message news:<8f*************************@posting.google.c 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
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.