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

Discussion in 'C Programming' started by AMT2K5, Oct 10, 2005.

  1. AMT2K5

    AMT2K5 Guest

    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;
    }
     
    AMT2K5, Oct 10, 2005
    #1
    1. Advertising

  2. AMT2K5

    Skarmander Guest

    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.
     
    Skarmander, Oct 10, 2005
    #2
    1. Advertising

  3. 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
     
    Martin Ambuhl, Oct 10, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ad
    Replies:
    5
    Views:
    1,621
  2. Kavya
    Replies:
    3
    Views:
    434
    red floyd
    Oct 30, 2006
  3. Michal Slocinski

    Heap dump file size vs heap size

    Michal Slocinski, Mar 25, 2008, in forum: Java
    Replies:
    1
    Views:
    771
    GArlington
    Mar 25, 2008
  4. viki
    Replies:
    6
    Views:
    625
    Erik Wikström
    Jun 28, 2008
  5. Raymond Schanks
    Replies:
    0
    Views:
    592
    Raymond Schanks
    Apr 11, 2010
Loading...

Share This Page