pointers

This is a discussion on pointers within the C Programming forums, part of the General Programming Boards category; Im writing a program which is being implemented as a singly linked list. I am having trouble with one of ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    6

    Cool 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]
    Last edited by fanaonc; 11-16-2001 at 07:21 PM.

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    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
    1978 Silver Anniversary Corvette

  3. #3
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    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).
    zen

  4. #4
    Registered User goran's Avatar
    Join Date
    Sep 2001
    Posts
    68
    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,
    Last edited by goran; 11-17-2001 at 01:21 AM.
    I don't wait for the future; it comes soon enough.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21