Sponsored Content
Full Discussion: Create a database in C
Top Forums Programming Create a database in C Post 302560669 by Corona688 on Friday 30th of September 2011 01:43:21 PM
Old 09-30-2011
I explained absolutely everything in my code comments and suggest you read them.

Quote:
while(l->next != NULL)

I feel like I'm missing something. Like cause I can't see the reason for looping
through l->next.
If you delete the current node, the previous node is left pointing to invalid memory.

Code:
Address 1:  state=1, next=2
Address 2: state=0, next=NULL

when you call free() on memory block 2, what happens to memory block 1? Absolutely nothing. It still points to memory block 2. It doesn't know it's the last node in the list! Next time you use the list it'll try to access block 2 anyway, and either crash or do strange things.

Whenever you're not at the first node in the list, you need to check and free the next node, not the current one, otherwise you'll leave dangling pointers all over the place.
Quote:
I'm just wondering if you did that second part just in case I want l to be iterated
through but I gotta make sure.
I explained exactly why I did absolutely everything in the comments I put in my code, I suggest you read them. If you don't understand my comments, ask questions about them, please don't just ignore them.
Quote:
My problem is the fact that I want descr_list to have struct descr *p as
a part of itself and I thought initializing a null part of d would do that but
now I am thinking that it wont.
If I get your meaning, it won't.

A pointer is a memory address and nothing more. Trying to initialize a NULL means trying to write to address 0 in memory, which crashes.

Since you never actually posted your data structures, I certainly can't show you how to fix them.

Quote:
Ok so heres a huntch I have at making the structure descr *d part of
descr_list. Maybe I just change the value adescr_list untill it is pointing to
a NULL structure and then make it equal to d.
Changing the pointers doesn't change the memory. Please read the examples I posted for you about array indexes.

Code:
// descr **p is pointless, returning a pointer is much less likely
// to be screwed up.
int initialize_descr(int newfd)
{
 struct descr *d=descr_list;

 // why are there absolutely no comments in your code?
 if( d != NULL)
 {
  while(d->next != NULL)
   d = d->next;

  if(!(d->next = malloc(sizeof(struct descr))))
  {
   nonfatal("malloc failure initializing descr");
   return 0;
  }

  d = d->next;
 }
 // We only allocate this memory when the list is empty.
 // But you forgot to put it into descr_list.
 // So the list will always stay empty!
 else if(!(d = malloc(sizeof(struct descr))))
 {
  nonfatal("malloc failure initializing descr");
  return 0;
 }
 d->newfd = newfd;
 d->state = 1;
 if(!(d->q = malloc(sizeof(struct descr))))
 {
  nonfatal("malloc failure initializing descr queue");
  d->state = 0;
  return 0;
 }
 d->q->next = NULL;
 d->next = NULL;
*p = d;
}

How about:
Code:
struct descr *initialize_descr(int newfd, struct descr **p)
{
 struct descr *d=descr_list;

 if(d == NULL)
 {
    // The base of the list itself must be updated.
    descr_list=malloc(sizeof(struct descr));
    // then set our own pointer to it too.
    d=descr_list;
    // if malloc failed, return error.
    if(d == NULL) return(NULL);
 }
 else
 {
    // find the end of the list.
    while(d->next != NULL) d=d->next;

    d->next=malloc(sizeof(struct descr));
    // if malloc failed, return error
    if(d->next == NULL) return(NULL);
    // set d to the memory we just allocated.
    d=d->next;
 }

 d->newfd = newfd;
 d->state = 1;

 if(!(d->q = malloc(sizeof(struct descr))))
 {
  nonfatal("malloc failure initializing descr queue");
  d->state = 0;
  return 0;
 }
 d->q->next = NULL;
 d->next = NULL;
 return(d);
}

Quote:
What do I have to add to this function to make a list of player structures out of the global variable descr_list?
something like while descr_list->next != NULL descr_list= descr_list->next descr_list ->next = p.
There's only two times you'd change the value of descr_list.

1) When the list is empty. descr_list must be set to the new first node.
2) When the first node is being deleted. descr_list must be set to the next node.

Code:
struct descr
{
 short int newfd;
 short int state; /* Maximum states allowed, 65536 */
 struct queue *q;
 struct descr *next;
struct descr *self;
};

If you need to add a 'self' pointer to keep your code together, you must be doing something very strange indeed.



---------- Post updated at 06:56 AM ---------- Previous update was at 06:20 AM ----------

So what do you think?
Will she work?
I think I have finally done it.[/quote] For yog's sake. If you've posted 5 times while I'm asleep, posting a 6th won't make me wake up.

I can't tell if your code works or not since you changed the structures again and didn't post what it now is, but it looks like you've added 3 or 4 useless pointers which are only helpful for working around bugs in your code.
Code:
int readjust_descr()
{
 struct descr *r, *track;
 track = descr_list;
    while(descr_list != NULL)
    {
     if(descr_list->state = 0)
     {
         r=descr_list;
         free(descr_list);
     }
     descr_list = descr_list->next;
    }
    descr_list = track;
}

[/QUOTE] It will not work, for all the reasons I gave you in my last post, which I really, really, really wish you'd read.

---------- Post updated at 11:43 AM ---------- Previous update was at 11:09 AM ----------

And you're still forgetting all your return 1's! The return 0's and error checking are useless if you don't return success when you actually have success!


Here's working code for a linked list.

Code:
#include <stdio.h>
#include <stdlib.h>

struct descr
{
    int state;
    struct descr *next;
};

// We pass a pointer to the head pointer for the list since we may need to
// change it.
struct descr *append_list(struct descr **head, int state) // Add new thing to the end
{
    struct descr *list;
    // Create new node
    struct descr *n=malloc(sizeof(struct descr));
    if(n == NULL) return(NULL); // Return error if malloc failed.

    n->state=state;
    n->next=NULL;

    // Empty list?  Set head and we're done.
    if((*head) == NULL)
    {
        (*head)=n;
        return(n); // return success
    }

    list=(*head); // set list to the first node.

    // We know the list must not be empty now.
    // Fast-forward until we're at the last descr.
    while(list->next != NULL) list=list->next;

    // Set the 'next' of the last descr to the new descr.
    list->next=n;

    // 'head' is fine where it is, so we're all done.
    return(n);
}

// Deletes all nodes set to the number 'state'.
int delete_state(struct descr **head, int state)
{
    struct descr *list;
    if((*head) == NULL) return(1); // Nothing to do when empty, but that's okay

    // If the list goes 0-> 0-> 0-> 0, we'll need to delete
    // the first node over and over.  After we delete, the second
    // becomes the first, so we start over.
    while((*head)->state == state)
    {
        struct descr *tmp=(*head)->next;
        free(*head); // Free the first node in the list
        (*head)=tmp; // Set the head pointer to the new first node

        if((*head) == NULL) return(1); // If list==NULL here, the list is empty and we're done!
    }

    // Below here, we're no longer changing the first node, so don't have to
    // worry about updating (*head).
    list=(*head);

    while(list->next != NULL)
    {
        // Keep deleting the node after the current one until we find a node
        // we don't need to delete.  When we delete the node after,
        // the node after that becomes the new 'next', so it's a loop.
        while(list->next->state == state)
        {
            struct descr *tmp=list->next->next; // Save the node after
            free(list->next);
            list->next=tmp;
            // if list->next is NULL, list->next->state will CRASH!
            // So when it is, quit the loop immediately!
            if(list->next == NULL) break;
        }

        list=list->next;
        if(list == NULL) break; // NULL means we hit the end of the list and must stop NOW!
    }

    return(0);
}

void free_list(struct descr **head)
{
    while((*head) != NULL)
    {
        struct descr *tmp=(*head)->next;
        free(*head);
        (*head)=tmp;
    }
}

int main(void)
{
    struct descr *head=NULL; // This is the head pointer our functions change with '(*head)'
    struct descr *n, *a, *b, *c, *d, *e, *f; // These will point to nodes we make.

    // create a list 0->1->2->0->3->0
    a=append_list(&head, 0);
    b=append_list(&head, 1);
    c=append_list(&head, 2);
    d=append_list(&head, 0);
    e=append_list(&head, 3);
    f=append_list(&head, 0);

    e->state=4; // Update the '3' node to '4'

    // print each node.
    for(n=head; n!=NULL; n=n->next)
      printf("-> %d", n->state);
    printf("\n");

    // delete all nodes where state==0
    delete_state(&head, 0);

    // print it again.
    for(n=head; n!=NULL; n=n->next)
      printf("-> %d", n->state);
    printf("\n");

    // Beware that pointers a, d, and f are invalid now since
    // their memory was freed
    // Don't use f->state or anything now.

    // Free the entire list.  It will set head to NULL, too.
    free_list(&head);
}


Last edited by Corona688; 09-30-2011 at 02:56 PM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

trying to create a virtual database..

:cool: I like Vibhory2 like the idea of tapping into the kernel.. although not as indepth as he/she... i want to create a virtual databse of a few ma chines with connectivity and defiinte knowledge to create it.. I havebeen working on the project for a year now.. ! before unix (1 year exactly) i... (20 Replies)
Discussion started by: moxxx68
20 Replies

2. UNIX for Advanced & Expert Users

create database on unix

how can i create database on unix from command line (without using DBCA) (2 Replies)
Discussion started by: gfhgfnhhn
2 Replies

3. Solaris

Can't create database after Oracle Database installation

I installed Oracle 10 software on Solaris 11 Express, everything was fine execpt I can't create database using dbca.rsp file. I populated file with following options. OPERATION_TYPE = "createDatabase" GDBNAME = "solaris_user.domain.com" SID = "solaris_user" TEMPLATENAME = "General... (0 Replies)
Discussion started by: solaris_user
0 Replies

4. Shell Programming and Scripting

Create database using Backup file

Hi, I have backup file of database in my server. I want to create a that database in the same Mysql Server. How can I do that? Please send the steps to create the database using backup file? Thanks a lot, (1 Reply)
Discussion started by: aish11
1 Replies

5. Programming

Creating a bash script that create/open database

Hi. I have two text files(tables) which include some information and I want to make some query codes using them. First of all, I want to create bash script that read this two tables, create/open database and insert data from files into database. #!/bin/bash while read line; do ... (1 Reply)
Discussion started by: rlaxodus
1 Replies

6. UNIX and Linux Applications

Really simple shell script to create oracle database

Hello , I am new in this forum and need your help as I am totally confused :confused: I read a lot of threads and tried to search a lot but did not get the exact answer to my question. I just want a simple (content wise may be long) shell script to create oracle database. In detail:... (5 Replies)
Discussion started by: rahoolm
5 Replies

7. Shell Programming and Scripting

Help with script to create users from database query

#!/bin/bash user=`mysql userList -uuserlist -puserlistpassword -s -N -e "SELECT userName FROM users WHERE activated='n'"` for i in $user; do useradd "$i" -m doneThis is what I have done so far. But obviously it still does not work. I'm trying to create users based on information stored in a... (5 Replies)
Discussion started by: bucketuk
5 Replies

8. UNIX for Dummies Questions & Answers

Create Alert for Database Problem

Hi all, I new to scripting and i need to know how to put the script when the capture goes down in the feeds database.. I tried with this "ps -ef | grep asn" command and it displaying the capture,apply time. But the alert we are receving now is replication is failed . So i need to develop the... (1 Reply)
Discussion started by: g.nanthagopal
1 Replies

9. AIX

Need a graphical interface on AIX server to create database

Hello, Please suggest me the ways how to get graphical interface on AIX server.I need to create oracle database for which I need graphical access. Best regards, Vishal (4 Replies)
Discussion started by: Vishal_dba
4 Replies

10. Shell Programming and Scripting

awk parsing file to create a database

Hi Guys, I have a list a hotels stored in many different text files. This list is kept in the following format: 20/03 Hotel: The Bear Hotel Honey Street Woodstock UK Tel:+44-xxxxxx Rate: 100 21/03 Hotel: The Bush Hotel Nice Street Farnham (4 Replies)
Discussion started by: freddie50
4 Replies
All times are GMT -4. The time now is 10:47 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy