A main can be changed by a function local without passing anything to the function?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
13:33 aluser@alf:~/C$ cat foo.c
#include <stdio.h>
main()
{
int i = 5;
func();
printf("%d", i);
}
func()
{
int x;
int *y = &x;
x[4] = 20;
}
13:33 aluser@alf:~/C$ gcc -o foo foo.c
foo.c: In function `func':
foo.c:13: error: subscripted value is neither array nor pointer
This is on gcc 3.3.3.
I can't imagine that x[4] would compile, because x is not a pointer or array and, even if the compiler wanted to interpret x as a pointer, what would be the size of its target?
still, given that change it's not inconceivable that y[4], since it goes off the end of what effectively is a one element array, ends up writing into a variable in another stack frame on your system. Somebody more familiar with the low level details should confirm.
One thing you could do to check if this is the case is printf("%p\n", &y[4]) in func() and printf("%p\n", &i) in main(), and see if the addresses are the same.
yeah, i tried printing all possible addresses, like
&i, &x, y, &y[0], &y[1], &y[2], &y[3], &y[4].
the addresses of i and x differed by 16. so the contents of (y + offset of 4) should give the value of i, right? but isn't this dangerous? a function may alter contents of another function and bugs like these are most difficult to find. what can i do to avoid this?
ah ok, then you understand the situation perfectly
This is a reason that not many languages have pointers like C and C++. Through a pointer you can access *any* memory address which is writable (or readable depending on what you're doing) by your process. There's really no magic answer except be careful.
If you're doing a lot of dynamic allocations you can use Electric Fence or dmalloc to help catch errors where you write past the end of an array (they do this by hacking malloc() and friends so that they always return a pointer to memory which is lined up against a non-readable non-writable page, so an overrun will seg fault). dmalloc can help debug memory leaks too.
Java (and a mess of other languages) people like to rub all this in C/C++ people's noses. C/C++ people point out that they want to tell the language what to do, not the other way around.
edit: I wrote this in response to
Quote:
yeah, i tried printing all possible addresses, like
&i, &x, y, &y[0], &y[1], &y[2], &y[3], &y[4].
the addresses of i and x differed by 16. so the contents of (y + offset of 4) should give the value of i, right? but isn't this dangerous? a function may alter contents of another function and bugs like these are most difficult to find. what can i do to avoid this?
oh yeah, it's also a reason why other languages have dynamic bounds checking on arrays. C/C++ people point out that this adds a bunch of extra code/bloat, and the program can be made faster by omitting the checks and making sure you don't run off the ends : )
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.