Memory allocation with malloc

P

PSN

Hello everyone,

int main()
{
char *pChar;
pChar = (char *)malloc(16);
memcpy(pChar, "AAAAAAAAAAAAAAA", 16);
printf ("***%s***\n", pChar);

realloc(pChar, 28);
memcpy(pChar, "BBBBBBBBBBBBBBBBBBBBBBBBBBB", 28);
printf ("***%s***\n", pChar);

free(pChar);
return 0;
}

Can someone please explain me why does this crash. what am i doing
wrong ??

Thank you for youri time

Prakash
 
P

PSN

Just to add a little more ...

1. Both the printfs print the strings correctly.
2. The crash occurs in the debug function, _CrtIsValidHeapPointer,
when the memory is freed.

Thanks again.
 
P

peter koch

Hello everyone,

int main()
{
char *pChar;
pChar = (char *)malloc(16);
memcpy(pChar, "AAAAAAAAAAAAAAA", 16);
printf ("***%s***\n", pChar);

realloc(pChar, 28);
memcpy(pChar, "BBBBBBBBBBBBBBBBBBBBBBBBBBB", 28);
printf ("***%s***\n", pChar);

free(pChar);
return 0;

}

Can someone please explain me why does this crash. what am i doing
wrong ??
Can you explain why you use malloc when you have the opportunity to
use std::string or std::vector (assuming you program in C++ as you
would otherwise be off-topic)?

realloc often allocates a new buffer, copies the content and frees the
old buffer - e.g.

void* realloc(void* ptr,size_t new_size)
{
void* new_area = malloc(new_size);
memcpy(new_area,ptr,size_allocated(ptr)); // size_allocated is not
an official function
free(ptr);
return new_area;
}

And you forget to update pChar with the result of the realloc call.

/Peter
 
P

PSN

I was always assuming that realloc would update the pointer passed. My
mistake again. I should have read the documentation well.

And i think its better to just use std::string's.

Thanks again.
 
J

Jim Langston

PSN said:
Hello everyone,

int main()
{
char *pChar;
pChar = (char *)malloc(16);
memcpy(pChar, "AAAAAAAAAAAAAAA", 16);
printf ("***%s***\n", pChar);

realloc(pChar, 28);

pChar = realloc(pChar, 28);
memcpy(pChar, "BBBBBBBBBBBBBBBBBBBBBBBBBBB", 28);
printf ("***%s***\n", pChar);

free(pChar);
return 0;
}

Can someone please explain me why does this crash. what am i doing
wrong ??

Shown is the fix, but it would be better to use std::string or std::vector.
 
R

Ron Natalie

PSN said:
Hello everyone,

int main()
{
char *pChar;
pChar = (char *)malloc(16);

Malloc is not declared here.
memcpy(pChar, "AAAAAAAAAAAAAAA", 16);

you don't check to see if malloc fails.
printf ("***%s***\n", pChar);

If I counted right above, You have exactly 16 A's there. Printf
%s expects a null-terminated string which you didn't provide.
realloc(pChar, 28); Ditto.

memcpy(pChar, "BBBBBBBBBBBBBBBBBBBBBBBBBBB", 28);
printf ("***%s***\n", pChar);
I'm not counting the B's but I bet you have the same problem.
 
P

Peter

Hello everyone,

use operators new and delete.
There is no need anymore to use malloc()/free().
The advantage of using new and delete is that you get error handling
via exceptions.
Of course you will have to write at least one try-catch-block.
 
J

Jim Langston

Ron Natalie said:
Malloc is not declared here.


you don't check to see if malloc fails.

If I counted right above, You have exactly 16 A's there. Printf
%s expects a null-terminated string which you didn't provide.

You counted wrong. he has 15 A's there.
I'm not counting the B's but I bet you have the same problem.

27 B's That was the first thing I had looked at in the code. He's just not
assiging the pointer returned from realloc anywhere.
 
R

Ron Natalie

PSN said:
I was always assuming that realloc would update the pointer passed. My
mistake again. I should have read the documentation well.
There is no such thing as pass by reference in C (which malloc and
realloc come from).
 

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
474,183
Messages
2,570,966
Members
47,516
Latest member
ChrisHibbs

Latest Threads

Top