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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:33:23+00:00 2026-06-17T00:33:23+00:00

I have simple code, #include stdafx.h #include <malloc.h> int main() { char *p =

  • 0

I have simple code,

#include "stdafx.h"
#include <malloc.h>
int main()
{
  char *p = (char*) malloc(10);
  p = "Hello";

  free(p);

  return 0;
}

This code is giving run time exception while terminating. Below is error snippiest,


Microsoft Visual C++ Debug Library

Debug Assertion Failed!

Program: …\my documents\visual studio 2010\Projects\samC\Debug\samC.exe
File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgheap.c
Line: 1322

Expression: _CrtIsValidHeapPointer(pUserData)

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)

Abort Retry Ignore

  • 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-17T00:33:24+00:00Added an answer on June 17, 2026 at 12:33 am

    This is how you write a string in the memory allocated by malloc to a char pointer.

    strcpy(p, "Hello");
    

    Replace the line

    p = "Hello";
    

    with the strcpy one & your program will work fine.

    You also need to

    #include <string.h>
    

    malloc returns a pointer to allocated memory. Say the address is 95000 (just a random number I pulled out).

    So after the malloc – p will hold the address 95000
    The p containing 95000 is the memory address which needs to be passed to free when you are done with the memory.

    However, the next line p = "Hello"; puts the address of the string literal “Hello” (which say exists at address 25000) into p.

    So when you execute free(p) you are trying to free 25000 which wasn’t not allocated by malloc.

    OTOH, when you strcpy, you copy the string “Hello” into the address starting at p (i.e. 95000). p remains 95000 after the strcpy.

    And free(p) frees the right memory.

    You can also avoid the malloc and use

    char *p = "Hello";
    

    However, in this method, you cannot modify the string.

    i.e. if after this you do *p = 'B' to change the string to Bello, it becomes an undefined operation. This is not the case in the malloc way.

    If instead, you use

    char p[] = "Hello";
    

    or

    char p[10] = "Hello";
    

    you get a modifiable string which need not be freed.

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

Sidebar

Related Questions

I have this simple code #include <stdio.h> #include <OpenGL/glext.h> #include <OpenGL/gl.h> int main (int
I have this simple code: #include <iostream> #include <fstream> using namespace std; int main(void)
I have this simple code: #include <stdio.h> #include <time.h> int main(){ enum Days {
I have this very simple code: #include <stdio.h> #include <math.h> int main() { long
I have a very simple piece of code: #include <stdio.h> #include <glib.h> int main(int
I have a simple problem; Here is the code : #include<stdio.h> main(){ int input;
I have a very simple c code: #include<stdio.h> int main() { enum boolean{true,false}; boolean
Suppose that we have a simple c code: #include <stdio.h> #include <stdlib.h> int main(int
I have a simple piece of c++ code: #include <iostream> using namespace std; int
I have the very simple following code: main.cpp #include ui_library_browser.h #include <QtGui/QApplication> #include StartWindow.h

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.