malloc and free

R

rockdale

I am doing a pure c programming and my code is following:

char* temp = (char*) malloc(10*sizeof(char));
temp="0123456789";
free(temp);

the free(temp) line got the error:
File: f:\\dd\vctools\crt_bld\self_x86\crt\src\dbgheap.c
Line: 1317
Expression: _CrtIsValidHeapPointer(pUserData)

if I change the assignment of temp as following
temp[0] = 'a';
temp[1] = 'a';
temp[2] = 'a';
temp[3] = 'a';
temp[4] = 'a';
temp[5] = 'a';
temp[6] = '\0';

anybody can explain this to me.

I am using Visual Studio 2008 on windows xp.

Is that a Visual Studio problem or it is my code?

thanks in advance.
 
R

Ron AF Greve

Hi,
rockdale said:
I am doing a pure c programming and my code is following:

char* temp = (char*) malloc(10*sizeof(char));
temp="0123456789";

You are assigning the result of "0123456789" (which is a pointer to that
string somewhere in memory) to temp (not on the heap) then trying to free
it. VC recognizes that you are freeing memory that was not allocated.

Use strcpy instead.
Also reserve room for the zero byte at the end
char* temp = (char*) malloc(11*sizeof(char) );

strcpy( temp, "0123456789" );

Regards, Ron AF Greve

the free(temp) line got the error:
File: f:\\dd\vctools\crt_bld\self_x86\crt\src\dbgheap.c
Line: 1317
Expression: _CrtIsValidHeapPointer(pUserData)

if I change the assignment of temp as following
temp[0] = 'a';
temp[1] = 'a';
temp[2] = 'a';
temp[3] = 'a';
temp[4] = 'a';
temp[5] = 'a';
temp[6] = '\0';

anybody can explain this to me.

I am using Visual Studio 2008 on windows xp.

Is that a Visual Studio problem or it is my code?

thanks in advance.
 
R

rockdale

Great. I know the assignment is something wrong but do not know why.
Thanks for your explaination.

I am doing a pure c programming and my code is following:
char* temp = (char*) malloc(10*sizeof(char));
temp="0123456789";

You are assigning the result of "0123456789" (which is a pointer to that
string somewhere in memory) to temp (not on the heap) then trying to free
it. VC recognizes that you are freeing memory that was not allocated.

Use strcpy instead.
Also reserve room for the zero byte at the end
char* temp = (char*) malloc(11*sizeof(char) );

strcpy( temp, "0123456789" );

Regards, Ron AF Greve
the free(temp) line got the error:
File: f:\\dd\vctools\crt_bld\self_x86\crt\src\dbgheap.c
Line: 1317
Expression: _CrtIsValidHeapPointer(pUserData)
if I change the assignment of temp as following
temp[0] = 'a';
temp[1] = 'a';
temp[2] = 'a';
temp[3] = 'a';
temp[4] = 'a';
temp[5] = 'a';
temp[6] = '\0';
anybody can explain this to me.
I am using Visual Studio 2008 on windows xp.
Is that a Visual Studio problem or it is my code?
thanks in advance.
 
B

Bo Persson

Ron said:
Hi,


You are assigning the result of "0123456789" (which is a pointer to
that string somewhere in memory) to temp (not on the heap) then
trying to free it. VC recognizes that you are freeing memory that
was not allocated.
Use strcpy instead.
Also reserve room for the zero byte at the end
char* temp = (char*) malloc(11*sizeof(char) );

strcpy( temp, "0123456789" );

Or use std::string instead, and it will take care of everything for
you, including freeing the buffer afterwards. :)

std::string temp ="0123456789";



Bo Persson
 
C

Chinson

although you ask question in the wrong place, the following maybe help
you:

allocate one memory in heap, whose size is 10*sizeof(char). If
succeed, return one valid address to temp, for example 0xf0123456;

temp ---------> 0xf0123456:///////////////////////
//////////////////////

When compling, "0123456789" is stored in constant area, for example
0x00401234 assign to temp

0x00401234 is not a valid heap pointer, so pop up error:
Great. I know the assignment is something wrong but do not know why.
Thanks for your explaination.

I am doing a pure c programming and my code is following:
char* temp = (char*) malloc(10*sizeof(char));
temp="0123456789";

You are assigning the result of "0123456789" (which is a pointer to that
string somewhere in memory) to temp (not on the heap) then trying to free
it. VC recognizes that you are freeing memory that was not allocated.

Use strcpy instead.
Also reserve room for the zero byte at the end
char* temp = (char*) malloc(11*sizeof(char) );

strcpy( temp, "0123456789" );

Regards, Ron AF Greve
the free(temp) line got the error:
File: f:\\dd\vctools\crt_bld\self_x86\crt\src\dbgheap.c
Line: 1317
Expression: _CrtIsValidHeapPointer(pUserData)
if I change the assignment of temp as following
temp[0] = 'a';
temp[1] = 'a';
temp[2] = 'a';
temp[3] = 'a';
temp[4] = 'a';
temp[5] = 'a';
temp[6] = '\0';
anybody can explain this to me.
I am using Visual Studio 2008 on windows xp.
Is that a Visual Studio problem or it is my code?
thanks in advance.
 
B

Bo Schwarzstein

although you ask question in the wrong place, the following maybe help
you:

allocate one memory in heap, whose size is 10*sizeof(char). If
succeed, return one valid address to temp, for example 0xf0123456;

temp ---------> 0xf0123456:///////////////////////
                                       //////////////////////

 > > temp="0123456789";

When compling, "0123456789" is stored in constant area, for example
0x00401234 assign to temp

0x00401234 is not a valid heap pointer, so pop up error:


Great. I know the assignment is something wrong but do not know why.
Thanks for your explaination.
I am doing a pure c programming and my code is following:
char* temp = (char*) malloc(10*sizeof(char));
temp="0123456789";
You are assigning the result of "0123456789" (which is a pointer to that
string somewhere in memory) to temp (not on the heap) then trying to free
it. VC recognizes that you are freeing memory that was not allocated.
Use strcpy instead.
Also reserve room for the zero byte at the end
char* temp = (char*) malloc(11*sizeof(char) );
strcpy( temp, "0123456789" );
Regards, Ron AF Greve
the free(temp) line got the error:
File: f:\\dd\vctools\crt_bld\self_x86\crt\src\dbgheap.c
Line: 1317
Expression: _CrtIsValidHeapPointer(pUserData)
if I change the assignment of temp as following
temp[0] = 'a';
temp[1] = 'a';
temp[2] = 'a';
temp[3] = 'a';
temp[4] = 'a';
temp[5] = 'a';
temp[6] = '\0';
anybody can explain this to me.
I am using Visual Studio 2008 on windows xp.
Is that a Visual Studio problem or it is my code?
thanks in advance.- Hide quoted text -

- Show quoted text -

temp is a pointer which hold a pointer on heap. In fact "0123456789"
is a const char* typed pointer, it's on PE's static segment, not
dynamics heap, so if you free it ,you will get the wrong.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,589
Members
45,100
Latest member
MelodeeFaj
Top