Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Simple malloc problem with Visual C++ 2005 {Heap}

Reply
Thread Tools

Simple malloc problem with Visual C++ 2005 {Heap}

 
 
AMT2K5
Guest
Posts: n/a
 
      10-10-2005
Hello, I am refreshing my memory on C as I've been in a C++
programming environment for a few months.

Why does the following code produce a heap protection error in Visual
C++ 2005?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]){

char
*newString = (char *)malloc(sizeof(char));
strcpy(newString, "2005");
printf("%s\n", newString);
free(newString);

return 0;
}

 
Reply With Quote
 
 
 
 
Skarmander
Guest
Posts: n/a
 
      10-10-2005
AMT2K5 wrote:
> Hello, I am refreshing my memory on C as I've been in a C++
> programming environment for a few months.
>
> Why does the following code produce a heap protection error in Visual
> C++ 2005?
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> int main(int argc, char *argv[]){
>
> char
> *newString = (char *)malloc(sizeof(char));


First, lose the cast; this only obscures possible errors. C++ wants
this, C doesn't, and it's generally agreed it shouldn't be done.

Second, you've allocated memory for exactly one character...

> strcpy(newString, "2005");


....and then copy 4 characters plus a terminating NUL to this area.

From C++ you're probably used to the idea that you can use 'string' and
get dynamic (re)allocation for free. In C you'll have to do it all
yourself, I'm afraid.

S.
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      10-10-2005
AMT2K5 wrote:
> Hello, I am refreshing my memory on C as I've been in a C++
> programming environment for a few months.
>
> Why does the following code produce a heap protection error in Visual
> C++ 2005?


Your compiler is not relevant. Your code is broken:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
#if 0
char
*newString = (char *)malloc(sizeof(char));
/* There should be no cast above; since sizeof(char) == 1 by definition,
that makes a lot of the above line just typing practice, better spent
checking the return value from malloc. */
strcpy(newString, "2005");
printf("%s\n", newString);
#endif
printf("sizeof(char) = %lu (ALWAYS),\n"
"sizeof \"2005\" = %lu\n",
(long unsigned) sizeof(char), (long unsigned) sizeof "2005");
#if 0
free(newString);
#endif
return 0;
}

sizeof(char) = 1 (ALWAYS),
sizeof "2005" = 5
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
to malloc or not to malloc?? Johs32 C Programming 4 03-30-2006 10:03 AM
porting non-malloc code to malloc micromysore@gmail.com C Programming 3 02-19-2005 05:39 AM
Malloc/Free - freeing memory allocated by malloc Peter C Programming 34 10-22-2004 10:23 AM
free'ing malloc'd structure with malloc'd members John C Programming 13 08-02-2004 11:45 AM
Re: free'ing malloc'd structure with malloc'd members ravi C Programming 0 07-30-2004 12:42 PM



Advertisments