What is wrong with this code:int cp(void){ char *s; s = malloc(12); strcpy(s, "Best School"); return (0);}
Question
What is wrong with this code:int cp(void){ char *s; s = malloc(12); strcpy(s, "Best School"); return (0);}
Solution
There are several issues with this code:
-
The function
cpis declared to return anint, but it doesn't return any meaningful value. It always returns0, which usually indicates success. However, there's no way to indicate failure (for example, ifmallocfails). -
The string "Best School" is 11 characters long, but you also need space for the null terminator (
\0), which makes it 12 characters long. So, you need to allocate 13 bytes of memory, not 12. -
The pointer
sis local to the functioncp. Once the function returns,sgoes out of scope and you lose the reference to the allocated memory. This is a memory leak. -
There's no error checking for the
malloccall. Ifmallocfails, it returnsNULLand the subsequentstrcpycall would result in undefined behavior. -
The allocated memory is not freed, which leads to a memory leak.
Here's a corrected version of the function:
#include <stdlib.h>
#include <string.h>
int cp(char **out) {
char *s = malloc(13);
if (s == NULL) {
return -1;
}
strcpy(s, "Best School");
*out = s;
return 0;
}
In this version, the function takes a pointer to a pointer to char. It allocates memory and copies the string into the new memory. If everything is successful, it stores the pointer to the new string in *out and returns 0. If malloc fails, it returns -1. The caller is responsible for freeing the memory when it's no longer needed.
Similar Questions
What is the output of the following code?char str1[] = "C programming";char str2[20];strcpy(str2, str1);printf("%s\n", str2);
#ifndef main.h#define main.h#include <stddef.h>int _putchar(char c);int _islower(int c);int _isalpha(int c);int _abs(int n);int _isupper(int c);int _isdigit(int c);int _strlen(char *s);void _puts(char *s);char *_strcpy(char *dest, char *src);int _atoi(char *s);char *_strcat(char *dest, char *src);char *_strncat(char *dest, char *src, int n);char *_strncpy(char *dest, char *src, int n);int _strcmp(char *s1, char *s2);char *_memset(char *s, char b, unsigned int n);char *_memcpy(char *dest, char *src, unsigned int n);char *_strchr(char *s, char c);unsigned int _strspn(char *s, char *accept);char *_strpbrk(char *s, char *accept);char *_strstr(char *haystack, char *needle);#endif main.h~ ~ ~
In the following code, which of the variables, a, b, c, d and e are strings/pointer-to-strings when code is at /* A */?#include <string.h>#include <stdio.h>void f(char *p) { p = "hello";}int main() { char a[16], c[16], *d, e[16]; char b[16] = {'h', 'e', 'l', 'l', 'o'}; strcpy(a, "hello"); f(c); d = "hello"; scanf("%s", e); // assume that the end-user entered hello\n /* A */ return 0;}
#include<stdio.h>#include<string.h>struct player { char pname[20];}pl;char* play(struct player *temp_pl){ strcpy(temp_pl->pname, "kohli"); return temp_pl->pname;}int main(){ strcpy(pl.pname, "dhoni"); printf("%s %s", pl.pname, play(&pl)); return 0;}
Consider these two statements: char v[6]; strcpy(v, "string");The best description of the situation after these two statements is that:Group of answer choicesThe call to strcpy() will corrupt memory and may result in a program run-time error at some later point in the program's executionA run-time array overflow will be detected, and the program will be halted and an error message printedThe call to strcpy() will be truncated, and only "tring" will be copied into vThe call to strcpy() is completely fine, and v will have the desired value of "string"The call to strcpy() will be truncated, and only "strin" will be copied into v
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.