char

c++ new/delete and char *

醉酒当歌 提交于 2020-11-30 04:58:29
问题 Can anyone help me, why I'm getting an error message while trying to free the allocated memory: Heap corruption detected. CTR detected the application wrote the memory after end of heap buffer. char *ff (char *s){ char *s1 = new char [strlen(s)]; strcpy(s1, s); return s1; } int _tmain(int argc, _TCHAR* argv[]) { char *s = new char [5]; strcpy(s, "hello"); char *s2 = ff(s); delete []s; // This works normal delete []s2; // But I get an error on that line return 0; } 回答1: char *s = new char [5];

c++ new/delete and char *

好久不见. 提交于 2020-11-30 04:58:07
问题 Can anyone help me, why I'm getting an error message while trying to free the allocated memory: Heap corruption detected. CTR detected the application wrote the memory after end of heap buffer. char *ff (char *s){ char *s1 = new char [strlen(s)]; strcpy(s1, s); return s1; } int _tmain(int argc, _TCHAR* argv[]) { char *s = new char [5]; strcpy(s, "hello"); char *s2 = ff(s); delete []s; // This works normal delete []s2; // But I get an error on that line return 0; } 回答1: char *s = new char [5];

Why does it prints that the size of char is 4 byte? [duplicate]

时光毁灭记忆、已成空白 提交于 2020-11-29 21:06:44
问题 This question already has answers here : malloc in C: same sizeof before and after? (5 answers) Determine size of dynamically allocated memory in C (15 answers) Closed 6 days ago . In this program I have located char pointer. I have located 1 byte of memory in heap. But when I print its size it shows 4 bytes. please help. //CODES// #include <stdio.h> #include <stdlib.h> int main(){ char *ptr; ptr=(char*)malloc(1); printf("The size is %d.",sizeof(ptr)); } 来源: https://stackoverflow.com

How to compare a char?

北城以北 提交于 2020-11-26 06:41:08
问题 I am learning c. I have a question. Why doesn't my program work? #include<stdio.h> #include<conio.h> #include<stdlib.h> char cmd; void exec() { if (cmd == "e") { printf("%c", cmd); // exit(0); } else { printf("Illegal Arg"); } } void input() { scanf("%c", &cmd); exec(); } int main() { input(); return 0; } I insert a "e" but it says illegal arg. cmd is not equal to "e". Why? I set cmd with scanf to "e". 回答1: First of, in C single quotes are char literals, and double quotes are string literals.