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?

Answers were Sorted based on User's Feedback



1)#include <iostream.h> int main() { int *a, *savea, i; savea = a = (int *) malloc(4 *..

Answer / uma sankar pradhan

The output differs due to the followinf two statements

savea+=sizeof(int)
and
savea++

savea++
=>savea=savea+1
=>savea=savea+1*(sizeof(int))


savea+=sizeof(int)
=>savea=savea+2
=>savea=savea+2*sizeof(int)

sizeof(int) is called the scalefactor of savea

Is This Answer Correct ?    5 Yes 1 No

1)#include <iostream.h> int main() { int *a, *savea, i; savea = a = (int *) malloc(4 *..

Answer / 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

1)#include <iostream.h> int main() { int *a, *savea, i; savea = a = (int *) malloc(4 *..

Answer / ips

In 1st Case(savea++)
--------------------
The Integer Pointer Is Incremented just Once.(as it Is
Implimented in c/c++).which means the pointer is shifted
4bytes(size of type 'int') ahead.

In 2nd Case(savea+=sizeof(int))
-------------------------------
Here The Statement implies:-
savea+=4;
The above statement says that,the integer pointer is to
be increamented 4times.means,the Pointer now is shifted 16
bytes(4*sizeof type 'int').which is Out of scope of the
integer array in the Programme.

Is This Answer Correct ?    3 Yes 1 No

1)#include <iostream.h> int main() { int *a, *savea, i; savea = a = (int *) malloc(4 *..

Answer / shil chawla

the program will not RUN becoz #include<iostream.h>
does not support "printf()".

Is This Answer Correct ?    1 Yes 1 No

1)#include <iostream.h> int main() { int *a, *savea, i; savea = a = (int *) malloc(4 *..

Answer / mahesh

first program prints sum of saved and size of int

second one prints only the contenst of saved

Is This Answer Correct ?    2 Yes 3 No

Post New Answer

More C++ General Interview Questions

What is the stack?

5 Answers   IBM, ICICI,


Write a program which is required to process the time of a clock in hours and minutes, entered from the keyboard. With this program, there are two requirements for any data entered by a user: 1. The data must be of the correct type (in this case, two ints). 2. The data must be in the correct range: this means that, for the minutes, negative numbers and any number above 59 must be rejected; for the hours, negative numbers and any number above 23 must be rejected. Output error message for invalid data input. Output the time one and a half hour after the time input. i.e. Hour: 22 Min: 32 One and a half hour after 22:32 is 00:02

0 Answers  


Explain virtual destructor?

0 Answers  


Write about the role of c++ in the tradeoff of safety vs. Usability?

0 Answers  


Is C++ case sensitive a) False b) Depends on implementation c) True

0 Answers  






Do we have to use initialization list in spite of the assignment in constructors?

0 Answers  


Explain bubble sorting.

0 Answers  


How do you declare a set in c++?

0 Answers  


Can we sort map in c++?

0 Answers  


How do you print for example the integers 3,2,1,5,4 in a binary tree within the console in format where it looks like an actual binary tree?

0 Answers  


Explain stack & heap objects?

0 Answers  


Write about the stack unwinding?

0 Answers  


Categories