1)#include <iostream.h>
int main()
{
int *a, *savea, i;
savea = a = (int *) malloc(4 * sizeof(int));
for (i=0; i<4; i++) *a++ = 10 * i;
for (i=0; i<4; i++) {
printf("%d\n", *savea);
savea += sizeof(int);
}
return 0;
}
2)#include <iostream.h>
int main()
{
int *a, *savea, i;
savea = a = (int *) malloc(4 * sizeof(int));
for (i=0; i<4; i++) *a++ = 10 * i;
for (i=0; i<4; i++) {
printf("%d\n", *savea);
savea ++;
}
return 0;
}
The output of this two programs will be different why?
Answer Posted / d289
because when incremented by size of int(4), point to invalid
position in the int array. Hence it will print out only one
correct out put for the first element and garbage for the
rest in the first program while for the second program it
will print out the contents of the int array correctly.
Is This Answer Correct ? | 3 Yes | 0 No |
Post New Answer View All Answers
Explain how overloading takes place in c++?
What is the benefit of encapsulation?
Why iomanip is used in c++?
Which programming language should I learn first?
Explain container class.
Write a program using GUI concept for the scheduling algorithms in Operating system like SJF,FCFS etc..
Difference between class and structure.
What happens when the extern "c" char func (char*,waste) executes?
How the memory management in vectors are being done. What happens when the heap memory is full, and how do you handle it ?
Is empty stack c++?
Do you know what are pure virtual functions?
What is prototype in c++ with example?
What are the two shift operators and what are their functions?
Discuss the effects occur, after an exception thrown by a member function is unspecified by an exception specification?
In c++, what is the difference between method overloading and method overriding?