Create a database in C


 
Thread Tools Search this Thread
Top Forums Programming Create a database in C
# 1  
Old 09-29-2011
Create a database in C

Hey, I have this basic server thing going on and I can't seem to allocate
memory correctly and I can't access the memory after I allocated it. I
was hoping people might suggest methods of doing this. I was thinking of
using the socket file descriptor to reference the users.
# 2  
Old 09-29-2011
What did you try, and in what way did it not work?
# 3  
Old 09-29-2011
You already kind of helped me with this problem but I ended up deleting all
of that because I couldn't access access each element. Everything would
compile but it wouldn't work the way I had expected.

Iterating and calloc questions.
# 4  
Old 09-29-2011
The compiler just tells you whether your code is syntactically correct, it doesn't tell you whether it works. It doesn't care whether your program cuts off its own foot as you end it in a semicolon.

Your problem in the other thread wasn't allocating memory. There's nothing wrong with the way you called malloc(). The problem was what you did with the memory after. You kept free()ing things you still needed, using the wrong pointers, accidentally overwriting things, etc. Your algorithm made no sense, and I couldn't correct it because you never explained what you were actually trying to do except in a generic "it uses memory" kind of way.

What you do with memory depends on how you want to arrange it and what you're putting in it.
# 5  
Old 09-29-2011
Well I want to tell you my first problem I have trying to use initialize_descr
below. The function send causes my server to crash. I initialized the data
and all should be well but I can't send data to init->newfd

Code:
int initialize_descr(int newfd, struct descr *p)
{
    struct descr *d;
    d = descr_list;
    if ( d != NULL ) 
    {
        while ( d->next != NULL)
        {
            d = d->next;
        }
        d = d->next;
    }    
 
    if(!(d = malloc( sizeof(struct descr))))
    {
        nonfatal("malloc failure");
        return 0;
    }
    d->newfd = newfd;
    d->state = 1;
    d->next = NULL;
    p = d;
}
 

    struct descr *init = NULL;
    
    FD_SET(newfd, &master); /* add to master set */
    if (newfd > fdmax)      /* keep track of the max */
                   fdmax = newfd;
    const char name_prompt[] = "Character name or (n)ew: ";
    initialize_descr(newfd, init);
    send(init->newfd, name_prompt, sizeof name_prompt, 0);

---------- Post updated at 12:36 PM ---------- Previous update was at 12:23 PM ----------

Because of that problem I am semi diverted from using that function. I believe
someone helped me fix that once and it required using a pointer to a pointer or
something that I didn't understand well enough to want to use it.
# 6  
Old 09-29-2011
Smilie That function uses all kinds of things you don't define anywhere and you still aren't telling me what it's supposed to do.

I can see lots of obvious errors in it though -- first and foremost you never actually allocate memory for 'init', so you're just feeding a null into your initialize_descr, you copy around pointers as if that changes their contents(it doesn't) and you attempt to read from NULL with init->newfd.

Your initialize_descr function seems strange too:

Code:
int initialize_descr(int newfd, struct descr *p)
{
    struct descr *d;
    d = descr_list;
    if ( d != NULL ) 
    {
        // Okay, you're trying to find the end of the list.
        while ( d->next != NULL)
        {
            d = d->next;
        }

        // Since the while loop ended, we know d->next == NULL.
        // So you're setting d=NULL, making the whole while loop pointless.
        d = d->next;
    }    
 

    // except it was pointless anyway, because the first thing you do
    // with 'd' once you've found the end of the list is trash it,
    // making it point to whatever malloc returns instead.
    if(!(d = malloc( sizeof(struct descr))))
    {
        nonfatal("malloc failure");
        return 0;
    }
    d->newfd = newfd;
    d->state = 1;
    d->next = NULL;

    // This does not do what you think it does.  You are setting the local variable p
    // to the address stored in d.  This does absolutely nothing to the memory
    // at that address.  It also does absolutely nothing to the variable that was
    // fed into this function.
    p = d;
    // No default return value, so you have no idea what it actually returns here
}

I think you're trying to append to the end of the list, right? In which case:

Code:
struct descr *initialize_descr(int newfd)
{
    struct descr *d=descr_list;

    if(d == NULL)
    {
      d=malloc(sizeof(struct descr));       // Empty list, so allocate new space
      descr_list=d; // list is no longer empty
    }
    else
    {    // Find the end of the list
        while(d->next != NULL) d=d->next;
        // Allocate new memory and add it to ->next, NOT d
        d->next=malloc(sizeof(struct descr));
        if(d->next == NULL) return(NULL); // alloc error
        // It is now in the list.
        // Set 'd' to the new memory so we can set it below.
        d=d->next;
    }

    if(d == NULL) return(NULL); // alloc error

    // Either way the first 'if' goes, 'd' is now memory we want to initialize.
    d->newfd = newfd;
    d->state = 1;
    d->next = NULL; // It's the end of the list
    return(d); // return a pointer to the new memory
}

...

struct descr *d=initialize_descr(fd);
...


Last edited by Corona688; 09-29-2011 at 01:59 PM..
# 7  
Old 09-29-2011
I want to save characters in a game. I want to use a structure so that I can make
mobile units that will smile at you if your room number is the same as theres when
the game loop iterates through a mobile function. I want to be able to save prompts
for the user so they can have cool looking experience mojo and I want to be able to
save each character every five minutes or so but I need to successfully allocate and
access and save and write to functions because you can't use one variable
simultaniously, Otherwise I will have to use files for some of this stuff and I don't
want to have to do that untill the game saves the character. Are you still lost as to
what I am trying to accomplish?

lastly I want to be able to send messages to anyone with the name
errigour or lovesly if tom types tell lovesly <message>.

---------- Post updated at 12:55 PM ---------- Previous update was at 12:55 PM ----------

I'm working on my own mud base.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question