C Internship - Apr-2024

If you are looking for an internship in C programming / algorithm areas, kindly run the C code mentioned below in your computer (preferably Linux ubuntu / redhat / fedora / centos / debian / suse, etc…). Your computer will seem to hang after 5 to 10 minutes of running this code or maybe the program will crash. So, run and review the following C-code under Linux platform & identify 10 Issues with this code.

  1. /*
  2.  * Sanfoundry.com Memory Leak Program – India Internship Bangalore
  3.  * Author: Manish Kumar Bhojasia, Location: Bangalore, India
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. #define KB1     1024
  11. #define MEMSIZE (4 * KB1)
  12.  
  13. char *string = "IDENTIFY 10 ISSUES IN THIS MEMLEAK PROGRAM";
  14. char *gstring = "IDENTIFY 10 ISSUES IN THIS MEMLEAK PROGRAM";
  15. char *gptr;
  16. int num;
  17.  
  18. char * f1_alloc(void);
  19. char * f2_copyme(void);
  20. void   f3_free(char *);
  21.  
  22. main()
  23. {
  24.     srand((int)getpid());
  25.     while (1) {
  26.         gptr = f1_alloc();
  27.         strcpy(gptr, string);
  28.         f2_copyme();
  29.         f3_free(gptr);
  30.     }
  31. }
  32.  
  33. char *f1_alloc()
  34. {
  35.     return (malloc(MEMSIZE));
  36. }
  37. char *f2_copyme()
  38. {
  39.     static int len, i = 0;
  40.     char *s;
  41.  
  42.     len = strlen(string);
  43.     s = (char *)malloc(len);
  44.     strncpy(s, string, len);
  45.     i++;
  46.     if (!(i % MEMSIZE))
  47.         printf("i=%d, %s\n", i, s);
  48. }
  49.  
  50. void f3_free(char *p)
  51. {
  52.     num = rand() % 3;
  53.     if (!num)
  54.         free(p);
  55. }

After deep analysis of the above code and running the code in your computer, note down those 10 issues along with line numbers in a document and email that document to [email protected] with subject as “C Programming Internship Application – 10 Bugs Identified“. We will review your submission and if it’s correct, you would be called for an Internship in C Programming / Algorithm at Sanfoundry.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.