473,761 Members | 4,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Global pointer pointing to locally defined type

Hi!

What happens to a globally defined pointer, e.g.

void *value;

that points to a particular type in a function:

int dummy_func (int a)
{
value = &a;
return 0;
}

after the function returns to main? If this doesn't work, how could I make
it work? Do I have to allocate memory for this int and copy the original
int to that and then point value to this newly allocated int?

Thanks for your time,

Andrej
Nov 14 '05 #1
31 2195
Andrej Prsa <an*********@gu est.arnes.si> scribbled the following:
Hi! What happens to a globally defined pointer, e.g. void *value; that points to a particular type in a function: int dummy_func (int a)
{
value = &a;
return 0;
} after the function returns to main?
It points to a variable that is no longer in scope, thus no longer
accessible. Any attempt to indirect through that pointer will cause
undefined behaviour. In short, the pointer points to garbage and
cannot be used until reassigned.
If this doesn't work, how could I make
it work? Do I have to allocate memory for this int and copy the original
int to that and then point value to this newly allocated int?


That could be one way, yes.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"'It can be easily shown that' means 'I saw a proof of this once (which I didn't
understand) which I can no longer remember'."
- A maths teacher
Nov 14 '05 #2
Andrej Prsa <an*********@gu est.arnes.si> scribbled the following:
Hi! What happens to a globally defined pointer, e.g. void *value; that points to a particular type in a function: int dummy_func (int a)
{
value = &a;
return 0;
} after the function returns to main?
It points to a variable that is no longer in scope, thus no longer
accessible. Any attempt to indirect through that pointer will cause
undefined behaviour. In short, the pointer points to garbage and
cannot be used until reassigned.
If this doesn't work, how could I make
it work? Do I have to allocate memory for this int and copy the original
int to that and then point value to this newly allocated int?


That could be one way, yes.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"'It can be easily shown that' means 'I saw a proof of this once (which I didn't
understand) which I can no longer remember'."
- A maths teacher
Nov 14 '05 #3
Hi!
If this doesn't work, how could I make
it work? Do I have to allocate memory for this int and copy the original
int to that and then point value to this newly allocated int?


That could be one way, yes.


Is there any better way you'd suggest?

Andrej
Nov 14 '05 #4
Hi!
If this doesn't work, how could I make
it work? Do I have to allocate memory for this int and copy the original
int to that and then point value to this newly allocated int?


That could be one way, yes.


Is there any better way you'd suggest?

Andrej
Nov 14 '05 #5
Andrej Prsa <an*********@gu est.arnes.si> scribbled the following:
Hi!
> If this doesn't work, how could I make
> it work? Do I have to allocate memory for this int and copy the original
> int to that and then point value to this newly allocated int?
That could be one way, yes.

Is there any better way you'd suggest?


Pass the address of a variable as a parameter to your function.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"All that flower power is no match for my glower power!"
- Montgomery Burns
Nov 14 '05 #6
Andrej Prsa <an*********@gu est.arnes.si> scribbled the following:
Hi!
> If this doesn't work, how could I make
> it work? Do I have to allocate memory for this int and copy the original
> int to that and then point value to this newly allocated int?
That could be one way, yes.

Is there any better way you'd suggest?


Pass the address of a variable as a parameter to your function.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"All that flower power is no match for my glower power!"
- Montgomery Burns
Nov 14 '05 #7
Andrej Prsa <an*********@gu est.arnes.si> writes:
> If this doesn't work, how could I make it work? Do I have to
> allocate memory for this int and copy the original int to that and
> then point value to this newly allocated int?


That could be one way, yes.


Is there any better way you'd suggest?


I recommend to avoid "global" (file scope) objects whenever possible.

What's the best solution to your problem depends on what your problem
is. :) If you describe a bit more elaborately what you're actually
trying to achieve, we might be able to make better suggestions.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #8
Andrej Prsa <an*********@gu est.arnes.si> writes:
> If this doesn't work, how could I make it work? Do I have to
> allocate memory for this int and copy the original int to that and
> then point value to this newly allocated int?


That could be one way, yes.


Is there any better way you'd suggest?


I recommend to avoid "global" (file scope) objects whenever possible.

What's the best solution to your problem depends on what your problem
is. :) If you describe a bit more elaborately what you're actually
trying to achieve, we might be able to make better suggestions.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #9
Hi!
> If this doesn't work, how could I make it work? Do I have to
> allocate memory for this int and copy the original int to that and
> then point value to this newly allocated int?

That could be one way, yes.


Is there any better way you'd suggest?


I recommend to avoid "global" (file scope) objects whenever possible.

What's the best solution to your problem depends on what your problem
is. :) If you describe a bit more elaborately what you're actually
trying to achieve, we might be able to make better suggestions.


Sure! I'm making a script language for modeling of eclipsing binaries. I
want to be able to reach a parameter table from all kinds of plug-ins to
this language, thus I have a global table like this:

typedef struct PHOEBE_paramete r_tag
{
GtkWidget *widget;
char *qualifier;
char *keyword;
char *description;
PHOEBE_type type;
void *valueptr;
} PHOEBE_paramete r_tag;

PHOEBE_paramete r_tag *PHOEBE_paramet ers;
int PHOEBE_paramete rs_no;

Here PHOEBE_type is a simple enumeration that defines all types that a
particular argument can be and GtkWidget is a GTK+ widget (GUI related).
PHOEBE_paramete rs is a global array, to which parameters are added by
calling a function:

void declare_paramet er (char *qualifier, GtkWidget *parent, char *widget,
char *keyword, char *description, ...)

The variable argument list holds the type and the initial value, e.g.:

declare_paramet er ("phoebe_name_v alue", PHOEBE, "data_star_name _entry",
"NAME", "Star name", TYPE_STRING, "New star");

As you may imagine, I want the valueptr of the global PHOEBE_paramete rs[i]
variable to point to whatever value corresponds to the given parameter.
Since it can be int, double, char *, but also an array of those, I hoped a
generic pointer void * would do the trick for me. And since this is a
back-end for a very large project (PHOEBE - PHysics Of Eclipsing
BinariEs), I want it to be, well, perfect! That's why I'm consulting you
guys!

Thanks for your time reading and answering!

Best wishes,

Andrej
Nov 14 '05 #10

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

Similar topics

8
102757
by: David Hitillambeau | last post by:
Hi guys, As I am new to Python, i was wondering how to declare and use global variables. Suppose i have the following structure in the same module (same file): def foo: <instructions> <instructions> def bar: <instructions>
16
362
by: Andrej Prsa | last post by:
Hi! What happens to a globally defined pointer, e.g. void *value; that points to a particular type in a function: int dummy_func (int a) {
204
13072
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 = {0,1,2,4,9};
8
13207
by: Rob T | last post by:
When I was using VS2003, I was able to compile my asp.net project locally on my machine and copy it to the production server and it would run just fine. I've now converted to VS2005. The project compiles & runs fine locally, but when I copy to the production machine, I get this error: Parser Error Message: Could not load type 'Global'. Source Error: Line 1: <%@ Application Codebehind="Global.asax.vb" Inherits="Global" %> Source...
0
9522
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, 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...
0
10111
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9948
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 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...
1
9902
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9765
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 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...
0
8770
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, 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...
1
7327
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6603
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();...
1
3866
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 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.