Thread: calloc()

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    2

    calloc()

    HI guys,
    In the following code, I am trying to allocate some memory for a character array of size 30. PLease let me know what I am doing wrong. Because here, even after freeing the memory I still see the first element there same as before freeing the memory.

    #include <stdio.h>
    #include <stdlib.h>
    char String[30];
    char *pString = String;

    //name of the array is synomus to the first element address?
    int main(){
    // printf(" %c\n",pString[0]);
    pString = (char *)calloc(30, sizeof(char));
    String[0] = 'a';

    printf(" %c\n",String[0]);


    free(pString);
    printf(" %c\n",String[0]);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Let's go throug your post, shall we?

    1) You didn't use code tags, which means you didn't read the forum announcements. That's bad.


    Quote Originally Posted by goresus
    HI guys,
    In the following code, I am trying to allocate some memory for a character array of size 30.
    2) Ok, so you just want 30 characters allocated. That's easy enough.

    Quote Originally Posted by goresus
    PLease let me know what I am doing wrong. Because here, even after freeing the memory I still see the first element there same as before freeing the memory.
    3) That's very wrong, if you are in fact doing what you say you're doing. Never, ever try to access memory you've just freed.

    4) /* after adding code tags... */
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    char String[30];
    There. You've just allocated 30 characters. Nothing more need be done. This is not dynamic memory allocation here. This is simply creating an array. You do not free this. Ever.

    5) Here you make a pointer point at the array you have. You still don't ever call free on this the way you have it:
    Code:
    char *pString = String;
    6) Here you go and change what pString points to. If you uncomment that line, you'll have another problem, which is that you haven't put anything in the array before you try to start printing bits and pieces of it. That's wrong to do.
    Code:
    //name of the array is synomus to the first element address?
    int main(){
      // printf(" %c\n",pString[0]);
      pString = (char *)calloc(30, sizeof(char));
    pString no longer points at your array. It's pointing at memory you've just allocated with calloc. Now it's safe to use free on the pointer pString.

    7) Here you stick the letter 'a' in your array, not in the memory you've just allocated.
    Code:
      String[0] = 'a';
    
      printf(" %c\n",String[0]);
     
    
      free(pString);
      printf(" %c\n",String[0]);
    }
    You don't ever put anything in the memor you've allocated. You simply allocate some, then free it, ignoring it entirely while you play games with your array.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    2
    >>You don't ever put anything in the memor you've allocated. You simply allocate some, then free it, ignoring it entirely while you play games with your array.<<

    HI there,
    Thank you for the valuable information. Your last comment, tough, I still can't get. Once we allocate the memory, how do we use it? If we dont put any thing in this memory, is it just pointers that we are creating? What is the purpose of setting aside this memory? And how to check whether the memory is free or not once we do it?

    Regards,
    Goresus

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well without any arrays to mess up understanding,
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(){
        char *pString = calloc(30, sizeof(char));  /* no cast needed in C */
        if ( pString != NULL ) { /* always check malloc returns for NULL */
            pString[0] = 'a';    /* use it much like you would an array */
            printf(" %c\n",pString[0]);
            free(pString);       /* all done */
            printf(" %c\n",pString[0]); /* NEVER DO THIS - refer to memory after free */
        }
        return 0;
    }
    Whilst pointer share some common syntax - eg, array subscripts, there are also some very important differences as well.
    http://www.eskimo.com/~scs/C-faq/s6.html
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc, calloc from the FAQ
    By salvadoravi in forum C Programming
    Replies: 10
    Last Post: 01-21-2008, 03:29 AM
  2. Help with calloc and pointers to strings.
    By sweetarg in forum C Programming
    Replies: 1
    Last Post: 10-24-2005, 02:28 PM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. Why use calloc()?
    By dwks in forum C Programming
    Replies: 8
    Last Post: 07-20-2005, 08:22 AM
  5. difference between calloc and malloc
    By saravanan_ts in forum C Programming
    Replies: 4
    Last Post: 07-28-2003, 06:13 AM