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

What are smart pointers?

672


What is the maximum value of a unsigned char a) 255 b) 256 c) 128

636


Inline parameters : What does the compiler do with the parameters of inline function, that can be evaluated in runtime ?

2016


Do the parentheses after the type name make a difference with new?

655


What is a linked list in c++?

552






Is java based off c++?

536


What are the vectors in c++?

583


Explain how a pointer to function can be declared in C++?

584


What is the use of bit fields in structure declaration?

544


What is == in programming?

619


Difference between a homogeneous and a heterogeneous container

664


Explain the concept of memory leak?

631


What do the header files usually contains?

636


Explain what you mean by a pointer.

636


What is algorithm in c++ programming?

591