Struct Foo{ char *pName;};main(){ struct Foo *obj = malloc(sizeof(struct Foo)); clrscr(); strcpy(obj->pName,"Your Name"); printf("%s", obj->pName);}

A) Name
B) compile error
C) Your Name
D) Runtime error

Showing Answers 1 - 13 of 13 Answers

Amit

  • Dec 22nd, 2005
 

Why runtime error ..explain pls..

  Was this answer useful?  Yes

runtime error or segmentation fault,

here malloc statement allocates on the wrapper i.e. struct Foo, it doesn't allocate any memory to the internal members.

thats why when strcpy tries to copy "Your Name" to pName, it dumps, because no memory is allocated to pName.

C_learner

  • Jan 18th, 2006
 

WELL ANSWER IS C

It prints Your Name on my compiler....I dont see any reasons why it shouldn't....even a char pointer prints until it finds the end of line....

  Was this answer useful?  Yes

Benz

  • Mar 29th, 2006
 

Hello every one

For this question the answer is definitely (B)

because when i compile this it gets an compile error.

should be this

struct Foo *obj = (Foo*)malloc(sizeof(struct Foo));

to get it compiled!

  Was this answer useful?  Yes

Hari

  • Sep 26th, 2006
 

Bhaskar is correct

  Was this answer useful?  Yes

dasam

  • Mar 30th, 2007
 

Run time error or segmentation.

since, the memory allocated to obj is only 4 bytes. but we are trying to copy 10 bytes of memory.

  Was this answer useful?  Yes

yba

  • Jul 10th, 2007
 

If we assume you have a compiler that isn't strictly enforcing Ansi-C, and you make it to having an executable, the interviewer probably wants you to realize why this code is in error.

The call to malloc() allocates enough space for the structure, which contains exactly one pointer.  The value of that pointer can be anything, you never initialize it, and whereas calloc() would guarantee it was zero, malloc() is not required to do so.  Your program could go ahead and print "Your Name".  It could also take a fault during the strcpy.  It is an error either way, because you used an uninitialized pointer.  C doesn't do any error-checking for you here.  We are sure you are going to trash 10 bytes, but from the source code we cannot tell what 10 bytes, or whether they are in your address space, or exactly what damage you will do.

  Was this answer useful?  Yes

Hi Everyone,

It seems simple but tricky. by allocating memory of size struct Foo does  not allocate memory if there is any pointer variable in structure itself.

Please allocate memory to that variable seperately to get gloing.

struct Foo *obj = (Foo*)malloc(sizeof(struct Foo));// will allocate sufficient memory to hold Foo structure object

//allocate memory for pName;

obj->pName = (char*)calloc(10,10);// or whatever size

strcpy(obj->pName,"Your Name");

printf("%s", obj->pName);

This will not crash !! happy coding.....

Regards,
Kapil

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions