Memory allocation with malloc

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PSN

    #1

    Memory allocation with malloc

    Hello everyone,

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

    realloc(pChar, 28);
    memcpy(pChar, "BBBBBBBBBBBBBB BBBBBBBBBBBBB", 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



  • PSN

    #2
    Re: Memory allocation with malloc

    Just to add a little more ...

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

    Thanks again.

    On Nov 16, 12:27 am, PSN <prakash...@gma il.comwrote:
    Hello everyone,
    >
    int main()
    {
    char *pChar;
    pChar = (char *)malloc(16);
    memcpy(pChar, "AAAAAAAAAAAAAA A", 16);
    printf ("***%s***\n ", pChar);
    >
    realloc(pChar, 28);
    memcpy(pChar, "BBBBBBBBBBBBBB BBBBBBBBBBBBB", 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

    Comment

    • peter koch

      #3
      Re: Memory allocation with malloc

      On 16 Nov., 00:27, PSN <prakash...@gma il.comwrote:
      Hello everyone,
      >
      int main()
      {
      char *pChar;
      pChar = (char *)malloc(16);
      memcpy(pChar, "AAAAAAAAAAAAAA A", 16);
      printf ("***%s***\n ", pChar);
      >
      realloc(pChar, 28);
      memcpy(pChar, "BBBBBBBBBBBBBB BBBBBBBBBBBBB", 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_alloc ated(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
      >
      Thank you for youri time
      >
      Prakash

      Comment

      • Jim Langston

        #4
        Re: Memory allocation with malloc


        "PSN" <prakash437@gma il.comwrote in message
        news:55029d6d-8a06-4d0d-a403-fb75aea4b67e@w3 4g2000hsg.googl egroups.com...
        Hello everyone,
        >
        int main()
        {
        char *pChar;
        pChar = (char *)malloc(16);
        memcpy(pChar, "AAAAAAAAAAAAAA A", 16);
        printf ("***%s***\n ", pChar);
        >
        realloc(pChar, 28);
        pChar = realloc(pChar, 28);
        memcpy(pChar, "BBBBBBBBBBBBBB BBBBBBBBBBBBB", 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.


        Comment

        • Ron Natalie

          #5
          Re: Memory allocation with malloc

          PSN wrote:
          Hello everyone,
          >
          int main()
          {
          char *pChar;
          pChar = (char *)malloc(16);
          Malloc is not declared here.
          memcpy(pChar, "AAAAAAAAAAAAAA A", 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, "BBBBBBBBBBBBBB BBBBBBBBBBBBB", 28);
          printf ("***%s***\n ", pChar);
          I'm not counting the B's but I bet you have the same problem.
          >
          free(pChar);
          return 0;

          Comment

          • Peter

            #6
            Re: Memory allocation with malloc

            On Nov 15, 3:27 pm, PSN <prakash...@gma il.comwrote:
            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.

            Comment

            • Jim Langston

              #7
              Re: Memory allocation with malloc

              "Ron Natalie" <ron@spamcop.ne twrote in message
              news:473cdb42$0 $802$9a6e19ea@n ews.newshosting .com...
              PSN wrote:
              >Hello everyone,
              >>
              >int main()
              >{
              >char *pChar;
              >pChar = (char *)malloc(16);
              >
              Malloc is not declared here.
              >
              >memcpy(pChar , "AAAAAAAAAAAAAA A", 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.
              You counted wrong. he has 15 A's there.
              >realloc(pCha r, 28);
              Ditto.
              >
              >memcpy(pChar , "BBBBBBBBBBBBBB BBBBBBBBBBBBB", 28);
              >printf ("***%s***\n ", pChar);
              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.
              >free(pChar);
              >return 0;

              Comment

              • Default User

                #8
                Re: Memory allocation with malloc - TPA

                PSN wrote:
                Just to add a little more ...

                Please don't top-post. Your replies belong following or interspersed
                with properly trimmed quotes. See the majority of other posts in the
                newsgroup, or the group FAQ list:
                <http://www.parashift.c om/c++-faq-lite/how-to-post.html>

                Comment

                Working...