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.
Hi I have a global character pointer say.,
char *p = 0;
char *q = 0;
I got two functions a() & b();
<CODE>
static int a()
{
p = malloc(10);
someMethod(p, q);
}
static int b()
{
q = malloc(10);
someMethod(p, q);
}
</CODE>
The above snippet will work considering p & q as global pointer. Is there any way I can use p & q as local between a() & b()?
static int a()
{
p = malloc(10);
someMethod(p, q);
}
static int b()
{
q = malloc(10);
someMethod(p, q);
}
Quote:
Originally Posted by akshay_satish
The above snippet will work considering p & q as global pointer. Is there any way I can use p & q as local between a() & b()?
Not really, because a() and b() are totally unconnected and their own scopes don't intersect. But I gather you're trying to avoid accessing global variables out of a function, which is a good idea.
The first thing that comes to mind is changing the interface of the two functions, so that they receive the two pointers as parameters. That's tricky here, because using malloc() you change the parameters inside the function, but not outside. To circumvent this, you'd have to pass a pointer to the actual parameter to the function. Since p and q are already pointers, the formal parameter would be a pointer to a pointer:
That looks a bit ugly, alright. But C always passes function arguments by value, there is no real "pass by reference". In C++ you could use references - it makes the code look more neat, but technically it's exactly the same. But wait, if you're looking at C++, you could do it better. Make an object (a class) that contains p and q as private members, and a() and b() as public methods. Then everything is cleanly encapsulated.
Doc CPU, wouldn't it be better, in C, to pass a state "object" (structure) instead?
Code:
struct state {
char *p;
char *q;
};
#define DEFAULT_STATE {0}
static void a(struct state *const state)
{
state->p = malloc(10);
somefunction(state->p, state->q);
}
static void b(struct state *const state)
{
state->q = malloc(10);
somefunction(state->p, state->q);
}
int main(void)
{
struct state somestate = DEFAULT_STATE;
a(&somestate);
b(&somestate);
return 0;
}
The DEFAULT_STATE constant, {0} means the entire structure will be initialized to binary zeros. (It is standard behaviour all C compilers I know will adhere to.) On all sane architectures, that means integer and floating point zeros, NULL pointers, and empty char arrays (filled with ASCII NULs == zero bytes).
While this may be rare to see in single-threaded applications, the void * parameter at end for pthread_create(3) is very, very often used for exactly this purpose.
It is also extremely common in more complex software. You see it all the time in projects like the Linux kernel and the Apache web server, where there are multiple state objects being passed around.
I think you can see the obvious benefits from this. There are no global variables, so you can instantiate any number of states you might need. If each thread only uses their own state object, the code is automatically thread safe (except for any external libraries and perhaps side effects like I/O). If you need more state, just extend the structure. For full thread safety, you can add either state object lock, or more fine-grained locking. If you use reference counting, you can garbage-collect entire state objects relatively easily.
static int a()
{
p = malloc(10);
someMethod(p, q);
}
static int b()
{
q = malloc(10);
someMethod(p, q);
}
Not really, because a() and b() are totally unconnected and their own scopes don't intersect. But I gather you're trying to avoid accessing global variables out of a function, which is a good idea.
The first thing that comes to mind is changing the interface of the two functions, so that they receive the two pointers as parameters. That's tricky here, because using malloc() you change the parameters inside the function, but not outside. To circumvent this, you'd have to pass a pointer to the actual parameter to the function. Since p and q are already pointers, the formal parameter would be a pointer to a pointer:
That looks a bit ugly, alright. But C always passes function arguments by value, there is no real "pass by reference". In C++ you could use references - it makes the code look more neat, but technically it's exactly the same. But wait, if you're looking at C++, you could do it better. Make an object (a class) that contains p and q as private members, and a() and b() as public methods. Then everything is cleanly encapsulated.
and why would it be not a good idea? As you can see both Doc CPU and Nominal Animal went along this route (but by far not as crappy as I did). It's a better idea than using global variables.
It sounds like you want the two pointer variables to be visible to only the functions a() and b()
If the functions a() and b() are in their own common compilation unit (source file), then they can mutually share static variables within that source file. So, just make p & q static, and make a() and b() global.
It sounds like you want the two pointer variables to be visible to only the functions a() and b()
If the functions a() and b() are in their own common compilation unit (source file), then they can mutually share static variables within that source file. So, just make p & q static, and make a() and b() global.
Now, a() and b() are callable from anywhere in your program, but the two variables are privately shared between the two fucntions.
In C++, a similar result can be achieved using an anonymous namespace
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.