1

I have searched and found a program for dangling pointers through some websites. I found one best program that executes perfectly in TC. But I cannot understand entire meaning of the program due my doubt in fflush(stdin). So please tell the meaning and other proceedings of the following program:

    #include<stdio.h>
    int *call(void);
    int main(void) {
      int *ptr;
      ptr = call();
      fflush(stdin);
      printf("%d", *ptr);
      return 0;
    }
    int *call(void) {
      int x = 25;
      ++x;
      return &x;
    } 
sepp2k
  • 341,501
  • 49
  • 643
  • 658
kalyan
  • 55
  • 8

3 Answers3

3

There's no point in fflush(stdin) in general because it's undefined behavior (for a more detailed description of why see this answer, below the line break in the answer).

fflush() flushes buffers, when it's used in a contex like this, it's typically used in an attempt to flush the input buffer stdin. Thus there's nothing "pending" the buffer is in a clean state.

Specifically in this program there's no point to it at all. There are no input operations, thus it will do nothing*.

*Of course "undefined behavior" means anything could happen so it could do something, but should the platform support this and it does clear the buffer as expected, there will be no obvious effect to the code.

Community
  • 1
  • 1
Mike
  • 40,613
  • 26
  • 100
  • 171
  • 1
    Well, as you said, it's UB, so it might very well do something (like nasal demons and so forth). But on platforms where `fflush(stdin)` empties the input buffered in the stream, it will indeed do nothing here. – sepp2k Feb 23 '14 at 14:08
  • @sepp2k - Ya know, I thought that after I wrote it... but then I thought "nah, no one will call me on that". ;) yes, you are of course correct that it could do *something* as it's UB. – Mike Feb 23 '14 at 14:10
2

In this specific case fflush(stdin) is used to overwrite the contents of ptr. Any function is likely to overwrite dangling stack pointer. It looks like fflush(stdin) was chosen arbitrarily. I'm getting similar results with this:

#include<stdio.h>

int* call(void);
int* dummy(void);

int main(){
    int *ptr;
    ptr=call();
    printf("%d\n",*ptr);
    dummy();
    printf("%d\n",*ptr);
    return 0;
}

int* dummy(void) {
    int x=0;
    return &x;
}

int* call(void){
    int x=25;
    ++x;
    return &x;
}
Piotr Praszmo
  • 16,785
  • 1
  • 51
  • 60
  • This code is broken like the original: UB due to use of the address of an object that has gone out of scope (via `&x` ). – Jens Feb 23 '14 at 14:32
  • 1
    @Jens I believe that's the point. It's supposed to demonstrate the dangers of dangling pointers. – Piotr Praszmo Feb 23 '14 at 14:34
0

The purpose of fflush is to clean or flush the buffer generated due to its argument.for example

char ch [10];
scanf("%s",ch); // here scanf finishes taking input after tou pressed enter and that enter may still remain in keyboardbuffer
fflush(stdin); //flushes that enter which is in keyboard buffer

Now suppose if we write ch to a file using FILE pointer we will have desired output on file. If you dont use

fflush(stdin);

And supose you want to write ch into a file then each each.time you write then it will be written in new line in file due to the effect of enter which is in keyboard buffer and is not desired.

OldSchool
  • 1,973
  • 4
  • 18
  • 39
  • 1
    This is undefined behavior and even if it wasn't, there are no input operations in the given code, so using `fflush` to clear remaining input would be absolutely pointless. I'm also not sure what you mean by "generated due to its argument" in your first sentence. – sepp2k Feb 23 '14 at 14:14
  • By "generated due to its argument" i mean that we use argument whose buffer we want to clean – OldSchool Feb 23 '14 at 14:23
  • And iam specifying function of fflush by my own example – OldSchool Feb 23 '14 at 14:24
  • 1
    You're *claiming* that `fflush(stdin)` has a certain behavior. However the standard does not define the behavior of `fflush` on input streams, so it is in fact undefined behavior. Note that `fflush(stdin)` will simply not do anything at all on many platforms, so your example won't do what you claim it does on those platforms. – sepp2k Feb 23 '14 at 14:30
  • But i have been taught that its behaviour is certain when something is there in the buffer and it will simply clean the buffer but when buffer is already empty then it may have UB – OldSchool Feb 23 '14 at 14:37
  • 1
    Then you have been taught wrong. Look at this: https://ideone.com/bvW2m5 . If `fflush` did what you say it does (on all platforms), the value of `next` should be `'e'` here, but it is `'\n'`. – sepp2k Feb 23 '14 at 14:39
  • Please give me a more example i am unable to understand it – OldSchool Feb 23 '14 at 14:58