Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4124470
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:44:42+00:00 2026-05-20T23:44:42+00:00

I have been working on a question in c++ but im stuck in a

  • 0

I have been working on a question in c++ but im stuck in a part.
It is a long code but I will only post a small part of it where i am stuck now. Here it is;

char* x = (char*)malloc(sizeof(char));
char y = (char)malloc(sizeof(char));
y = *x;

The problem is when I do it, if X points to a char named “HELLO”, when i print y it only prints ‘H’ letter.

I want to copy the whole word that char* has into a char variable. Also I dont know the sizes of the char’s, because they are given by the user. So the length could be anything. I tried strcpy() but couldnt managed to solve the problem.

Any help appreciated.

**Thanks for all the comments. Now I decided to use std::string according to your comments. And I guess Im never gonna use malloc again. Now I have to go back to code and change everything according to string and check if everything works.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:44:43+00:00Added an answer on May 20, 2026 at 11:44 pm

    To paraphrase Inigo Montoya, I don’t think this code means what you think it means.

    malloc(sizeof(char))

    This allocates a certain amount of memory. How much memory is allocated is dictated by malloc‘s parameter. In this case you’re passing sizeof(char) which is, by definition, one byte exactly. Therefore, you are allocating one byte of memory.

    If you insist on using malloc (more on this later) then what you should be doing is figuring out how long the string you want to store is, add one more byte for the NULL terminator, and then malloc that much. In the case of the string Hello, world. which is 13 characters, the corresponding call would be:

    malloc(14)

    Next:

    char y = (char)malloc(sizeof(char));

    malloc returns a pointer to the memory it allocated for you. y in this case isn’t a pointer, it’s just a char. The two aren’t the same. It compiles and appears to work because you use the bludgeoning tool known as a C-style cast: (char)malloc(...). This tells the compiler, “I know I’m pointing the gun at my foot. Just do what I tell you and don’t complain.” Which it dutifully does. But you’re doing the wrong thing for several reasons:

    1. malloc returns a pointer but you’re trying to cast it to a char
    2. You only allocated 1 byte but you assumed you were allocating a whole string’s worth of memory
    3. y is just a char but you treat it as if it were a whole string.

    So if you again insist on using malloc, you need to do something like this:

    static const char* HELLO = "Hello, malloc.";
    
    char* x = malloc(strlen(HELLO)+1);
    strcpy(x, HELLO);
    char* y = malloc(strlen(x)+1);
    strcpy(y, x);
    

    But, you shouldn’t be using malloc at all in C++. Instead, you should be using std::string:

    std::string x = "Hello, string.";
    std::string y = x;
    

    This is better because:

    1. strings manage thier own memory. You don’t leak memory like you did in your code.
    2. string is type-safer. You violated type safety when you cast the return from malloc to a char. You can do it, but its not a char. That’s bad. string won’t let you shoot yourself like this without great effort.
    3. It’s less code. The best code is the code you never write.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

a very simple question I am afraid but I have been stuck for days
Have been working on this question for a couple hours and have come close
I have been working on a project on and off, but I haven't touched
I have been working with Zend for a few months now and am at
I have been working within another question on this site and have almost completed
I’ve been working on a few small scale Access projects that have turned large
I have been working with ASP.NET MVC for a couple of months now and
I am sure this is a simple question but have not been able to
I have been working with SQL Server as a Developer a while. One thing
I have been working on a large java application. It is quite parallel, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.