Understang this malloc() related code

This is a discussion on Understang this malloc() related code within the C Programming forums, part of the General Programming Boards category; Please indicate to me if I have the general idea of this memory allocating code correct. Thanks Code: char * ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    Understang this malloc() related code

    Please indicate to me if I have the general idea of this memory allocating code correct. Thanks

    Code:
    char * hPtr[10];   // Defines an array of 10 character Pointers
    for(int ctr = 0; ctr < 10; ctr++)
    { // Allocates 15 bytes for each hPtr element.
    hPtr[ctr] = (char *)malloc(15 sizeof(char)); }
    If a user enters more than 15 ( or is it 14) bytes worth what could happen?

  2. #2
    Unregistered
    Guest
    Code:
    char * hPtr[10];   // Defines an array of 10 character Pointers
    for(int ctr = 0; ctr < 10; ctr++)
    { // Allocates 15 bytes for each hPtr element.
    hPtr[ctr] = (char *)malloc(15 sizeof(char)); }
    should be
    Code:
    char * hPtr[10];
    for(int ctr = 0; ctr < 10; ctr++){
        /*Allocates space for 15 characters to each pointer*/
        hPtr[ctr] = malloc(15 * sizeof(char));
    }
    -Prelude

  3. #3
    Unregistered
    Guest
    As for your second question, if the user enters more characters than allowed for the 15 will be entered and the rest thrown away. If the user enters less than allocated, the array will still have some room left but nothing dire will happen when you access it.

    -Prelude

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    char * hPtr[10];
    hPtr[ctr] = malloc(15 * sizeof(char));
    Maybe this is a complier problem, but I've just tried this with Borland's C++ 4.5 & TClite and I get an error stating that it couldn't convert a void pointer to a charater pointer.

    Other than that thanks for helping me to understand it.
    Last edited by lambs4; 11-30-2001 at 02:37 PM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    > stating that it couldn't convert a void pointer to a charater pointer
    That's because you're compiling C code with a C++ compiler.

    Make sure your source code is .c, not .cpp or whatever your compiler treats c++ source files as.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    hPtr[ctr] = (char *)malloc(15 sizeof(char));
    hPtr[ctr] = malloc(15 sizeof(char));
    Are these statements equivalent? Or is the first statement not part of the C syntax and possible C++?
    Last edited by lambs4; 11-30-2001 at 04:08 PM.

  7. #7
    Unregistered
    Guest
    hPtr[ctr] = (char *)malloc(15 sizeof(char));
    hPtr[ctr] = malloc(15 sizeof(char));

    Both are equivalent in the fact that they're wrong. The error is in the actual call to malloc
    malloc(15 sizeof(char));
    15 sizeof(char) will not allocate space for 15 characters, you have to multiply the sizeof char by 15 to do that, hence
    malloc(15 * sizeof(char));

    The other difference is the first one uses a cast and the second doesn't. With the current standard, a cast is not needed anymore though in the past it was required. So on that the choice is up to you, they're both the same.

    -Prelude

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  3. Replies: 1
    Last Post: 03-21-2006, 06:52 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 12:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21