Thread: Generic function for initialisation

  1. #1
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343

    Question Generic function for initialisation

    Hello, me again!

    I am trying to write a function that I will be able to use to initialise the values of any type of data. Any opinions on this function.

    Cheers.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <memory.h>
    
    typedef struct Node NODE;
    
    struct Node
    {
    	int  *pData;
    	NODE *pNext;
    	NODE *pPrev;
    };
    
    void init_list (void *, size_t);
    
    int main(void)
    {
    	NODE *pMylist;
    
    	if ((pMylist = (NODE *) malloc(sizeof(NODE))) == NULL)
    	{
    		printf("ERROR: Unable to allocate memory\n");
    		exit(1);
    	}
    
    	init_list(pMylist, sizeof(NODE));
    
    	free(pMylist);
    
    	return 0;
    }
    
    void init_list (void *pData, size_t size)
    {
    	memset(pData, NULL, size);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Can't be done (easily)
    http://www.eskimo.com/~scs/C-faq/q7.31.html

    Setting all bits zero, which is what memset(pData, 0, size); does, doesn't guarantee that NULL pointers are really NULL pointers, or that floats are really 0.0

  3. #3
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Ah... OK - so it is probably best to initialise each member individualy to be sure.

    Thanks again Salem!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM