Char *f(void){ char *a; a=malloc(sizeof(char)*10); strcpy(a,"hello World"); return a; }int main(){ char *b; b = f(); printf("%s",b); }Is there any error in this code?

Showing Answers 1 - 24 of 24 Answers

vishwajith

  • Dec 15th, 2006
 

THERE IS NO ERROR IN THE GIVEN CODE.

The output will be, hello World .

  Was this answer useful?  Yes

vistor

  • Dec 15th, 2006
 

buffer overflow. should be a = malloc(sizeof(char)*12); "hello world" is 11 characters, also need one for trailing "".

  Was this answer useful?  Yes

ameer

  • Dec 16th, 2006
 

buffer overflow. should be a = malloc(sizeof(char)*12); "hello world" is 11 characters, also need one for trailing "".

  Was this answer useful?  Yes

rajat goel

  • Dec 17th, 2006
 

In most cases it will display 'hello world' but not always as it might happen that by the time the control returns to main the memory block alloted to 'hello world' n=might be used by some other application ,, remeber the string is of length 12 ( including space and character) and it has illegally got that extra space for two bytes so if os allots it to some other application or code than we might get a garbage value...

  Was this answer useful?  Yes

Another point to note that the function malloc will return a void pointer which should be explicitly typecasted into char *. Althouth this wouldn't generate any error (only a warning), but it should be taken care for bigger programs.

  Was this answer useful?  Yes

mekala

  • Dec 22nd, 2006
 

Use the header files , ,

  Was this answer useful?  Yes

kbjarnason

  • Jul 1st, 2010
 

First error: no prototype for malloc in scope, meaning that the compiler will think malloc returns an int, not a pointer, which on some systems will dramatically pooch your code as soon as you do the assignment to a.

Second error, you don't allocate space for the terminating character in the string - the thing that tells the string functions (and printf) where the string ends.  Any attempt to read the result _as a string_ will run off the end, with unpredictable results.

Third error, there's no prototype in scope for printf; variadic functions need prototypes in scope or strange things can happen.

Fourth error: no return value from main.  In C99 you can get away with this, but not in C90.

Fifth error: you do not free the memory you allocate.  It's your house, you should do your own housecleaning.

Sixth error: no test to see if malloc actually succeeded before attempting to write to the buffer (and subsequently read from it).

Seventh error: this is not technically an _error_, but it is ridiculously poor style and suggests a lack of understanding of C types and sizes... there is no need to multiply by sizeof(char); sizeof(char) is by definition 1.  What's 10 * 1?  Right - 10. 

That help?

@vipin gupta: Another point to note that the function malloc will return a void pointer which should be explicitly typecasted into char *. Althouth this wouldn't generate any error (only a warning) but it should be taken care for bigger programs.

This is only true for C++ (in which case you shouldn't be using malloc() at all); in C, pointers to void can implicitly be converted to or from any other object pointer type. Explicitly casting the result of malloc() is considered bad practice, since doing so can suppress a useful compiler diagnostic if you forget to #include stdlib.h. 

The canonical form for calling malloc is
<pre>
#include <stdlib.h>
...
T *p = malloc(sizeof *p * number_of_elements);
</pre>

for any type T.  While it is true that sizeof(char) is 1 by definition making the sizeof *p in the above call redundant, the above form will be useful if you ever have to change the type from char to, say, wchar_t.  IOW, going from

<pre>
char *a = malloc(sizeof *a * number_of_elements);
</pre>

to

<pre>
wchar_t *a = malloc(sizeof *a * number_of_elements);
</pre>

only requires changing the type of a; you don't have to change anything in the malloc() call itself. 

  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