473,322 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Runtime null pointer assignment

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

struct list *s=malloc(sizeof(list));
free(s);
s=NULL;

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

Cheers,
Antti
Feb 25 '08 #1
4 2441
looker
18
What is the proper way of assigning null pointers in runtime. If I do the following:

struct list *s=malloc(sizeof(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
Expand|Select|Wrap|Line Numbers
  1. struct list *s = (struct list *)malloc(sizeof(struct list));
  2.  
Feb 26 '08 #2
apa
2
malloc is used to allocating memory and return a (void *).
You should return a pointer of type struct list from malloc.
so
Expand|Select|Wrap|Line Numbers
  1. struct list *s = (struct list *)malloc(sizeof(struct list));
  2.  
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

Expand|Select|Wrap|Line Numbers
  1. int llistAdd(LList list, char const *text){
  2.  
  3.   LListNode *new=(LListNode *)calloc(1,sizeof(LListNode));
  4.  
  5.   new->text=malloc((strlen(text)+1)*sizeof(char));
  6.    strcpy(new->text, text);
  7.  
  8. 73:   if(llistCount(list)==0){
  9. 74:   list->first_node=new;
  10. 75:    list->last_node=new;;
  11. 76:    return 1;
  12.   }
  13. ...
  14.  
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_malloc.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.
Feb 26 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
int llistAdd(LList list, char const *text){

LListNode *new=(LListNode *)calloc(1,sizeof(LListNode));

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

73: if(llistCount(list)==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?
Feb 26 '08 #4
If the whole goal is to just assign NULL to a pointer then there is no need for malloc at this point. Just do

Expand|Select|Wrap|Line Numbers
  1. 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.
Feb 26 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: gary | last post by:
Hi, We all know the below codes are dangerous: { int *p = new int; delete p; delete p; } And we also know the compilers do not delete p if p==NULL. So why compilers do not "p = NULL"...
31
by: vp | last post by:
If I have a pointer char * p, is it correct to assign NULL to this pointer by: "memset( &p, 0, sizeof(p));" instead of "p = NULL;" The reason I ask is I have an array of structure of N...
102
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV"...
41
by: Alexei A. Frounze | last post by:
Seems like, to make sure that a pointer doesn't point to an object/function, NULL (or simply 0) is good enough for both kind of pointers, data pointers and function pointers as per 6.3.2.3: 3 An...
3
by: Nick Kiguta | last post by:
I have two classes, a Node class and a Bstree class. The Bstree class contains a pointer to a Node class as one of its private members. Now I need to build a binary search tree from multiple...
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
27
by: rocco.rossi | last post by:
I've been trying to write a function capable of checking if a pointer value is set to NULL, and if it isn't, of deallocating and setting it's value to NULL regardless of the pointer's type. I...
51
by: muktipada | last post by:
Hello, As a C++ developer which one we should use for pointer assignment, NULL or 0. typedef DummyC DummyClass*; // in some header file. DummyC obj = NULL; if (obj == NULL) {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.