Hi All,
The following code as per my knowledge should not work:
int* some()
{
int b = 10;
return &b;
}
int main(int argc, char** argv)
{
int* test = NULL;
test = some();
printf("test ; %d\n", *test);
*test = 20;
return 0;
}
as it is returning the address of a variable on stack which should not
be valid once the function returns. However, this code seems to be
working absolutely fine in VC++ 6.0 on x86 processor. Could anyone
please explain what is correct or wrong and why is it working.
Also, in the following statement
char *p = "Hello world";
where is the memory for p allocated? is it on stack, heap or global
area and when is it freed, if so?
Thanks in advance,
Manish 19 3093
Manish Tomar wrote: Hi All,
The following code as per my knowledge should not work:
int* some() { int b = 10; return &b; }
int main(int argc, char** argv) { int* test = NULL;
test = some(); printf("test ; %d\n", *test); *test = 20;
return 0; }
as it is returning the address of a variable on stack which should not be valid once the function returns. However, this code seems to be working absolutely fine in VC++ 6.0 on x86 processor. Could anyone please explain what is correct or wrong and why is it working.
You have undefined behaviour. The value is still valid because nothing
has had a chance to overwrite it. Add a couple more function calls
before the printf and see what happens.
Also, in the following statement char *p = "Hello world"; where is the memory for p allocated? is it on stack, heap or global area and when is it freed, if so?
It isn't, "Hello world" is a string literal, look it up in the FAQ.
--
Ian Collins.
Manish Tomar said: Hi All,
The following code as per my knowledge should not work:
And it doesn't. Unfortunately, "not work" can have a variety of outcomes.
One of those outcomes is "what someone who thinks the code is correct would
expect to happen, up until the cost of it not working exceeds the value of
continuing to hire the person writing the code".
In other words, the code will continue to work until your boss is
demonstrating the program to an important customer, at which point it will
fail in a highly visible and embarrassing way.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
In statement printf("Hello world"), the printf function receives the
string literal as char* which means it receives some memory address.
Hence some memory should be used to store this string. Where is it?
Manish Tomar wrote: In statement printf("Hello world"), the printf function receives the string literal as char* which means it receives some memory address. Hence some memory should be used to store this string. Where is it?
printf("It's at %p\n", (void *)"Hello world");
--
pete
pete wrote: Manish Tomar wrote: In statement printf("Hello world"), the printf function receives the string literal as char* which means it receives some memory address. Hence some memory should be used to store this string. Where is it?
printf("It's at %p\n", (void *)"Hello world");
IMHO, the OP was looking for stack/heap as an answer. Which of course
if one of the most commonly occuring questions around here.I remember
having read a rather excellent post by Mr. Paul Hsieh regarding this,
sometime
back.STFW.
"Suman" <sk*****@gmail. com> wrote: pete wrote: Manish Tomar wrote: In statement printf("Hello world"), the printf function receives the string literal as char* which means it receives some memory address. Hence some memory should be used to store this string. Where is it? printf("It's at %p\n", (void *)"Hello world");
IMHO, the OP was looking for stack/heap as an answer. Which of course if one of the most commonly occuring questions around here.
The question misses the point; and any definite answer is likely to be
unreliable.
(FWIW, any string literal has static duration; the most logical place to
put them where they occur normally is neither on the stack nor on what
is inaccurately termed the heap; but this is a function parameter, so
putting _this_ string literal where normal function parameters are
passed is also a viable option; other options are not inconceivable; and
most importantly, any program which cares the slightest _where_ string
literals reside is designed broken.)
I remember having read a rather excellent post by Mr. Paul Hsieh regarding this, sometime back.
I would be most surprised.
Richard
Manish Tomar wrote: In statement printf("Hello world"), the printf function receives the string literal as char* which means it receives some memory address. Hence some memory should be used to store this string. Where is it?
Where ever the implementation chooses to store it. On a number of the
systems I've dealt with it would be burnt in to an EEPROM, but this is
slightly less likely on a desktop PC.
All you need to know is that the string exists for the lifetime of the
program.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro: http://clc-wiki.net/wiki/Intro_to_clc
Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
Suman wrote: pete wrote: Manish Tomar wrote: In statement printf("Hello world"), the printf function receives the string literal as char* which means it receives some memory address. Hence some memory should be used to store this string. Where is it?
printf("It's at %p\n", (void *)"Hello world");
IMHO, the OP was looking for stack/heap as an answer.
"The stack" and "the heap"
aren't part of the C programming language.
--
pete
pete wrote: Suman wrote:
[...] IMHO, the OP was looking for stack/heap as an answer.
"The stack" and "the heap" aren't part of the C programming language.
Of course not, which is why I avoided any allusion to them in
my reply. I somehow missed the whole thread, and now that
I have it, I do see that the OP _did_ mention stack/heap etc
in early on.
The problem is your reply is technically sound and correct.
But I have a gut feeling it fails to communicate with the OP.
Whence my lament. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Roland Hall |
last post by:
Is there a way to return multiple values from a function without using an
array? Would a dictionary object work better?
--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation -...
|
by: Milan Gornik |
last post by:
Hello to all,
My question is on right way of returning newly created class from a function
(and thus, from class method or operator). As I currently see it, there are
two different ways to do that. One way is to create new class as a local
(automatic) variable in function and then to return it:
|
by: john |
last post by:
Relatively new to C coding, so any help would greatly be appreciated.
I'm having problems try to return my string array from my parsing
function. When I do a printf I am getting the correct value for my first
element but my subsequent printf's will return garbage. Can someone let
me know what I am doing wrong. Thanks in advance.
-jlewis
|
by: wonderboy |
last post by:
Hey guys,
I have a simple question. Suppose we have the following
functions:-
//-----My code starts here
char* f1(char* s)
{
char* temp="Hi";
return temp;
|
by: Generic Usenet Account |
last post by:
Is it okay to return a local datastructure (something of type struct)
from a function, as long as it does not have any pointer fields? I
think it is a bad idea, but one of my colleagues does not seem to think
so. According to my colleague, if the data structure does not "deep
copy" issues, it is perfectly okay to return a local variable. This is
because at the point of invocation, a copy of the data structure is
made.
In all fairness...
| |
by: I.M. !Knuth |
last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until
I started goofing around with this:
================================================================================
/* A function that returns a pointer-of-arrays to the calling function. */
#include <stdio.h>
int *pfunc(void);
|
by: rsa_net_newbie |
last post by:
Hi there,
I have a Managed C++ object (in a DLL) which has a method that is
defined like ...
Generic::List<String^>^ buildList(String^ inParm)
Now, when I compile it, I get "warning C4172: returning address of
local variable or temporary". In good old 'C', that would indicate
that a 'static' was missing from the declaration of the returned value.
|
by: student1976 |
last post by:
All
Beginner/Intermediate level question. I understand that returning
ptr to local stack vars is bad.
Is returning foo_p_B from fnB() reliable all the time, so that using
foo_p_A does not break?
Thanks
Josh
|
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;
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |