473,320 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Behavior of VC in return type

I am using vs2k5 for C++ IDE. I need to understand concept of the
following Code behavior on VC compiler.

Code-1
---
Object getObject()
{
return Object();
}

<In main function>
Object obj;
Obj = getObject();

Code-2
---
char * getTestArray()
{
char str[256];
strcpy(str, "this Text");
return str;
}

<In main function>
char text[256];
strcpy(text, getTestArray());

I really need answer of the following question,
1. What is the difference in code-1 and code-2 returning?
2. Will "strcpy(text, getTestArray());" statement make some problem in
feature??

Regards,
-aims

Apr 3 '07 #1
4 1127

"Mohammad Omer" <mo******@gmail.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...
>I am using vs2k5 for C++ IDE. I need to understand concept of the
following Code behavior on VC compiler.

Code-1
---
Object getObject()
{
return Object();
}

<In main function>
Object obj;
Obj = getObject();
This makes a copy of the entire object into main's local variable, ok.
>
Code-2
---
char * getTestArray()
{
char str[256];
strcpy(str, "this Text");
return str;
}

<In main function>
char text[256];
strcpy(text, getTestArray());
This copies a pointer to getTestArray's local memory into main's local
memory. The array the pointer points to is no longer available, so your
program will have undefined behavior (read, random bad things will happen,
you could crash, you could overwrite important data, you could format your
hard disk). In this case, because you are only reading from the bad
pointer, and it's pointer to an invalid location on the stack, you will
probably just get nonsense data copied with strcpy. However, since strcpy
terminates based on finding a NUL character, not a buffer size, the garbage
might not have a NUL in the first 256 bytes, in which case strcpy would
overflow the destination array, overwrite strcpy's stack frame including the
return address, and your program will crash. memcpy, being controlled by
buffer length, wouldn't be so bad -- you'd get a controlled amount of
garbage and no stack corruption -- but the fundamental problem of returning
a pointer to a variable that goes out of scope needs to be corrected.
>
I really need answer of the following question,
1. What is the difference in code-1 and code-2 returning?
2. Will "strcpy(text, getTestArray());" statement make some problem in
feature??

Regards,
-aims

Apr 3 '07 #2
This copies a pointer to getTestArray's local memory into main's local
memory. The array the pointer points to is no longer available, so your
program will have undefined behavior (read, random bad things will happen,
you could crash, you could overwrite important data, you could format your
hard disk). In this case, because you are only reading from the bad
pointer, and it's pointer to an invalid location on the stack, you will
probably just get nonsense data copied with strcpy. However, since strcpy
terminates based on finding a NUL character, not a buffer size, the garbage
might not have a NUL in the first 256 bytes, in which case strcpy would
overflow the destination array, overwrite strcpy's stack frame including the
return address, and your program will crash. memcpy, being controlled by
buffer length, wouldn't be so bad -- you'd get a controlled amount of
garbage and no stack corruption -- but the fundamental problem of returning
a pointer to a variable that goes out of scope needs to be corrected.
void getTestArray(char * str)
{
strcpy(str, "this Text");
return str;
}

<In main function>
char text[256];
getTestArray(text);

what you suggest me, is this solution will solving abnormal behavior
of a code or strcpy function replace with memcpy function to insure
the memory crashes??

Regards,
-aims

Apr 4 '07 #3

"Mohammad Omer" <mo******@gmail.comwrote in message
news:11**********************@n76g2000hsh.googlegr oups.com...
>This copies a pointer to getTestArray's local memory into main's local
memory. The array the pointer points to is no longer available, so your
program will have undefined behavior (read, random bad things will
happen,
you could crash, you could overwrite important data, you could format
your
hard disk). In this case, because you are only reading from the bad
pointer, and it's pointer to an invalid location on the stack, you will
probably just get nonsense data copied with strcpy. However, since
strcpy
terminates based on finding a NUL character, not a buffer size, the
garbage
might not have a NUL in the first 256 bytes, in which case strcpy would
overflow the destination array, overwrite strcpy's stack frame including
the
return address, and your program will crash. memcpy, being controlled by
buffer length, wouldn't be so bad -- you'd get a controlled amount of
garbage and no stack corruption -- but the fundamental problem of
returning
a pointer to a variable that goes out of scope needs to be corrected.

void getTestArray(char * str)
{
strcpy(str, "this Text");
return str;
}

<In main function>
char text[256];
getTestArray(text);

what you suggest me, is this solution will solving abnormal behavior
of a code or strcpy function replace with memcpy function to insure
the memory crashes??
That code is correct, but it would be even better to also accept the buffer
length as an argument and check it before doing the copy. Otherwise another
caller could do:

char bad[2];
getTestArray(bad);

Which would crash the same as the first code you posted.
>
Regards,
-aims

Apr 4 '07 #4
That code is correct, but it would be even better to also accept the buffer
length as an argument and check it before doing the copy. Otherwise another
caller could do:

char bad[2];
getTestArray(bad);

Which would crash the same as the first code you posted.
Right, I got the point. Thanks for guiding me. :)

Regards,
-aims

Apr 5 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michael Bosco | last post by:
Hi. I am fairly new to ASP. I have to build a website in ASP 3 that interfaces with VB 6 dll's. I've noticed some strange behavior, so I devised the following tests. Here's my test VB 6...
1
by: Stanimir Stamenkov | last post by:
Here's an example: <form action="bogus" method="post"> <p> <a href="prev.cgi"><input type="submit" name="prev" value="< Back"></a> <a href="next.cgi"><input type="submit" name="next"...
10
by: Minti | last post by:
I tried the following code on Borland C++ complier and Microsoft VC7.0 both seem to give conflicting results void foo(const int& x) { std::cout << "In const foo\n"; } void foo(int& x) ...
12
by: Dave Rahardja | last post by:
Does the C++ standard specify the behavior of floating point numbers during "exceptional" (exceptional with respect to floating point numbers, not exceptions) conditions? For example: double...
66
by: Mantorok Redgormor | last post by:
#include <stdio.h> struct foo { int example; struct bar *ptr; }; int main(void) { struct foo baz; baz.ptr = NULL; /* Undefined behavior? */ return 0;
2
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
6
by: Samuel M. Smith | last post by:
I have been playing around with a subclass of dict wrt a recipe for setting dict items using attribute syntax. The dict class has some read only attributes that generate an exception if I try to...
3
by: 63q2o4i02 | last post by:
Hi, I was wondering how I may get a python function to know what its name is without me having to write it manually? For example: def func1(): <do some stuff1> print 'func1' return True ...
5
by: Pupeno | last post by:
Hello, I am experiencing a weird behavior that is driving me crazy. I have module called Sensors containing, among other things: class Manager: def getStatus(self): print "getStatus(self=%s)"...
160
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.