return type

S

starket

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
 
P

Petec

starket said:
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
 
A

ak

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()

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;
}


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
 
T

Tomasz Szulist

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.
 
P

Petec

Tomasz said:
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
 
M

Michiel Salters

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
 
Joined
Jul 30, 2007
Messages
1
Reaction score
0
This is part from one job!I apply for it, too... This is a skill test so YOU CHEATING!
 

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,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top