Jump to content

Super Stuck, and Totally Confused

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
HapHazard

HapHazard

    Newbie

  • Members
  • PipPip
  • 10 posts
So I am stuck, The problem I have been working on is implementing a linked list on a hard disk, using system calls. Most things seem to work. I am working right now on the head node, but the problem seems to be that when I create the first node, I am unable of reading it, because of a segmentation fault. I have walked line by line through the code. And it seems that when I try to get the head length it gives me -250 instead of 6. And then everything craps out on me. I have even tried hard coding the 6 in, and it still gave me problems. I am hoping that it is a simple problem that I am being stupid about and don't notice. So heres the code.

lab1.cpp

#include <stdlib.h>

#include <iostream>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <string.h>

#include <unistd.h>


#include "Node.h"

#include "HeadNode.h"



int main( int argc, char * argv[] )

{


  //Open File Descriptor

  int fd = open("test.bin", O_RDWR|O_CREAT ,S_IRWXU);


  //Seek to the beginning and then create a HeadNode

  off_t seek = lseek(fd, 0, 0);

  HeadNode* head = new HeadNode(fd, 127, 16);


  //This is for testing purposes to see the value of HeadLength

  std::cout << "Head Length: " << head->getHeadLength(fd) << std::endl;



  //Seek back to the beginning and move past the HeadNode

  seek = lseek(fd, 0, 0);

  seek = lseek(fd, head->getHeadLength(fd), 0);


  //Create a Node right after the head

  //---I believe the problem is in the Node constructor---

  Node* test = new Node(fd, (char*) "Test this out!\0");


  //Test the value of HeadLength

  std::cout << "Head Length: " << head->getHeadLength(fd) << std::endl;


  //Seek back to the beginning then seek past the head

  seek = lseek(fd, 0, 0);

  seek = lseek(fd, head->getHeadLength(fd), 0);

  //Read and print out the string in the node

  std::cout << "Test:" << (char*)test->getData(fd) << std::endl;


  close(fd);



  return 0;

}


HeadNode.cpp

#include <cmath>

#include "HeadNode.h"

#include <sys/types.h>

#include <unistd.h>


HeadNode::HeadNode(int fd, int charLength, int maxNodes) {

    off_t tempPos = lseek(fd, 0, SEEK_CUR);

    off_t seek = lseek(fd, 0, SEEK_CUR);

    ssize_t charSize = write( fd, (void *)&maxNodes, ATTR_SIZE);

    int nodes = 0;

    charSize = write( fd, (void *)&nodes, ATTR_SIZE);

    int indexLength = (int)ceil((log2((double)maxNodes)/8.0));

    charSize = write( fd, (void *)&indexLength, ATTR_SIZE);

    int firstNode = -1;

    charSize = write( fd, (void *)&firstNode, ATTR_SIZE);

    int headNodeLength = ATTR_SIZE * 6;

    charSize = write( fd, (void *)&headNodeLength, ATTR_SIZE);

    charSize = write( fd, (void *)&charLength, ATTR_SIZE);

    seek = lseek( fd, tempPos, SEEK_SET);


}

HeadNode::HeadNode(){


}


HeadNode::HeadNode(const HeadNode& orig) {

}


HeadNode::~HeadNode() {

}

int HeadNode::getCharLength(int fd){

    off_t tempPos = lseek(fd, 0, SEEK_CUR);

    int charLength;

    off_t seek = lseek(fd, 5 * ATTR_SIZE, SEEK_SET);

    ssize_t charSize = read( fd, (void *)&charLength, ATTR_SIZE);

    seek = lseek( fd, tempPos, SEEK_SET);

    return charLength;

}

int HeadNode::getIndexLength(int fd){

    off_t tempPos = lseek(fd, 0, SEEK_CUR);

    int indexLength = 0;

    off_t seek = lseek(fd, 2 * ATTR_SIZE, SEEK_SET);

    ssize_t charSize = read( fd, (void *)&indexLength, ATTR_SIZE);

    seek = lseek( fd, tempPos, SEEK_SET);

    return indexLength;

}

int HeadNode::getFirstNode(int fd){

    off_t tempPos = lseek(fd, 0, SEEK_CUR);

    int firstNode;

    off_t seek = lseek(fd, 3 * ATTR_SIZE, SEEK_SET);

    ssize_t charSize = read( fd, (void *)&firstNode, ATTR_SIZE);

    seek = lseek( fd, tempPos, SEEK_SET);

    return firstNode;

}

int HeadNode::getMaxNodes(int fd){

    off_t tempPos = lseek(fd, 0, SEEK_CUR);

    int maxNodes;

    off_t seek = lseek(fd, 0 * ATTR_SIZE, SEEK_SET);

    ssize_t charSize = read( fd, (void *)&maxNodes, ATTR_SIZE);

    seek = lseek( fd, tempPos, SEEK_SET);

    return maxNodes;

}

int HeadNode::getNodes(int fd){

    off_t tempPos = lseek(fd, 0, SEEK_CUR);

    int nodes;

    off_t seek = lseek(fd, 1 * ATTR_SIZE, SEEK_SET);

    ssize_t charSize = read( fd, (void *)&nodes, ATTR_SIZE);

    seek = lseek( fd, tempPos, SEEK_SET);

    return nodes;


}

void HeadNode::setFirstNode(int fd, int node){

    off_t tempPos = lseek(fd, 0, SEEK_CUR);

    off_t seek = lseek(fd, 3 * ATTR_SIZE, SEEK_SET);

    ssize_t charSize = write( fd, (void *)&node, ATTR_SIZE);

    seek = lseek( fd, tempPos, SEEK_SET);

}

void HeadNode::incrementNodes(int fd){

    off_t tempPos = lseek(fd, 0, SEEK_CUR);

    int nodes;

    off_t seek = lseek(fd, 1 * ATTR_SIZE, SEEK_SET);

    ssize_t charSize = read( fd, (void *)&nodes, ATTR_SIZE);

    nodes++;

    seek = lseek(fd, -ATTR_SIZE, SEEK_SET);

    charSize = write( fd, (void *)&nodes, ATTR_SIZE);

    seek = lseek( fd, tempPos, SEEK_SET);


}

int HeadNode::getHeadLength(int fd){

    //Grab seek location

    off_t tempPos = lseek(fd, 0, SEEK_CUR);

    int headLength;

    //Seek to the 4th byte

    off_t seek = lseek(fd, 4 * ATTR_SIZE, SEEK_SET);

    //Read the headLength

    ssize_t charSize = read( fd, (void *)&headLength, ATTR_SIZE);

    //Set seek back to original location

    seek = lseek( fd, tempPos, SEEK_SET);

    //Return the headLength

    return headLength;

}

void HeadNode::decrementNodes(int fd){

    off_t tempPos = lseek(fd, 0, SEEK_CUR);

    int nodes;

    off_t seek = lseek(fd, 1 * ATTR_SIZE, SEEK_SET);

    ssize_t charSize = read( fd, (void *)&nodes, ATTR_SIZE);

    nodes--;

    seek = lseek(fd, -ATTR_SIZE, SEEK_SET);

    charSize = write( fd, (void *)&nodes, ATTR_SIZE);

    seek = lseek( fd, tempPos, SEEK_SET);


}


Node.cpp

#include <sys/types.h>

#include <unistd.h>

#include "Node.h"

#include "HeadNode.h"

#include <string.h>


Node::Node(int fd, char* str) {

    //Grab the seek location

    off_t tempPos = lseek(fd, 0, SEEK_CUR);

    //Set the index of the next node to unallocated -1

    int next = -1;

    //write the next index and the char*

    ssize_t charSize = write( fd, (void *)&next, HeadNode::getIndexLength(fd));

    charSize = write( fd, (void *)&str, strlen(str));

    //seek back to the original location

    off_t seek = lseek( fd, tempPos, SEEK_SET);

    


}


Node::Node(const Node& orig) {

}


Node::~Node() {

}

char* Node::getData(int fd){

    off_t tempPos = lseek(fd, 0, SEEK_CUR);

    off_t seek = lseek(fd, HeadNode::getIndexLength(fd), SEEK_SET);

    char* charStr;

    ssize_t charSize = read( fd, (void *)&charStr, HeadNode::getCharLength(fd));

    seek = lseek( fd, tempPos, SEEK_SET);

    return charStr;

}

int Node::getNext(int fd){

    off_t tempPos = lseek(fd, 0, SEEK_CUR);

    int next;

    ssize_t charSize = read( fd, (void *)&next, HeadNode::getIndexLength(fd));

    off_t seek = lseek( fd, tempPos, SEEK_SET);

    return next;


}

void Node::setNext(int fd, int index){

    off_t tempPos = lseek(fd, 0, SEEK_CUR);

    ssize_t charSize = write( fd, (void *)&index, HeadNode::getIndexLength(fd));

    ssize_t seek = lseek( fd, tempPos, SEEK_SET);

}



HeadNode.h

#ifndef _HEADNODE_H

#define	_HEADNODE_H

#include "Node.h"


class HeadNode {

public:

    HeadNode(int fd, int size, int maxNodes);

    HeadNode();

    HeadNode(const HeadNode& orig);

    virtual ~HeadNode();

    int getMaxNodes(int fd);

    int getNodes(int fd);

    int getFirstNode(int fd);

    void setFirstNode(int fd, int first);

    static int getIndexLength(int fd);

    static int getCharLength(int fd);

    int getFirstOpen(int fd);

    void incrementNodes(int fd);

    void decrementNodes(int fd);

    int getHeadLength(int fd);

private:

    const static int ATTR_SIZE = 1;


};


#endif	/* _HEADNODE_H */



Node.h

#ifndef _NODE_H

#define	_NODE_H


class Node {

public:

    Node(int fd, char* str);

    Node(const Node& orig);

    virtual ~Node();

    char* getData(int fd);

    int getNext(int fd);

    void setNext(int fd, int index);

private:

    //int next;

    //char* data;

};


#endif	/* _NODE_H */




Output:

Head Length: 6
Head Length: -250
Segmentation fault


Thank you for your help in advance, and I can add more information if necessary.
-HapHazard

#2
HapHazard

HapHazard

    Newbie

  • Members
  • PipPip
  • 10 posts
I figured out a solution and if I have time later I will post it if anyone is interested.
-HapHazard