473,406 Members | 2,843 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,406 software developers and data experts.

Array scope

Consider this lines of code:

*int make_array();
void get_array(int *array_pointer);

*int make_array()
{
int i;
array[20];
for(i=0,i<=19,i++)
{
array[i]=i;
}
return array;
}

void get_array(int *array_pointer)
{
for(i=0,i<=19,i++)
{
printf("%d",*array_pointer);
array_pointer++;
}
}
get_array(make_array());

I made this code in order to make my question.
Does the last line of code make sence?
Does the array exists when make array has ended?
When i run such code i don't have a problem.
Am i lucky that the items are still there and unchanged?
Nov 14 '05 #1
5 1779
"George_K" <ge************@hotmail.com> wrote in message
news:52**************************@posting.google.c om...
Consider this lines of code:

*int make_array();
You mean:

int *make_array();

Or, better:

int *make_array(void);
void get_array(int *array_pointer);

*int make_array()
Same again.
{
int i;
array[20];
You mean:
int i,
array[20];

Or:
int i;
int array[20];
for(i=0,i<=19,i++)
You mean:
for (i = 0; i <= 19; i++)

The more usual form for the test would be "i < 20". See if you can think of
any possible reasons for this.
{
array[i]=i;
}
return array;
Causes undefined behaviour (or if not now, then when you try to access the
pointed-to array later); array ceases to exist when the function ends.
}

void get_array(int *array_pointer)
{
You need to declare i:
int i;
for(i=0,i<=19,i++)
The same comments as the previous for loop apply here.
{
printf("%d",*array_pointer);
array_pointer++;
}
}

get_array(make_array());

I made this code in order to make my question.
Does the last line of code make sence?
Apart from "get_array" being a poor choice of function name IMO, there is
nothing wrong with the last line in itself.
Does the array exists when make array has ended?
No. If you want it to, either (1) declare it in the calling function and
change make_array() so that it accepts a pointer and the size of the array,
or (2) use malloc() (remembering to free() the array later).
When i run such code i don't have a problem.
Am i lucky that the items are still there and unchanged?


Yes. Or unlucky, depending on how you look at things.

Alex
Nov 14 '05 #2
ge************@hotmail.com (George_K) wrote:
Consider this lines of code:

*int make_array();
void get_array(int *array_pointer);

*int make_array()
{
int i;
array[20];
for(i=0,i<=19,i++)
{
array[i]=i;
}
return array;
Oops. You've just returned a pointer to a variable whose life-time has
ended. Use this pointer in any way and you have undefined behaviour. See
also <http://www.eskimo.com/~scs/C-faq/q7.5.html>.
}

void get_array(int *array_pointer)
{
for(i=0,i<=19,i++)
{
printf("%d",*array_pointer);
array_pointer++;
}
}

get_array(make_array());

I made this code in order to make my question.
Does the last line of code make sence?
The whole make_array() function is broken. This line in itself is not
the problem. Fix make_array(), and the last line is OK.
(Of course, if you fix make_array() by calling malloc(), you now need to
make sure that get_array() either free()s that memory or saves the
pointer somewhere global so it can be free()d later, and making array
static has its own problems, but those are secondary, and solvable. As
it is, it is broken.)
Another problem is the use of magic numbers for the size of the array,
but that can easily be solved in production code using a #defined
constant.
Does the array exists when make array has ended?
Nope.
When i run such code i don't have a problem.
Am i lucky that the items are still there and unchanged?


Unlucky, I'd say. If you crash, you at least know that you've got a
problem. If it appears to run, it will probably decide to start crashing
just as you're demonstrating your program to your biggest client...

Richard
Nov 14 '05 #3
Richard Bos <rl*@hoekstra-uitgeverij.nl> spoke thus:
Unlucky, I'd say. If you crash, you at least know that you've got a
problem. If it appears to run, it will probably decide to start crashing
just as you're demonstrating your program to your biggest client...


You mean like when Bill Gates was demonstrating his product's alleged
plug-and-play capability at a major conference?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #4
ge************@hotmail.com (George_K) wrote in message news:<52**************************@posting.google. com>...
Consider this lines of code:

*int make_array();
void get_array(int *array_pointer);

*int make_array()
{
int i;
array[20];
for(i=0,i<=19,i++)
{
array[i]=i;
}
return array;
}

void get_array(int *array_pointer)
{
for(i=0,i<=19,i++)
{
printf("%d",*array_pointer);
array_pointer++;
}
}
get_array(make_array());

I made this code in order to make my question.
Does the last line of code make sence?
Does the array exists when make array has ended?
When i run such code i don't have a problem.
Am i lucky that the items are still there and unchanged?


another solution could be to make the array static. But the array as
it stands goes away as soon as you leave the function. It might still
be around, it might not. But you are looking at something that no
longer official exists
Nov 14 '05 #5
Stuart Gerchick wrote:
ge************@hotmail.com (George_K) wrote in message news:<52**************************@posting.google. com>...
[function returning pointer to `auto' array]

another solution could be to make the array static. But the array as
it stands goes away as soon as you leave the function. It might still
be around, it might not. But you are looking at something that no
longer official exists


s/official//

The disappearance of an `auto' variable when its
containing block exits is not a purely academic concern
nor a mere matter of pedantry. On most C implementations
`auto' variables are allocated on a stack, and the stack
is popped when a function returns (and sometimes when an
inner block exits, too). The next function you call (or
block you enter) will overlay the same stack memory with
its own `auto' variables, wiping out whatever the first
function (block) stored there.

Even without any subsequent function call, many C
implementations feel free to modify the stack memory
beyond the deepest current block. For example, the
context switch generated by a hardware interrupt may well
push registers, program counter, and other context onto
the stack, once again scribbling all over the memory
addressed by your bogus pointer.

On comp.lang.c we like to say things like "evaluating
`INT_MAX + 1' may make demons fly out of your nose."
From the point of view of the C language Standard this
is correct -- but in the real world it is, of course,
utter nonsense. Returning a pointer to an `auto' variable,
though, is an error of a different kind: not only *can*
it cause strange trouble, it almost certainly *will* do
so, and not only on the DeathStation 9000 but on real live
machines just like those you use every day. Don't Do That.

--
Er*********@sun.com

Nov 14 '05 #6

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

Similar topics

6
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return...
1
by: Alejandro Narancio | last post by:
Hello my name is Alejandro and i've a question. I need to make a functions with this characteristics: - params: an array of strings with the names of components in a page (for example, some...
11
by: Laphan | last post by:
Hi All I'm using .getRows() with a local var array instead of doing a recursive loop so that I'm being a good ASP newvbie and closing my object i/o's (the recordset in this case) as quick as...
6
by: Michael | last post by:
Hi, all, I want to keep the address of one static array in some place in order to access the content of this array in other module. Which way should I use? I try it in this way: find one DRAM...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
6
by: silverburgh.meryl | last post by:
Hi, In one A.cpp file, I have defined a static array of JSFunctionSpec, like this: static JSFunctionSpec JProfFunctions = { {"JProfStartProfiling", JProfStartProfiling, 0, 0, 0 },...
5
by: jonathan184 | last post by:
Hi I am trying to access a log file and read the fikle into an array and split the file using the space as a delimiter but i get this error. Parse error: syntax error, unexpected $end in...
15
by: Steve | last post by:
I am having problems getting values out of an array. The array is set as a global array and values are pushed into it as they are read from a JSON file using a "for loop". When the "for loop" is...
15
by: mdh | last post by:
May I ask. If an array is defined , not as a static, but outside of a function, is there any guarantee as to the contents of each element? Thanks.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
jinu1996
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...
0
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
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
isladogs
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...

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.