question on storage duration

M

Mark

Hello

given the very simple code:

#include <stdlib.h>
#include <stdio.h>

int g(void)
{
int i = 2;
return i;
}

int *f(void)
{
int *p;
p = malloc(sizeof *p);
/* skip result checking for the example's brevity */
*p = 2;
return p;
}

int main(void)
{
int i;
int *j;

i = g();
j = f();
printf("g()=%d, f()=%d\n", i, *j);
return 0;
}

Here is my reasoning.

In g() I declare automatic variable 'i' and initialize it with 2. The
storage, occupied by the variable, exists till the function returns. So we
can safely return the value. In function f() 'p' is as well automatic
variable, the value of which points to some storage, obtained by 'malloc()'
and this storage exists as long as the program runs. In main() local pointer
'j' is assigned to the returned value of g() and we can perfectly access the
value of that pointer.

Am I correct?
 
B

Ben Bacarisse

Mark said:
given the very simple code:

#include <stdlib.h>
#include <stdio.h>

int g(void)
{
int i = 2;
return i;
}

int *f(void)
{
int *p;
p = malloc(sizeof *p);
/* skip result checking for the example's brevity */
*p = 2;

The code that checks:

if (p)
*p = 2;

is shorter than the explanation that you want to be brief!
return p;
}

int main(void)
{
int i;
int *j;

i = g();
j = f();
printf("g()=%d, f()=%d\n", i, *j);
return 0;
}

Here is my reasoning.

In g() I declare automatic variable 'i' and initialize it with 2. The
storage, occupied by the variable, exists till the function
returns. So we can safely return the value. In function f() 'p' is as
well automatic variable, the value of which points to some storage,
obtained by 'malloc()' and this storage exists as long as the program
runs. In main() local pointer 'j' is assigned to the returned value of
g() and we can perfectly access the value of that pointer.

Am I correct?

Yes.
 
N

Nick Keighley

given the very simple code:

#include <stdlib.h>
#include <stdio.h>

int g(void)
{
    int i = 2;
    return i;
}

int *f(void)
{
    int *p;
    p = malloc(sizeof *p);
    /* skip result checking for the example's brevity */
    *p = 2;
    return p;
}

int main(void)
{
    int i;
    int *j;

    i = g();
    j = f();
    printf("g()=%d, f()=%d\n", i, *j);
    return 0;
}

Here is my reasoning.

In g() I declare automatic variable 'i' and initialize it with 2. The
storage, occupied by the variable, exists till the function returns. So we
can safely return the value. In function f() 'p' is as well automatic
variable, the value of which points to some storage, obtained by 'malloc()'
and this storage exists as long as the program runs.

or until free() is called for the memory
In main() local pointer
'j' is assigned to the returned value of g() and we can perfectly access the
value of that pointer.

Am I correct?

yes
 
R

Richard Bos

Ben Bacarisse said:
The code that checks:

if (p)
*p = 2;

is shorter than the explanation that you want to be brief!

No; the code that does _only_ that is completely useless, and indeed
dangerous, as a check for malloc() failure. You'd need to add code in
main(), as well, so as not to dereference a null pointer.

This is a mistake that is often made, in fact: check for null or error
returns, and then do nothing with the result of that check. The effect
is that the error, instead of being caught, is only propagated up a
level. It's even more dangerous than not checking at all.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top