Runtime null pointer assignment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • apa
    New Member
    • Feb 2008
    • 2

    Runtime null pointer assignment

    What is the proper way of assigning null pointers in runtime. If I do the following:

    struct list *s=malloc(sizeo f(list));
    free(s);
    s=NULL;

    the program will work but Valgrind will give a complaint "Invalid write of size 4".

    Cheers,
    Antti
  • looker
    New Member
    • Dec 2007
    • 18

    #2
    Originally posted by apa
    What is the proper way of assigning null pointers in runtime. If I do the following:

    struct list *s=malloc(sizeo f(list));
    free(s);
    s=NULL;

    the program will work but Valgrind will give a complaint "Invalid write of size 4".

    Cheers,
    Antti
    malloc is used to allocating memory and return a (void *).
    You should return a pointer of type struct list from malloc.
    so
    [code=c]
    struct list *s = (struct list *)malloc(sizeof (struct list));
    [/code]

    Comment

    • apa
      New Member
      • Feb 2008
      • 2

      #3
      Originally posted by looker
      malloc is used to allocating memory and return a (void *).
      You should return a pointer of type struct list from malloc.
      so
      [code=c]
      struct list *s = (struct list *)malloc(sizeof (struct list));
      [/code]
      I think the compiler will do this pointer conversion implicitly. The question was not formed properly. This is more more accurate description of the problem

      [code=c]
      int llistAdd(LList list, char const *text){

      LListNode *new=(LListNode *)calloc(1,size of(LListNode));

      new->text=malloc((s trlen(text)+1)* sizeof(char));
      strcpy(new->text, text);

      73: if(llistCount(l ist)==0){
      74: list->first_node=new ;
      75: list->last_node=new; ;
      76: return 1;
      }
      ...
      [/code]

      gives a Valgrind error

      Invalid write of size 4
      ==6423== at 0x80488A1: llistAdd (llist.c:75)
      ==6423== by 0x80484DB: main (main.c:20)
      ==6423== Address 0x417F02C is 0 bytes after a block of size 4 alloc'd
      ==6423== at 0x4021AA4: calloc (vg_replace_mal loc.c:279)
      ==6423== by 0x8048795: llistConstruct (llist.c:20)
      ==6423== by 0x80484B9: main (main.c:17)
      no error on line 74 though. Similar problem occurs when assigning NULL to a freed variable.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by apa
        int llistAdd(LList list, char const *text){

        LListNode *new=(LListNode *)calloc(1,size of(LListNode));

        new->text=malloc((s trlen(text)+1)* sizeof(char));
        strcpy(new->text, text);

        73: if(llistCount(l ist)==0){
        74: list->first_node=new ;
        75: list->last_node=new; ;
        76: return 1;
        }
        How are you getting this to compile?

        list-> needs a pointer. Is LList a pointer?

        Comment

        • edwardrsmith
          New Member
          • Feb 2008
          • 62

          #5
          If the whole goal is to just assign NULL to a pointer then there is no need for malloc at this point. Just do

          Code:
          struct List temp*=NULL;
          This sets it to NULL. It may even be that you don't need to use malloc but it is impossible to tell without seeing what the pointer is used for later.

          Comment

          Working...