开发者

Strange compiler complaint when using similar code

For a project, I have 开发者_如何学JAVAto ask the user for a file name, and I'm reading it in character by character using getchar.

From the main, I call the function char *coursename= introPrint(); //start off to print the usage directions and get the first bit of input. That function is defined as

char *introPrint(){
  int size= 20;
  int c;
  int length=0;
  char buffer[size];


  //instructions printout, cut for brevity

  //get coursename from user and return it
  while ( (c=getchar()) != EOF && (c != '\n') ){
    buffer[length++]= c;
    if (length==size-1)
      break;

  }
  buffer[length]=0;
  return buffer;
}

This is basically identical code I wrote to ask the user for input, replace the character echo with asterisks, then print out the results. Here, though, I'm getting a function returns address of local variable warning for the return statement. So why am I getting no warnings from the other program, but trigger one for this code?


You're returning the address of buffer, which is destroyed when it goes out of scope (when the function returns). When you try to use the returned pointer, the behavior of the program is undefined: the program may reuse the memory where the instance of buffer was previously located for other purposes.

Use a static buffer, allocate one with malloc or let the caller pass in a buffer. In the last case, the function and its caller also need to communicate about the length of the buffer somehow (by an extra argument size, for example).


The warning tells you that you have stack memory (for your buffer object) inside your function and thus you should not access it from outside. Depending on the environment the memory used to save the input may be overwritten by other data when you want to use it.

It is better to define allocate memory to your coursename and then pass a pointer to your function or dynamically allocate the buffer memory.


Because here, you really return pointer to a local (for the function's scope) var:
char buffer[size];

So, after the executing of the function, this array is destroyed and the pointer is invalid and you get undefined behavior.

To fix this, you should allocate the array dynamically, using malloc:

char* buffer = (char*)malloc( size * sizeof( char ) );


Just a guess that you didn't return a pointer to the buffer in the other function.

The problem is that all local variables are destroyed when the function returns. That means that the returned pointer doesn't point anywhere anymore. There is no buffer. Not good!


when you declare char buffer[] it places that buffer on the stack. When the function exits, the stack is popped and the buffer ceases to exist. If you want to return a dynamic buffer, call malloc() (or GlobalAlloc() in windows, or optionally use the new operator in c++).

The static keyword will cause the buffer to exist outside the stack, essentially the same as a global, only not accessible outside the function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜