-
pointers
Im writing a program which is being implemented as a singly linked list. I am having trouble with one of the functions.
The funtion will malloc space for a new block and for the key and data areas pointed to from it and return a pointer to new block. It will return null if malloc fails.
Will this work? :)
static Block *newBlock( int keylength, int vallength )
{
Block *nblock;
Block *v;
Block *k;
nblock = (Block) malloc(sizeof(Block));
v = (Block) malloc(sizeof(vallength));
k = (Block) malloc(sizeof(keylength));
return nblock;
}
All the code related to the function
[code]
#include <stdio.h>
#include <string.h>
/* prototypes, return codes, and typedef for Mapping */
#include "mapss.h"
#define DEBUG 0
typedef struct list_blk {
char *key;
char *data;
struct list_blk *next;
} Block;
typedef struct {
int length;
Block *head;
Block *tail;
} List_header;
/* what a Mappings turn out to be in the implementation-- */
typedef List_header *MapList;
/* prototypes and macros for local functions */
static Block *find( MapList L, const char* key );
static Block *newblock( int keylength, int valuelength);
static void freeblock( Block *bptr );
/* A useful macro: see if ptr is "positioned" at the logical end-of-list,
* the position just after the last real key-val pair on the list.
*/
#define PTR_AT_END(L, ptr) ( ((ptr)->next == ((MapList)L)->tail ))
Mapping makeMapping()
{
MapList L;
L = (MapList)malloc(sizeof(List_header));
if (L == NULL) return NULL; /* malloc failed! */
L->length = 0;
L->head = newblock(0, 0); /* key and data fields never used */
L->tail = newblock(0, 0);
if (L->head && L->tail) {/* malloc succeeded in both calls to newblock */
L->head->next = L->tail;
return (void *)L;
}
else return 0;
}
/* optional, but think about it */
void unmakeMapping(Mapping M)
{
printf("unmakeMapping nyi\n"); /* nyi = not yet implemented */
}
int install( Mapping M, const char *key, const char *data )
{
MapList L = (MapList)M; /* convert app's "Mapping" to impl's list */
Block *bptr = find( L, key); /* try to find key */
printf("install stub\n");
return INSTALL_FAILED;
}
int lookup( Mapping M, const char *key, char *data ) {
MapList L = (MapList)M; /* convert app's "Mapping" to impl's list */
printf("lookup stub\n");
return NOT_FOUND;
}
int delete( Mapping M, const char* key) {
MapList L = (MapList)M; /* convert app's "Mapping" to impl's list */
printf("delete stub\n");
return NOT_FOUND;
}
/* Return the count of Mapping L, = length of underlying list */
int count( Mapping L ) {
printf("count stub\n");
return 0;
}
/********************* local functions *********************/
/* Look for key on L, returning pointer to the block
* -before- the block containing the key. Returns the block
* before the tail if key not found.
*
*/
static Block *find( MapList L, const char* key )
{
printf("find stub\n");
return 0;
}
/* malloc space for a new Block and for the key and data
* areas pointed to from it, return pointer to new Block.
* Return NULL if malloc fails.
*/
static Block *newblock( int keylength, int vallength)
{
printf("newblock stub\n");
return 0;
}
/* Free the space pointed to by bptr - after freeing
* the space reserved for the key and the data areas.
*/
static void freeblock( Block *bptr )
{
printf("freeblock stub\n");
return;
}
[\code]
-
Post the struct 'Block' so I can see how you implement it. Better yet, just post all of the code using the [ c o d e ] and [ / c o d e ] but without all the spaces.
--Garfield
-
If you have pointers within the Block struct that need to be pointed to the memory you've allocated, then you'll have to do it before returning from the function, otherwise the pointers won't point to anything and the memory you've allocated will be lost.
If these are just variables that are members of your struct then you don't have to allocate memory for them and can just assign to them (the memory they need is part of the memory allocated for the Block struct).
-
static Block *newBlock( int keylength, int vallength )
{
Block *nblock;
nblock = (Block *) malloc(sizeof(Block));
return nblock;
}
since you have defined nblock, v and k as Block*, you have to use the same prototype when using malloc.
v = (Block) malloc(sizeof(vallength));
k = (Block) malloc(sizeof(keylength));
You have defined v as block*, however (1) while using malloc you are using prototype as block * and (2) sizeof() is given as size of int. This will never work.
I haven't gone thrugh your entire code but if you wish to access your key and data in your struct then you can use nblock->key and nblock->data to access those members. here in your code v and k are useless as you are not returning them to main.
cheers,