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 4326
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.c om (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,b 2)
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.c om (starket) wrote in message news:<8f******* *************** ***@posting.goo gle.com>... 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 now I'm trying
an XmlTextWriter instead.
I whipped-up another webservice based on this:...
|
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 of that software. That subject seems not to be addressed,
at least not directly, in the C FAQ where FAQ 5.2 seems most relevant.
References:
* C...
|
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 returning a pointer or a reference to a
base class, then the overridden version of the function may
return a pointer or reference to a class derived...
| |
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 = base class
Triangle, Square = classes derived from Shape
Prism = class derived from Shape
TriangularPrism, SquarePrism = classes derived from...
|
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 object
- a variable (B) of the type "Type" which contains the type of the object in
(A) (A should be of the type B)
- a method which should return 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
"ReturnSuper". This gives the following compile error due to a bad
return type:
TestSuper does not implement interface member TestBase.Func....
|
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 please shed some light on this subject?
Thanks a lot,
Ben
|
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 help me.
Lets say X is my vector, which contains 10 elements. I want to
assign elements indexed 2 to 5 to another vector Y, so I want to do it
like...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |