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 5 4318
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
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
> 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.
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 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: DaKoadMunky |
last post by:
Please consider the following...
<CODE>
#include <string>
using namespace std;
typedef int PrimitiveType;
typedef string ClassType;
|
by: Bruce W.1 |
last post by:
The intent of my web service is an RSS feed from a blog. Originally I
used a StringBuilder to make the XML and returned a string from the
webmethod. But this doesn't display properly in IE. So...
|
by: LaEisem |
last post by:
On-the-job, I have "inherited" a lot of old C language software.
A question or two about when "casting" of null pointer constants
is needed has occurred during behind-the-scenes cleanup
of some...
|
by: Michael C |
last post by:
eg
void DoIt()
{
int i = FromString("1");
double d = FromString("1.1");
}
int FromString(string SomeValue)
{
|
by: C++ |
last post by:
According to Thinking in C++
"You cannot modify the return type of a virtual function during
overriding.but there is a special case in which you can slightly
modify the return type. If you¡¯re...
|
by: kikazaru |
last post by:
Is it possible to return covariant types for virtual methods inherited
from a base class using virtual inheritance?
I've constructed an example below, which has the following structure:
Shape...
|
by: Alexander Widera |
last post by:
hi,
is it possible to return an object of an unknown (but not really unknown)
type with an method?
i have the following situation:
- a variable (A) of the type "object" which contains the...
|
by: hufaunder |
last post by:
I have a class "TestSuper" that implements the interface "TestBase".
The interface has a property of type "ReturnType". The class
"TestSuper" does not return "ReturnType" but a derivation...
|
by: =?Utf-8?B?QmVu?= |
last post by:
Hi all,
I'm trying to understand the concept of returning functions from the
enclosing functions. This idea is new to me and I don't understand when and
why I would need to use it.
Can someone...
|
by: fabian.lim |
last post by:
Hi All,
Im a newbie to C++, I am trying to customize the vector template
STL to a template Class. My code is shown below. Im confused about
something and maybe somebody here might be able to...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |