2

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

5
  • Use std::string for the semantics you would expect of a string.
    – chris
    Commented Jan 8, 2013 at 12:19
  • 1
    You are confusing p = "Hello" with strcpy(p, "hello) Commented Jan 8, 2013 at 12:21
  • It's a C code, so char * I need to use, secondly I was not getting cause of exception. Commented Jan 8, 2013 at 12:22
  • @PranitPKothari, If it's C, why is it tagged with C++?
    – chris
    Commented Jan 8, 2013 at 12:24
  • @chris, Sorry, edited Tags. Commented Jan 8, 2013 at 12:26

5 Answers 5

6

p = "Hello"; makes p point to a string literal and discards the previously assigned value. You can't free a string literal. You can't modify it.

If you want p to hold that string, just use

char* p = "Hello";

or

char p[] = "Hello";

if you plan on modifying it.

Neither requires free.

2
  • 1
    So what will happen to memory allocated with malloc? Commented Jan 8, 2013 at 12:20
  • 3
    @PranitPKothari it will leak. Commented Jan 8, 2013 at 12:20
5

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.

2
p = "Hello";
free(p);

Since Hello is statically allocated, you cannot free it. I'm not sure why you allocate some memory just to throw the pointer away by changing it to another pointer, but that has no effect. If you do this:

int i = 1;
i = 2;

i has no memory that it once held a 1, it holds a 2 now. Similarly, p has no memory that it once held a pointer to some memory you allocated. It holds a pointer to an immutable constant now.

1

this is a nice one. the char sequence "hello" is constant and therefore placed niether on the heap nor the stack, but in the .bss/.data segment. when you do p="hello" you make p point to the address of the string hello in that segment instead of the memory you alocated on the heap using malloc. when you go to free p it tries to free the memory in the .bss/.data segment, and naturally fails.

what you probably want is something like strcpy(p,"hello"); which goes over every char in "hello" and places it in the memory pointed to by p. essentially creating a copy of the string "hello" at memory address p.

0

If you want to copy the contents of the string "Hello" to the memory you allocated, you need to use strcpy:

strcpy(p, "Hello");

The line

p = "Hello";

assigns the address of the string literal "Hello" to the pointer p, overwriting the pointer value that was returned from malloc, hence the crash when you call free.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.