1

Consider the following function:

 char *f()
 {   
 char *s=malloc(8);
 }
 main()
 {
  printf("%c",*f()='A');
 }

If I comment the line char *s=malloc(8); I get an error as if the assignment *f()='A' accessed invalid memory. Since I never return any variable why does above assignment work at all?

2nd question: 'A' is assigned to temporary variable created on return of function . So why can't ++a etc. be used as lvalue?

Moo-Juice
  • 38,257
  • 10
  • 78
  • 128

6 Answers6

4

Assuming return values are passed in registers, the return value from malloc might still be there when returning from f(). By pure chance.

When assigning to *f() you are not assigning to a temporary but to the memory returned from malloc. Assigning to ++a is totally different.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
0

Your function f() is not returning anything, you need to add:

return s;

But, in all honesty, this is just going to be the start of your problems. You also need to free() the return value of f().

I do not know why you have tagged this question C++, this is clearly C, and so I have untagged as such.

Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
0

You have to return the pointer in f() with the return statement, or else an illegal pointer will be returned:

char *f()
{   
    char *s=malloc(8);
    return s;
}
Constantinius
  • 34,183
  • 8
  • 77
  • 85
  • "...or else an illegal pointer will be returned" Where in the world does this come from? – Chris Lutz Aug 02 '11 at 09:43
  • Probably from my bad use of a foreign language. But what exactly is wrong with that statement? – Constantinius Aug 02 '11 at 09:52
  • That's not what happens, either according to the standard (which says undefined behavior) or in this real world example (the pointer from `malloc` is left over in the return register, effectively causing it to be returned from `f` as well). – Chris Lutz Aug 02 '11 at 09:55
0

Your function does not return anything. Since you declare the function with the return-type char *, not returning anything results in undefined behavior, as defined in paragraph 6.6.3.2 of the current C++ Standard:

Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.

Undefined behavior means that anything can happen. To fix that problem, your function should look like this:

char *f()
{   
    return malloc(8);
}
Community
  • 1
  • 1
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
0

In your function, you return a pointer where you can assign stuff... ++a prevents it by returning const reference or instance. you can have the same behaviour if you have const char* f() { ... }.

Of course you could also implement ++a differently :)

duedl0r
  • 9,289
  • 3
  • 30
  • 45
0

1/ The return of f() is an uninitialized pointer, but exists.
*f() return the value pointed by an unspecified (random) address.
Writing at this address is an invalid memory access, or maybe not if this address is by "chance" a writable piece of memory (stack or previously allocated heap).
In C, it is your responsibility to ensure that you properly access the memory.

2/ 'A' is not assigned here to a temporary.

log0
  • 10,489
  • 4
  • 28
  • 62