Thread: help required with memcpy

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

    Help required with memcpy

    Hi - Been trying to make sense of memcpy and void pointers. I have a project that I am trying to complete which requires that I use memcpy to copy data of any type from a user of the program to a variable of type void. However I have not been able to get it to work with scalar variables of type int. But it does work with any other type of data including complex data structures. Any ideas?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <memory.h>
    
    int main(void)
    {
    	int *pInt;
    	void *pVoid;
    
    	pInt = (int *) malloc(sizeof(int));
    	if(pInt == NULL)
    	{
    		printf("Error Allocating memory!");
    		exit(1);
    	}
    
    	pVoid = (void *) malloc(sizeof(int));
    	if(pVoid == NULL)
    	{
    		printf("Error Allocating memory!");
    		exit(1);
    	}
    
    	printf("Enter a number: ");
    	scanf("%d", &pInt);
    	
    	memcpy(pVoid, pInt, sizeof(int));
    
    	return 0;
    }
    Last edited by foniks munkee; 03-07-2002 at 04:24 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    scanf("%d", &pInt);
    Since your pInt variable is already a pointer, and scanf takes a pointer argument, you should not be using the "address of" operator here. Take it out. Try this:

    Code:
    scanf( "%d", pInt );
    See if that solves your problem. I only took a quick glance at the code and nothing else popped out at me as being an issue except maybe use a void pointer type cast for the second arugment of the memcpy perhaps.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Since your pInt variable is already a pointer, and scanf takes a pointer argument, you should not be using the "address of" operator here. Take it out. Try this:
    Yup Your right it works.. Thanks! This wasn't a good example of the problem I am having then. D'oh. The following code is probably pretty familiar to a few by now..
    I call the following function in this manner:
    Code:
    insert(pMylist, pData, sizeof(pData));
    Now, if pData is a pointer of type struct it works. If it is a pointer of type int, it does not work. This function has to be generic!!! But from what I can tell calling memcpy like this:
    Code:
    // ..works in the following function if pData is struct * data type
    memcpy(pNewnode->data, pData, size);
    // ..works in the following function if pData is int * data type
    memcpy(&pNewnode->data, pData, size);
    Code:
    int insert(DLL *pRoot, void *pData, size_t size)
    {
    	NODE *pNewnode;
    
    	// Allocate memory for the next node in the list
    	if ((pNewnode = (NODE *) malloc(sizeof(NODE))) == NULL)
    	{
    		printf("Error: Unable to allocate memory for pNewnode");
    		fflush(stdout);
    		exit(EXIT_FAILURE);
    	}
    
    	// Allocate memory for the input from user
    	if ((pNewnode->data = malloc(size)) == NULL)
    	{
    		printf("Error: Unable to allocate memory for pNewnode->data");
    		fflush(stdout);
    		exit(EXIT_FAILURE);
    	}
    	
    	pNewnode->next = NULL;
    	pNewnode->prior = NULL;
    
    	// Copy the data to the new node: This bit currently broken
    	memcpy(pNewnode->data, pData, size);
    	
    	if (pRoot->head == NULL)
    	{
    		pRoot->head = pNewnode;
    		pRoot->tail = pNewnode;
    		pRoot->count = 1;
    	}
    	else
    	{
    		pNewnode->prior = pRoot->tail;
    		pRoot->tail->next = pNewnode;
    		pRoot->tail	= pNewnode;
    		pRoot->count += 1;				// Increase the count for the number of nodes
    	}
    	
    	return 1;
    }
    Last edited by foniks munkee; 03-07-2002 at 07:18 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Memcpy(); Errors...
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2006, 11:35 AM
  4. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  5. memcpy with 128 bit registers
    By grady in forum Linux Programming
    Replies: 2
    Last Post: 01-19-2004, 06:25 AM