473,511 Members | 9,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PLEASE HELP - Fundamental C language question

Could some C guru please help me? Suppose I have a code snippet as:
int* doit(){
int intArray[25];
int *ip;
ip = intArray;
return ip;
}

Since the array intArray is allocated on the stack and so destroyed
when function returns, is it safe to say that at compile time it is
not known what ip points to?
Thanks in advance for your help.

Nov 15 '05 #1
4 1728

cp**********@yahoo.com wrote:
Could some C guru please help me? Suppose I have a code snippet as:
int* doit(){
int intArray[25];
int *ip;
ip = intArray;
return ip;
}

Since the array intArray is allocated on the stack and so destroyed
when function returns, is it safe to say that at compile time it is
not known what ip points to?
Thanks in advance for your help.


If I remember right, it would also be safe to say that you won't know
what doit() points to. BTW, it is better to write int* doit() as int
*doit(void);

Chad

Nov 15 '05 #2
cp**********@yahoo.com said:
Could some C guru please help me? Suppose I have a code snippet as:
int* doit(){
int intArray[25];
int *ip;
ip = intArray;
return ip;
}

Since the array intArray is allocated on the stack
More precisely, the array intArray is an automatic object.
and so destroyed
when function returns, is it safe to say that at compile time it is
not known what ip points to?


At compile time, it doesn't point anywhere, because at compile time the
program isn't running!

When the function returns, the returned pointer is of no use, as you rightly
surmise, because the object to which it points (the first int in the array)
no longer exists. (Nor do the other ints in the array, of course!)

Solutions:

a) pass the address of the first array element as an argument;
b) create the space for the array dynamically;
c) make intArray static.

Of these, c) is the worst choice, in my opinion; b) is better, but you ought
to free the allocated memory when you're finished with it; a) is probably
the simplest choice.
--
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)
Nov 15 '05 #3
On 2005-10-30, Chad <cd*****@gmail.com> wrote:

cp**********@yahoo.com wrote:
Could some C guru please help me? Suppose I have a code snippet
as:
int* doit(){
int intArray[25];
int *ip;
ip = intArray;
return ip;
}

Since the array intArray is allocated on the stack and so
destroyed when function returns, is it safe to say that at
compile time it is not known what ip points to?
Thanks in advance for your help.


If I remember right, it would also be safe to say that you won't
know what doit() points to. BTW, it is better to write int* doit()
as int *doit(void);

Chad


It doesn't matter when he's defining a function like that. int
*doit(void) is better when declaring a prototype because int *doit()
means not to check arguments at all
Nov 15 '05 #4

<cp**********@yahoo.com> wrote
Could some C guru please help me? Suppose I have a code snippet as:
int* doit(){
int intArray[25];
int *ip;
ip = intArray; /* at this point we can use ip eg *ip = 123 will set intArray[0] to 123 */ return ip;
}


intArray is an "automatic variable". Usually but not always this means it is
put on a stack. Some small embedded systems use a different system.

Yu cannot use the return address of this function at all. Probably if you do
you will corrupt the first variable of any subroutine called next, but you
could cause a crash or a system defined error, or anything ("undefined
behaviour". Even loading the return address into a variable

/* illegal */
int *ptr - doit();

can cause a crash. Some very advanced systems detect that the pointer is
pointing to memory the program doesn't own, and terminate with an error
message.
Nov 15 '05 #5

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

Similar topics

51
4225
by: Casper Bang | last post by:
My question is fundamental I beleive but it has been teasing me for a while: I have two classes in my app. The first class is instantiated as a member of my second class. Within this first class,...
9
1581
by: Martin | last post by:
Due to the the large numbers of MS trained programmers unemployed we are looking at using MS Development environments. VB programmers are currently 10 a penny. However I find C a bit more...
1
1622
by: David Goodyear | last post by:
At the moment im experimenting with ideas in C++ and would really like to solve the following, please please help. Sorry i dont even know what the subject is this would come under? :( Sorry if...
3
1498
by: Kyle Kolander | last post by:
I recently looked over the faq item relating to fundamental type sizes: http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.5 and was a bit surprised, as I had been taught more-or-less the...
8
1787
by: Kyle Kolander | last post by:
Sorry, I sent this to comp.std.c++ and meant to send it here as well... Why are the minimum size guarantees for fundamental types intentionally omitted from section 3.9.1 Fundamental types of...
1
9587
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
118
6583
by: 63q2o4i02 | last post by:
Hi, I've been thinking about Python vs. Lisp. I've been learning Python the past few months and like it very much. A few years ago I had an AI class where we had to use Lisp, and I absolutely...
20
1874
by: David | last post by:
I feel like an idiot asking this but here goes: I understand the 'concept' of scope and passing data by value and/or by reference but I am confused on some specifics. class example{ int i; //my...
0
7251
marktang
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,...
0
7148
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...
1
7089
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...
0
7517
tracyyun
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...
0
5673
agi2029
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,...
0
4743
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...
0
3230
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...
0
1581
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 ...
1
790
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.