-2

I'm preparing for an interview and came across a practice question that I could not figure out. Here is the code:

#include <stdio.h>

int main()
{
    char *p,*q;
    p=(char *)malloc(25);
    q=(char *) malloc(25);
    strcpy(p,"amazon" );
    strcpy(q,"hyd");
    strcat(p,q);
    printf("%s\n",p);
}

I compiled it and executed it, but it worked just fine. There is a problem with the code, which makes me suspect it has something to do with uninitialized variables. The output is amazonhyd like it should be. Could someone let me know what I'm missing?

edit: This is a practice interview question, and the question is asking what is wrong with in this code.

  • 3
    what makes you think that "There is a problem with the code" ? – artm Jan 08 '17 at 03:39
  • You've omitted `#include ` so the compiler has to work in C90 mode and assumes `malloc()` returns an `int`, but if you're on a 64-bit system, the value it returns is not the same size as an `int`, so you run into all sorts of problems. Less seriously, you omitted `#include ` too. However, the assumptions made by C90 mode are unlikely to affect the operation of the string functions. – Jonathan Leffler Jan 08 '17 at 03:43
  • You might be interested in [this question about casting the result of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Some programmer dude Jan 08 '17 at 03:44
  • Besides the header-file problem, the question might be about memory leaks. Without more context about the code and the interview question it's really hard to tell what is the expected answer. – Some programmer dude Jan 08 '17 at 03:46
  • For style reasons I would ask for functions to have prototypes even in their definitions, but that's not a "problem". – Kerrek SB Jan 08 '17 at 03:47
  • Oh, and since this is C you really should use `int main(void)` to say that `main` doesn't take any arguments. Also note that not having an explicit `return` statement can lead to errors on older compilers (that's not up to date with newer standards of C). – Some programmer dude Jan 08 '17 at 03:52
  • @Someprogrammerdude Thank you for showing me the link, it was useful on reviewing pointers. – JJDoeCoder Jan 08 '17 at 04:27

1 Answers1

5

There are missing includes.

The malloc function requires stdlib.h, while strcpy and strcat require string.h. Without them, these functions are assumed to return in int.

This is particularly problematic for malloc if sizeof(int) != sizeof(void *). Because the implied definition does not match the actual definition, you invoke undefined behavior. One of the ways for undefined behavior to manifest itself is that the program appears to work properly. However, this behavior is not guaranteed.

For the program to be conforming, add the following at the top.

#include <stdlib.h>
#include <string.h>

Also, this program allocates memory but does not free it. Each call to malloc should have a corresponding call to free. Otherwise, you have a memory leak.

Add the following after the call to printf to patch the memory leak.

free(p);
free(q);
dbush
  • 205,898
  • 23
  • 218
  • 273
  • Thank you for reminding me about headers and memory leaks! While I do not have the answer for this practice question, your answer is clear and constructive. – JJDoeCoder Jan 08 '17 at 04:26