C Programming - Memory Allocation - Discussion
Discussion Forum : Memory Allocation - Find Output of Program (Q.No. 3)
3.
What will be the output of the program?
#include<stdio.h>
#include<string.h>
int main()
{
char *s;
char *fun();
s = fun();
printf("%s\n", s);
return 0;
}
char *fun()
{
char buffer[30];
strcpy(buffer, "RAM");
return (buffer);
}
Answer: Option
Explanation:
The output is unpredictable since buffer is an auto array and will die when the control go back to main. Thus s will be pointing to an array , which not exists.
Discussion:
16 comments Page 1 of 2.
Vishal said:
1 decade ago
The output is Garbage value. Because in the function definition, the character array 'buffer[30]' is declared as local. The scope of the local variable is limited within the blocks. But in the example, we are trying to return the address of a local array. This leads to problem & it is known as 'Dangling Pointer'.
To over come the problem, declare character array as 'static char buffer[30]' instead 'char buffer[30]' or declare character array Globally.
To over come the problem, declare character array as 'static char buffer[30]' instead 'char buffer[30]' or declare character array Globally.
Hoang Pham said:
8 years ago
The return will be "RAM". The reason is very simple.
1. Return value will be copied to another place in stack.
2. Buffer will be free at the end of fun().
This concept is similar to return a local object in Cpp. The object will be copied to another place in a stack and the local object will be freed at the end of the function.
1. Return value will be copied to another place in stack.
2. Buffer will be free at the end of fun().
This concept is similar to return a local object in Cpp. The object will be copied to another place in a stack and the local object will be freed at the end of the function.
Om awasthi said:
1 decade ago
Below code will return ram.
#include<stdio.h>
#include<string.h>
int main()
{
char *s;
char *fun();
s = fun();
printf("%s\n", s);
return 0;
}
char *fun()
{
static char buffer[30];
strcpy(buffer, "RAM");
return (char*)buffer;
}
#include<stdio.h>
#include<string.h>
int main()
{
char *s;
char *fun();
s = fun();
printf("%s\n", s);
return 0;
}
char *fun()
{
static char buffer[30];
strcpy(buffer, "RAM");
return (char*)buffer;
}
Saurabh said:
1 decade ago
DEVC++ is giving the answer RAM. I don't know why and also there is no error because of returning the address of local variable that is buffer, it is just giving the warning but answer is coming.
Narendar kotrangi said:
9 years ago
char *fun()
{
char *buffer;
buffer=(char*)malloc(sizeof(char*));
strcpy(buffer, "RAM");
return (buffer);
}
This should work.
{
char *buffer;
buffer=(char*)malloc(sizeof(char*));
strcpy(buffer, "RAM");
return (buffer);
}
This should work.
Caro said:
1 decade ago
I execute this code and it returns RAM. It is strange. I don't understand why.
Sharat said:
1 decade ago
Buffer array is already a char what is the need to typecast in last statement?
Shilpi said:
1 decade ago
But an array cannot exist in a return statement. It will cause an error.
Ranjit said:
1 decade ago
@Shilpi
Here buffer is basically a pointer, so it can be returned.
Here buffer is basically a pointer, so it can be returned.
Shamu said:
1 decade ago
In order to print "RAM" how the program must be modified ?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers