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 8233485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:08:44+00:00 2026-06-07T18:08:44+00:00

I’m currently attempting to learn the C side of C++. I attempt to malloc

  • 0

I’m currently attempting to learn the C side of C++.

I attempt to malloc a chunk of memory for a char array of 256 and then I assigned it a char* "Hello World!" but when I come to free the object I get an error.

Can anyone please explain to me the error.

#include <exception>
#include <stdexcept>
#include <iostream>

int main()
{
    void* charVoidPointer = malloc( sizeof(char) * 256 ) ;
    charVoidPointer = "Hello World";

    std::cout << (char *)charVoidPointer;
    free (charVoidPointer);
}
  • 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-06-07T18:08:46+00:00Added an answer on June 7, 2026 at 6:08 pm

    “Hello World” is statically allocated by the compiler. It is part of the program and exists at some place addressable by the program; call it address 12.

    charVoidPointer initially points to some place allocated for you by malloc; call it address 98.

    charVoidPointer = “Hello …” causes charVoidPointer to point to the data in your program; address 12. You lose track of address 98 previously contained in charVoidPointer.

    And you can’t free memory not allocated by malloc.

    To demonstrate more literally what I mean:

    void* charVoidPointer = malloc(sizeof(char) * 256);
    printf("the address of the memory allocated for us: %p\n", charVoidPointer);
    charVoidPointer = "Hello World";
    printf("no longer the address allocated for us; free will fail: %p\n",
           charVoidPointer);
    

    What you meant was:

    strcpy(charVoidPointer, "Hello World");
    

    Edit: Example of addressing memory for other types

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
      // an array of 10 int
      int *p = (int*)malloc(sizeof(int) * 10);
    
      // setting element 0 using memcpy (works for everything)
      int src = 2;
      memcpy(p+0, &src, sizeof(int));
    
      // setting element 1 using array subscripts.  correctly adjusts for
      // size of element BECAUSE p is an int*.  We would have to consider
      // the size of the underlying data if it were a void*.
      p[1] = 3;
    
      // again, the +1 math works because we've given the compiler 
      // information about the underlying type.  void* wouldn't have
      // the correct information and the p+1 wouldn't yield the result
      // you expect.
      printf("%d, %d\n", p[0], *(p+1));
    
      free (p);
    }
    

    Experiment; Change the type from int to long, or double, or some complex type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Specifically, suppose I start with the string string =hello \'i am \' me And
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites 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.