#include <iostream.h>
#include <string.h>
char *fun(void);
void main()
{
char *p = fun();
cou<<p;
}
char *fun()
{
char buf[32];
strcpy(buf,"hello");
return buf;
}
In this program how the address of the buffer will be pass to the pointer?
I am confused caz buf is a local variable, and when fun() come back to the main at that time if I am not wrong stack will be cleared.
Can I know the stack working for main and for the function fun()?
or from where can i get the information about the stack?
4 2081 Banfa 9,065
Expert Mod 8TB -
char *fun()
-
{
-
char buf[32];
-
strcpy(buf,"hello");
-
return buf;
-
}
-
This is an error and very very poor style. You should not return a pointer to a local variable because as soon as you return that pointer is no longer valid as the memory for the variable is returned to the stack. This invokes undefined behaviour.
If you continue to do this in the end you will cause bizzare and hard to trace errors in your program.
You should either make the variable returned static, but then there will only ever be 1 copy of the variable so each call will overwrite the previous data. -
char *fun()
-
{
-
static char buf[32];
-
strcpy(buf,"hello");
-
return buf;
-
}
-
or allocate the memory for the pointer but then the calling code must free the memory or cause a memory leak -
char *fun()
-
{
-
char * buf;
-
buf = malloc(strlen("hello")+1);
-
if (buf != NULL)
-
{
-
strcpy(buf,"hello");
-
}
-
return buf;
-
}
-
Or return a pointer to a string constant but then you can not alter the data returned -
const char *fun()
-
{
-
return "hello";
-
}
-
ya though buf is a local variable. my output is correct.
that is why i am not getting the point, how my output is correct.
I am using Turboc++ compiler
Once you exit the function fun(), the memory occupied by buf no longer belongs to your program. It is possible, yet not probable, that some other program requests memory and it gets the memory that buf occupied.
The memory now belongs to that program, and it can change the contents of buf to whatever it wants and it has every right to do so. Then, when you access the memory that now belongs to the other program, you may print something other than "hello".
Banfa 9,065
Expert Mod 8TB
Indeed, just because your code no longer owns the memory doesn't mean that it has been overwritten, it just means that it shouldn't be accessing it.
On my system I do not get "hello" output at all because ostream& ostream::operator<<(const char * s) puts data on the stack.
However try this -
#include <iostream.h>
-
#include <string.h>
-
-
char *fun(void);
-
-
void main()
-
{
-
-
char *p = fun();
-
-
NotFun();
-
-
cout<<p;
-
}
-
-
char *fun()
-
{
-
-
char buf[32];
-
strcpy(buf,"hello");
-
-
return buf;
-
}
-
-
void NotFun()
-
{
-
-
char buf[32] = "I'm having a bad day, go away";
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Edward K. Ream |
last post by:
Hello all,
First of all, my present state of mind re pep 318 is one of sheepish
confusion. I suspect pep 318 will not affect Leo significantly, but I am
most surprised that an apparently...
|
by: Aguilar, James |
last post by:
Hello all,
To begin, yes, this -is- a homework assignment. However, it is for my
Algorithms class, and my instructor has given us explicit permission to use
"expert" groups like newsgroups, so...
|
by: Tony Johansson |
last post by:
Hello experts!
I have two class template below with names Array and CheckedArray.
The class template CheckedArray is derived from the class template Array
which is the base class
This program...
|
by: Sushil |
last post by:
Hi gurus
I was reading FAQ
"alloca cannot be written portably, and is difficult to implement on
machines without a conventional stack."
I understand that the standard does not mandate...
|
by: gmccallum |
last post by:
General Info:
A struct is stored on the stack and a class on the heap.
A struct is a value type while a class is a reference type.
Question:
What if a struct contains a string...
|
by: jaso |
last post by:
Can you give any comments on this code?
I used one goto, is it bad?
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#define NOT_NULL 1
|
by: rover8898 |
last post by:
Hello all,
I used setjmp() in a recent of program of mine (it is not completed, so
I have not the chance to test it out yet). I am not very profocient in
C coding as are some of my co-workers....
|
by: Steven T. Hatton |
last post by:
No, this is not a troll, and I am not promoting Java, C-flat, D, APL, Bash,
Mathematica, SML, or LISP. A college teacher recently posted to this
newsgroup regarding her observation that there has...
|
by: Haskell Prelude |
last post by:
Hello Friends -
Can anyone answer these C questions?
1. Given the following initial declarations and value assignments:
int ints = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
110, 120, 130,...
|
by: AmberJain |
last post by:
Hello,
I am presently studying Data structures in C. And so I have a simple question about stack. The book which I got my hands on says about stack:
For me this quote (from the book) is a bit...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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: 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...
|
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,...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
| |