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


Please Help Members By Posting Answers For Below Questions

Explain how overloading takes place in c++?

661


What is the benefit of encapsulation?

692


Why iomanip is used in c++?

738


Which programming language should I learn first?

658


Explain container class.

803






Write a program using GUI concept for the scheduling algorithms in Operating system like SJF,FCFS etc..

3467


Difference between class and structure.

794


What happens when the extern "c" char func (char*,waste) executes?

724


How the memory management in vectors are being done. What happens when the heap memory is full, and how do you handle it ?

1937


Is empty stack c++?

612


Do you know what are pure virtual functions?

735


What is prototype in c++ with example?

640


What are the two shift operators and what are their functions?

668


Discuss the effects occur, after an exception thrown by a member function is unspecified by an exception specification?

728


In c++, what is the difference between method overloading and method overriding?

698