Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Memory allocation to void * pointer 2

Status
Not open for further replies.

jdubowy

Programmer
Mar 1, 2001
13
US
I have a structure (which I can not modify) which has as as it's first element a pointer of type 'void *' . This structure is designed for general linked list use, and I need the 'void *' pointer to point to another structure type. I am having trouble somewhere, either allocating memory to this pointer or casting this pointer correctly.
what I've done is as follows:

typedef struct linkedListNode
{
void * pData; /* this allows any user-defined data */
struct ListNodeTag * pLink;
} ListNodeType;

/*.........*/

ListNodeType * pcurrentnode;
AnyStructType * pstruct;

pcurrentnode->pData =
(AnySructType *)malloc(sizeof(AnySructType)*1);

*pcurrentnode->pData = *pstruct;

This ' pcurrentnode->pData' is not correctly pointing to the data that I'm trying to assign to it from 'pstruct'.
If anybody sees what is wrong, I'd very much appreciate finding out.

Thanks
 
>pcurrentnode->pData =
(AnySructType *)malloc(sizeof(AnySructType)*1);

This line isn't causing the problems, but you might want to change it:

pcurrentnode->pData=malloc(sizeof(AnySructType));

The cast is unnecessary - you can assign to and from void pointers without a cast or any loss of information.

Multiplying times one is also superfluous.

>*pcurrentnode->pData = *pstruct;

Two problems here. You need to allocate memory for and stuff something in pstruct (perhaps you omitted this part of the code for brevity) and you need to drop the dereference operators:

pstruct=malloc(sizeof *pstruct);

/* copy data in appropriately */

pcurrentnode->pData=pstruct;

Now your linked list node data points to the allocated memory pointed to by pstruct. The way you had it set up you were just assigning the contents pointed to by the pointer, rather than assigning the pointer itself.

HTH,

Russ
bobbitts@hotmail.com
 
DO NOT DO LIKE rbobbitt said!!!

in this line:
pcurrentnode->pData=pstruct;
you will copy just a pointer to pstruct.
After that you will have memory leak from this allocation:
pcurrentnode->pData=malloc(sizeof(AnySructType));
and when user will freed pstruct you will lose data in your list too.
Not good enough.

If you want to copy user data (not a pointer) you can call memcpy:
memcpy(pcurrentnode->pData,(void*)pstruct,sizeof(AnySructType));

or try this:
*((AnySructType*)pcurrentnode->pData)=*pstruct;
But htis can be platform dependent behavior, it works on UNIX-IRIX, but I don't know about Nt.
I suggest first example, especially when you don't know exact type of structure AnyStructType and have only this structure size (sizeof(AnyStructSize));

Regards.


 
Right, my mistake, you should both allocate memory for pcurrentnode->pData AND copy the memory (not just assign the pointer) from pstruct. So it should look something like this:

/* Allocate memory for pstruct */
pstruct=malloc(sizeof *pstruct);

/* allocate memory for node data */
pcurrentnode->pData=malloc(sizeof *pstruct);

/* copy data in appropriately to pstruct*/

/* copy data from pstruct to node data */
memcpy(pcurrentnode->pData,pstruct,sizeof *pstruct);

Russ
bobbitts@hotmail.com
 
Hey,
Did u allocate memory for "pcurrentnode" ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top