473,322 Members | 1,781 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,322 software developers and data experts.

Pointers and functions

Hi All,

The following code make sense to me:

void x() {
int *a = NULL;
int b;
b = 4;
a = &b;
printf("a: %d",*a); // prints "a: 4"
}

Can someone please explain why this blows up?

void x() {
int *a = NULL;
y(a);
printf("a: %d",a); // Dies
}

void y(int* i){
int b;
b = 4;
i = &b;
}

Thanks again for the help!
Doug

Sep 14 '06 #1
8 1311
Doug Haber wrote:

[...]
Can someone please explain why this blows up?

void x() {
int *a = NULL;
y(a);
printf("a: %d",a); // Dies
}

void y(int* i){
int b;
b = 4;
i = &b;
}
It doesn't blow up. What made you think it should? Are you sure the code
you posted is the one on which you ran your tests? To begin with, x()
and y() should to be swapped or a forward declaration of y() introduced.
Please, try to post minimal, compilable code; that will make it easier
for people to assist you.

Regards,

--
Ney André de Mello Zunino
Sep 14 '06 #2

Doug Haber wrote:
Hi All,

The following code make sense to me:

void x() {
Doug Haber wrote:
Hi All,

The following code make sense to me:

void x() {
int *a = NULL;
int b;
b = 4;
a = &b;
printf("a: %d",*a); // prints "a: 4"
}

Can someone please explain why this blows up?

void x() {
int *a = NULL;
y(a);
printf("a: %d",a); // Dies
}

void y(int* i){
int b;
b = 4;
i = &b;
}
I am sure the function y() will blow up. I will lead to run time
exception.

reason is simple. you have declared a local varialble b in function
y().
Variable b exists in function y()'s scope only.
You are trying to refer the address of b outside the function. which
will not exists outside y().
after call to y(a), a points to b of function y().
This causes the exception.

-- Murali Krishna
int *a = NULL;
int b;
b = 4;
a = &b;
printf("a: %d",*a); // prints "a: 4"
}

Can someone please explain why this blows up?

void x() {
int *a = NULL;
y(a);
printf("a: %d",a); // Dies
}

void y(int* i){
int b;
b = 4;
i = &b;
}

Thanks again for the help!
Doug
Sep 14 '06 #3

Doug Haber wrote:
Hi All,

The following code make sense to me:

void x() {
int *a = NULL;
int b;
b = 4;
a = &b;
printf("a: %d",*a); // prints "a: 4"
}

Can someone please explain why this blows up?

void x() {
int *a = NULL;
y(a);
printf("a: %d",a); // Dies
}

void y(int* i){
int b;
b = 4;
i = &b;
}
I am sure the function y() will blow up. It will lead to run time
exception.

reason is simple. you have declared a local varialble b in function
y().
Variable b exists in function y()'s scope only.
You are trying to refer the address of b outside the function. which
will not exists outside y().
after call to y(a), a points to b of function y().
This causes the exception.

-- Murali Krishna

Sep 14 '06 #4
Ney André de Mello Zunino wrote:
Doug Haber wrote:

[...]
>Can someone please explain why this blows up?

void x() {
int *a = NULL;
y(a);
printf("a: %d",a); // Dies
}

void y(int* i){
int b;
b = 4;
i = &b;
}


It doesn't blow up. What made you think it should? Are you sure the
code you posted is the one on which you ran your tests? To begin with,
x() and y() should to be swapped or a forward declaration of y()
introduced. Please, try to post minimal, compilable code; that will
make it easier for people to assist you.

Regards,
1 - In y(...) you assign to i &b which is a local variable that only
exist during y(...) life.
2- Your printf prints incorrect value because you want to print a and
not *a.
3- Furthemore you will have crash if you write printf like this:
printf("a: %d",*a);

1 bad possible solution
void y(int* i){
static int b;
b = 4;
i = &b;
}

Stef
Sep 14 '06 #5
Murali Krishna schrieb:
Doug Haber wrote:
>Hi All,

The following code make sense to me:

void x() {
Doug Haber wrote:
>Hi All,

The following code make sense to me:

void x() {
int *a = NULL;
int b;
b = 4;
a = &b;
printf("a: %d",*a); // prints "a: 4"
}

Can someone please explain why this blows up?

void x() {
int *a = NULL;
y(a);
printf("a: %d",a); // Dies
}

void y(int* i){
int b;
b = 4;
i = &b;
}

I am sure the function y() will blow up. I will lead to run time
exception.

reason is simple. you have declared a local varialble b in function
y().
Variable b exists in function y()'s scope only.
You are trying to refer the address of b outside the function. which
will not exists outside y().
after call to y(a), a points to b of function y().
This causes the exception.
Additionally, a will still be nullptr after y(a), because the integer
pointer is passed by value, not by reference, therefore x() never sees
the value &b;

To the OP:
If you really want to access variables of local visibility through a
returned reference (which is actually one quite convenient way for
implementing a singleton), the locally visible variable must be static.

For instance:

MyClass* getSingletonInstance()
{
static MyClass hooray; // the static makes the difference
return &hooray;
}

void theCaller()
{
MyClass* iWantTheInstance= getSingletonInstance();
}

Now you can safely access static MyClass hooray from within theCaller.

-- Murali Krishna
> int *a = NULL;
int b;
b = 4;
a = &b;
printf("a: %d",*a); // prints "a: 4"
}

Can someone please explain why this blows up?

void x() {
int *a = NULL;
y(a);
printf("a: %d",a); // Dies
}

void y(int* i){
int b;
b = 4;
i = &b;
}

Thanks again for the help!
Doug
Sep 14 '06 #6
Hi All,

Sorry for the confusing post. Here is code that compiles:

void doIt(int* x){
int b = 2;
x = &b;
}

int _tmain(int argc, _TCHAR* argv[])
{
int* a = NULL;
doIt(a);
printf("a = %d", *a); // blows up
return 0;
}

I think I have two issues:
1. int b in doIt() goes out of scope when the function returns, so its
address is no longer valid when we get back to _tmain(). However even
if I rewrite doIt() as:

void doIt(int* x){
x = new int;
*x = 2;
}
It still fails which brings me to:
2. I think what I really want is for doIt() to take an int**. I think
the reason is that if it takes int *, it's actually getting a copy of
the pointer in _tmain, so allocating space and assigning a value to
this new pointer doesn't do me any good. However, if I rewrite the
program as below it works. Does this make sense?

void doIt(int** x){
*x = new int;
int b = 2;
**x = b;
}

int _tmain(int argc, _TCHAR* argv[])
{
int* a = NULL;
doIt(&a);
printf("a = %d", *a); // prints "a = 2"
return 0;
}

Finally, the below also works, but I think it's *bad* because I think I
shouldn't use an address from another function. Do you agree?

void doIt(int** x){
*x = new int;
int b = 2;
*x = &b;
}
Thanks again!
Doug
Doug Haber wrote:
Hi All,

The following code make sense to me:

void x() {
int *a = NULL;
int b;
b = 4;
a = &b;
printf("a: %d",*a); // prints "a: 4"
}

Can someone please explain why this blows up?

void x() {
int *a = NULL;
y(a);
printf("a: %d",a); // Dies
}

void y(int* i){
int b;
b = 4;
i = &b;
}

Thanks again for the help!
Doug
Sep 14 '06 #7
First, let us not top-post in this group. Luckly I never got a warning
regarding this till now. :)

Doug Haber wrote:
Hi All,

Sorry for the confusing post. Here is code that compiles:

void doIt(int* x){
int b = 2;
x = &b;
}

int _tmain(int argc, _TCHAR* argv[])
{
int* a = NULL;
doIt(a);
printf("a = %d", *a); // blows up
return 0;
}
I think I have two issues:
1. int b in doIt() goes out of scope when the function returns, so its
address is no longer valid when we get back to _tmain().
OK, we have already discussed about local varialbles. We should not
return the address of the local variable. because it dies after the
function execution. but a doesn't get the address of b. it still is
zero.
However even
if I rewrite doIt() as:

void doIt(int* x){
x = new int;
*x = 2;
}
It still fails which brings me to:
2. I think what I really want is for doIt() to take an int**. I think
the reason is that if it takes int *, it's actually getting a copy of
the pointer in _tmain, so allocating space and assigning a value to
this new pointer doesn't do me any good.
you are almost right. In main, we have declared pointer a and assigned
it to NULL (zero).
a's work is to point to some address and a is also created with some
address (&a).

so &a will have some address (system created)
our a points to zero. we assigned.

now in function doIt(int *x), x takes zero. not &a. and ofcourse x will
have it's own address (&x), that is no where related to &a. now &x and
&a are different.

in x = new int; x gets a value which is no where related to a. So when
doIt() completes it's execution, a will still point to zero. That is
why it fails.
However, if I rewrite the
program as below it works. Does this make sense?

void doIt(int** x){
*x = new int;
int b = 2;
**x = b;
}

int _tmain(int argc, _TCHAR* argv[])
{
int* a = NULL;
doIt(&a);
printf("a = %d", *a); // prints "a = 2"
return 0;
}
it makes sense. it should take the address of a, where a is a pointer
so we have to take pointer to pointer.
with the above explanation again. "new int" allocates memory. The point
is where it is allocating memory. For sure, it is allocating memory for
a. It is not local to function doIt().
Finally, the below also works, but I think it's *bad* because I think I
shouldn't use an address from another function. Do you agree?

void doIt(int** x){
*x = new int;
int b = 2;
*x = &b;
}
It worked because you are not manupulating any data in &b after the
function execution. but are you getting 2 in the result?
in my knowledge the results are undefined.
This is program logic error.

HTH.

-- Murali Krishna.

Sep 15 '06 #8
Murali Krishna wrote:
>Can someone please explain why this blows up?

void x() {
int *a = NULL;
y(a);
printf("a: %d",a); // Dies
}

void y(int* i){
int b;
b = 4;
i = &b;
}

I am sure the function y() will blow up. I will lead to run time
exception.
Not really. Function y() is well defined yet somewhat useless since it
has no observable effect.
reason is simple. you have declared a local varialble b in function
y().
Variable b exists in function y()'s scope only.
You are trying to refer the address of b outside the function. which
will not exists outside y().
after call to y(a), a points to b of function y().
This causes the exception.
What you describe would be true if argument i would be of type int *&.

--
Markus

Sep 15 '06 #9

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

Similar topics

1
by: Alex | last post by:
Is there any problem with sending function pointers through in a variable argument list? I have a function like the following: typedef (*ptr2FuncType1)( int ); typedef (*ptr2FuncType2)( double...
2
by: Thomas Matthews | last post by:
Hi, I would like to create a table (or vector) of pointers to templated functions. 1. How do I declare a typedef of a pointer to a templated function? For example, I have some functions...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
13
by: munni | last post by:
hi i want to write a program on function pointers where i have not written any programs using function pointers till now. i want to take an array of 26 functions which r to be pointed by this...
11
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI...
15
by: Christian Christmann | last post by:
Hi, in which situations pointers to functions might be more efficient/convenient than a direct function call? In the example I've found so far, I see no advantage of using pointers to...
1
by: Bushido Hacks | last post by:
A private utility function using a function pointer sounds ideal to me. I want to simpify writing the same set of loops. While there are a few functions that can't use it, setting and modifying...
12
by: claudiu | last post by:
Hi, I'll go straight to the first question. Why does the code below compile? void f(int i = 0); int main() { (&f)();
12
by: bgold | last post by:
Hey. I have a base class (SPRITE), and using this base class I have derived a large number of derived classes (PERSON, BULLET, MISSILE, etc.). Now, at a certain point in my program, I have a pair...
8
by: a | last post by:
Hello. Suppose I have a family of functions f_0(x) = 0*x f_1(x) = 1*x .... f_n(x) = n*x taking float and returning float (naturally, the actual functions would
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shćllîpôpď 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.