Announcement: Be excellent to each other.


Caravel Forum : Other Boards : Anything : The incredible edible programming thread
1
Page 2 of 2
New Topic New Poll Post Reply
Poster Message
b0rsuk
Level: Smiter
Avatar
Rank Points: 489
Registered: 11-23-2003
IP: Logged
icon Re: The incredible edible programming thread (0)  
Solved.
I changed first use of strcat function to strcpy . I did it because I already changed it from strcpy to strcat. At the time I tried to print contents of the variable, and somehow strcpy variant printed ugly character at beginning of the string.

Don't ask me why. I could use some official explaination.

____________________________

http://www.gamasutra.com/features/20051128/adams_01.shtml
11-11-2005 at 07:04 PM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
trick
Level: Legendary Smitemaster
Rank Points: 2580
Registered: 04-12-2003
IP: Logged
icon Re: The incredible edible programming thread (+1)  
b0rsuk wrote:
The hexdump function you gave me prints 02 character by definition. Why ?
No, it doesn't. The "%02x" in there means "take the integer argument and print it as a hexidecimal number with two digits, with a leading zero if required". If it prints 02, there's really a 0x02 byte in the string.
Solved.
I changed first use of strcat function to strcpy . I did it because I already changed it from strcpy to strcat. At the time I tried to print contents of the variable, and somehow strcpy variant printed ugly character at beginning of the string.

Don't ask me why. I could use some official explaination.
I already explained this, actually (thought you had already fixed this ?), but sure, I can do it again:

When you define and allocate an array like this in C:
char foo[20];
You don't actually initialize the memory. This means that the memory the array occupies can be anything. In your case, this memory happened to contain:
02 00 xx xx xx ..
When you call strcat on this uninitialized array, strcat assumes there's already a valid string in there, so it searches the array for the first occurence of 0, the terminating zero. In your case, the first character in the array was 0x02, which is not equal to zero, so it goes on to the next one, which, by pure chance, contains a zero. "Yay!" thinks strcat, "a terminating zero! This is where I'll start copying the string I was given. Oh, what a joyous day." And so it does. Result: You get a string starting with 0x02. The fact that it was 0x02 is random, it could really have been anything.

Moral of the story: Don't use uninitialized memory! :)

- Gerry
11-12-2005 at 03:13 AM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
b0rsuk
Level: Smiter
Avatar
Rank Points: 489
Registered: 11-23-2003
IP: Logged
icon Re: The incredible edible programming thread (0)  
trick wrote:
Moral of the story: Don't use uninitialized memory! :)
- Gerry

Moral of the story: My head hurts. It's 05:08, I STILL have 3 programs to go. I have to be at university at 08:00.

As far as C server goes, I got (basically) everything to work except "write to file" part. It worked if I used putchar instead of fputc, and no amount of tweaking will help. It just the "odane" function just eats all input and nothing goes to "dane" file. I use put functions because it's supposed to stop at escape character (I use \\e and it works)


int odane(char *gdzie)
{
char ch;
    char plik[strlen(gdzie) + 6];       /* 5 za dane */
    strcpy(plik, gdzie);
    strcat(plik, "/dane");
    printf("Test plik3: %s\\n", plik);

    FILE *zapis = fopen(plik, "w");
    if (zapis != NULL)
{
do {ch = getchar();
fputc(ch, zapis); }
while (ch != '\\e'); return 0;
}
else return 1;
}


int zapukaj(char *gdzie)
{
    char plik[strlen(gdzie) + 10];      /* 9 za /lockfile, +1 za znak koncowy */
    strcpy(plik, gdzie);
    strcat(plik, "/lockfile");

    if ((open(plik, O_WRONLY | O_CREAT | O_EXCL, 0666)) == -1) {
        return 1;
    } else {
        close(plik);
        return 0;
    }
}                               /*koniec funkcji zapukaj */

int swynik(char *gdzie)
{
    char plik[strlen(gdzie) + 7];       /* Dodatek za wyniki */
    strcpy(plik, gdzie);
    strcat(plik, "/wyniki");
    printf("Test plik2: %s\\n", plik);
    if ((open(plik, O_WRONLY | O_CREAT | O_TRUNC, 0666)) == -1) {
        return 1;
    } else {
        close(plik);
        return 0;
    }
}

/* MAIN */
/* MAIN */
/* MAIN */
/* MAIN */
int main(int argc, char *argv[])
{

    if (argc != 2) {
        printf("Format wywolania: cclient sciezka \\n");
        return 1;
    }

    char ja[strlen(getlogin())];
    strcpy(ja, getlogin());
    printf("%s\\n", ja);

    char sciekli[strlen(ja) + 12];      /* sciezka do roboczego klienta */
    strcpy(sciekli, "/home/zaocz/");
    strcat(sciekli, ja);
    strcat(sciekli, "/tmp");
    printf("Test sciekli:%s\\n", sciekli);

/* teraz zajme sie sciezka */
    char sciezka[strlen(argv[1]) + 17]; /* 12 bo /home/zaocz/ ma tyle; +4 za /tmp */
    strcpy(sciezka, "/home/zaocz/");
    strcat(sciezka, argv[1]);   /* login osoby z serwerem */
    strcat(sciezka, "/tmp");    /* calosc to katalog roboczy SERWERA */
    printf("Test - %s \\n", sciezka);


    if (swynik(sciekli) == 1) {
        printf("Blad przy tworzeniu pliku: wyniki \\n");
        return 1;
    } else
        printf("powstal wynik \\n");


/* Poczatek wlasciwego programu */
    while (zapukaj(sciezka) != 0) {
        printf("Zajete ! \\n");
        sleep(1);
    }
    printf("Stworzono lockfile - teraz wrzucenie danych\\n");
    if (odane(sciezka) == 1) {
        printf("Blad przy otwieraniu danych\\n");
        return 1;
    } else
        printf("Dane wrzucone\\n");



odane function is meant to put input where it belongs. The path (plik) is ok, I checked it.
ANyway, you won't repply fast enough,* even assuming you have nothing to do but to search for bugs in my ugly and needlessly long code.

Another C program is supposed to generate N child processes. I tried to do it in a simple loop, but it doesn't stop generating processes. So another failure.

The 3rd program is just sweet - bash calculations with n! thingies, newton thingies (n!)/(k!(n-k)!), and big variables. Several loong hours of "coding" wasted.


* so the sole point of this post is to let you know that MY HEAD HURTS and it won't get better for twenty hours or so.

____________________________

http://www.gamasutra.com/features/20051128/adams_01.shtml

[Last edited by b0rsuk at 11-12-2005 04:28 AM]
11-12-2005 at 04:21 AM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
trick
Level: Legendary Smitemaster
Rank Points: 2580
Registered: 04-12-2003
IP: Logged
icon Re: The incredible edible programming thread (+1)  
b0rsuk wrote:
Moral of the story: My head hurts. It's 05:08, I STILL have 3 programs to go. I have to be at university at 08:00.
I know the feeling. I'm in the same time zone as you, you know. Anyway, I wish you good luck.

int odane(char *gdzie)
{
char ch;
    char plik[strlen(gdzie) + 6];       /* 5 za dane */
    strcpy(plik, gdzie);
    strcat(plik, "/dane");
    printf("Test plik3: %s\\n", plik);

    FILE *zapis = fopen(plik, "w");
    if (zapis != NULL)
{
do {ch = getchar();
fputc(ch, zapis); }
while (ch != '\\e'); return 0;
}
else return 1;
}
Are you sure the file is actually opened ? Also, you don't close the file, so the buffer might not be flushed (this is admittedly a long shot, but still possible). Try inserting 'fclose(zapis);' before 'return 0;'. Also:
(...)
    char plik[strlen(gdzie) + 8];       /* Dodatek za wyniki, +1 za znak koncowy */
(...)
    char sciekli[strlen(ja) + 17];      /* sciezka do roboczego klienta */
(...)

ANyway, you won't repply fast enough,* even assuming you'd care enough to search for bugs in my code.
Well, I hope I replied fast enough. I know how annoying these things can be. Anyway, got msn messenger or icq/aim ? I'm gerry_jj@hotmail.com at msn (doesn't work as email) and 4979224 at icq.

- Gerry

[Last edited by trick at 11-12-2005 04:47 AM]
11-12-2005 at 04:46 AM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
b0rsuk
Level: Smiter
Avatar
Rank Points: 489
Registered: 11-23-2003
IP: Logged
icon Re: The incredible edible programming thread (0)  
trick wrote:
b0rsuk wrote:
Moral of the story: My head hurts. It's 05:08, I STILL have 3 programs to go. I have to be at university at 08:00.
I know the feeling. I'm in the same time zone as you, you know. Anyway, I wish you good luck.
So you probably know the "eyes full of sand" feeling, too ?

Are you sure the file is actually opened ? Also, you don't close the file, so the buffer might not be flushed (this is admittedly a long shot, but still possible). Try inserting
'fclose(zapis);' before 'return 0;'.
Well, if there's no "dane" file, fopen sucessfuly creates one. By the way: can I change default behaviour of fopen so it doesn't create a file if there's none ?

The main reason why I chose fopen instead of read/write and similar is because end of input is meant to be signalised (for both client and server) by ESC character. Getchar and variants make it easier to check if current character is the ESC character. And that's why I use do while loop - because it's meant to be written to file, too.

Also:
(...)
    char plik[strlen(gdzie) + 8];       /* Dodatek za wyniki, +1 za znak koncowy */
(...)
    char sciekli[strlen(ja) + 17];      /* sciezka do roboczego klienta */
(...)


I think that you're overestimating the importance of +1. If I understand correctly, strlen already takes care of that, and strcat (if I remember correctly) moves the zero to the end of string.
I even tried enlarging the array to 50 or more and nothing changed.
Last time I fixed it by changing first use of strcat to strcpy. This time I began with strcpy and... no good.

Well, I hope I replied fast enough. I know how annoying these things can be. Anyway, got msn messenger or icq/aim ? I'm gerry_jj@hotmail.com at msn (doesn't work as email) and 4979224 at icq.
I only have gg (Poland-specific messenger) and Skype (which I use mostly for text messages, because I type faster than speak (in english))
My Skype account is czlowiekwiadro (OneManBucket)


It turned out that we have extra dose of "concurrency/multithreading" classes tomorrow. But I get back home at around 19 o'clock and I already missed several hours of sleep, so it's not likely I'll be able to finish the programs in last few hours.
Even more C tasks lie ahead - FIFO queues...
EDIT
So far the only FIFO assignments are in Bash. FIFOs seem trivial in bash :D

____________________________

http://www.gamasutra.com/features/20051128/adams_01.shtml

[Last edited by b0rsuk at 11-12-2005 08:14 AM]
11-12-2005 at 07:50 AM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
trick
Level: Legendary Smitemaster
Rank Points: 2580
Registered: 04-12-2003
IP: Logged
icon Re: The incredible edible programming thread (+1)  
b0rsuk wrote:
So you probably know the "eyes full of sand" feeling, too ?
Mm.
Well, if there's no "dane" file, fopen sucessfuly creates one. By the way: can I change default behaviour of fopen so it doesn't create a file if there's none ?
No, you can't change the behavior of fopen like this (unlike open). However, you can do what you want by using stat:
struct stat st;

if (!stat("file", &st)
{
    /* file exists */
}
else
{
    /* file doesn't exist (or path was unreadable) */
}
You could also have used 'fopen("file", "r");' to check if the file exists, but that would have failed in the case of a write-only file, for example.
I think that you're overestimating the importance of +1. If I understand correctly, strlen already takes care of that, and strcat (if I remember correctly) moves the zero to the end of string.
No, strlen returns the length of the string in characters. So, strlen("a") returns 1, but the string "a" requires 2 chars -- one for the 'a', and one for the terminating zero. Ie you always have to add 1 to the result of strlen to get the number of chars to allocate. You're right about strcat.

I even tried enlarging the array to 50 or more and nothing changed.
Last time I fixed it by changing first use of strcat to strcpy. This time I began with strcpy and... no good.
So adding the fclose didn't help ?

My Skype account is czlowiekwiadro (OneManBucket)
Don't have a Skype account, sorry .. Anyway, is that a Discworld reference ? :)

- Gerry

[Last edited by trick at 11-12-2005 09:58 AM : nonsense]
11-12-2005 at 09:45 AM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
b0rsuk
Level: Smiter
Avatar
Rank Points: 489
Registered: 11-23-2003
IP: Logged
icon Re: The incredible edible programming thread (0)  
...so I got up around 3 o'clock only to find that the server I keep programs on is down. I have some bits of the program here, but the strings are hardcoded, it seems.

B0rsuk is fuming mad.

____________________________

http://www.gamasutra.com/features/20051128/adams_01.shtml
11-13-2005 at 03:16 AM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
trick
Level: Legendary Smitemaster
Rank Points: 2580
Registered: 04-12-2003
IP: Logged
icon Re: The incredible edible programming thread (+1)  
Thus b0rsuk learned the value of backups.

That said, I just ran into a similar problem myself, actually. I needed a file on a university webpage, and the server seemed to be down. This was disaster, so I figured I could try accessing the page from within the university net as well just in case. ssh'ed to the university login server, fired up mozilla through remote X, and tried to access the page from there -- and it worked! Yay!

My point is, if you've got some kind of access to a different server in the same net, that might be worth a shot.

- Gerry

[Last edited by trick at 11-13-2005 09:25 AM]
11-13-2005 at 09:17 AM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
DiMono
Level: Smitemaster
Avatar
Rank Points: 1181
Registered: 09-13-2003
IP: Logged
icon Re: The incredible edible programming thread (0)  
b0rsuk wrote:
...so I got up around 3 o'clock only to find that the server I keep programs on is down. I have some bits of the program here, but the strings are hardcoded, it seems.

B0rsuk is fuming mad.
I accidentally deleted my master (read: only) copies of several files while developing my new website by thinking a link was a real directory. You're not the only one to get bitten in the butt by having only one copy of files. Just remember to learn your lesson: always back up your data!

I just want to say thank you to everyone who helped me out in this thread. I launched my site yesterday and it looks great. I couldn't have done it without you... well, not as quickly anyway.

____________________________
Deploy the... I think it's a yellow button... it's usually flashing... it makes the engines go... WHOOSH!
11-13-2005 at 04:14 PM
View Profile Send Private Message to User Send Email to User Visit Homepage Show all user's posts This architect's holds Quote Reply
b0rsuk
Level: Smiter
Avatar
Rank Points: 489
Registered: 11-23-2003
IP: Logged
icon Re: The incredible edible programming thread (0)  
1. I use ssh all the time.

(squirrels report it may have been a DOS attack)

2. I was able to download my _entire_ program from this forum. I was just too lazy to rewrite several of my functions and then do it again. Or, not bright enough (3:00 am) to just imitate the directory tree.
In any case, I had some important stuff in @mails. I use my univ account for mail

3. It wouldn't matter, even if I had step-by-step guide. I didn't make any progress in 1.5 hours. Unless by "progress" you mean "stuck on another bug".
The open function would create a file, and return (null) as file descriptor. Oh joy.

So far, I fininished one program and failed another (concurrence classes). New tasks:
1. client-server apps using FIFO, in bash. (1 week)
2. client-server + FIFO + C. (2 weeks).

4. A while ago, I deleted my new hold by mistake. I was going to delete just one (empty) level, but you know the interface... I rebuilt all except one room, which was actually quite good but took a lot of time to polish.

5. I HAVE NO TIME TO PLAY DROD ! !

____________________________

http://www.gamasutra.com/features/20051128/adams_01.shtml

[Last edited by b0rsuk at 11-13-2005 06:21 PM]
11-13-2005 at 05:51 PM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
b0rsuk
Level: Smiter
Avatar
Rank Points: 489
Registered: 11-23-2003
IP: Logged
icon Re: The incredible edible programming thread (0)  
In this post, I explained my problem with reading from named pipes/FIFOs using Bash. But in meanwhile I solved it by using & instead of ; . So I may as well say something
OFF-TOPIC
Yes, it's a Discworld reference.
Speaking abou Discworld references, how do you explain Information Retrieval Technicians in DROD:JTRH ? (If what squirrells report is correct). Probably Krammer's idea.

____________________________

http://www.gamasutra.com/features/20051128/adams_01.shtml

[Last edited by b0rsuk at 11-17-2005 11:34 AM]
11-17-2005 at 09:47 AM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
Mouse
Level: Master Delver
Avatar
Rank Points: 246
Registered: 04-21-2005
IP: Logged
icon Re: The incredible edible programming thread (0)  
This question totally does not follow any of the preceding stuff but anyway:

What's the best way to go about learning PHP?

11-17-2005 at 10:38 PM
View Profile Send Private Message to User Show all user's posts Quote Reply
ErikH2000
Level: Legendary Smitemaster
Avatar
Rank Points: 2796
Registered: 02-04-2003
IP: Logged
icon Re: The incredible edible programming thread (+1)  
I don't know why we gotta have one huge topic for everything about programming. Starting a new one for your question, Mouse.

-Erik

____________________________
The Godkiller - Chapter 1 available now on Steam. It's a DROD-like puzzle adventure game.
dev journals | twitch stream | youtube archive (NSFW)
11-17-2005 at 11:29 PM
View Profile Send Email to User Show all user's posts This architect's holds Quote Reply
b0rsuk
Level: Smiter
Avatar
Rank Points: 489
Registered: 11-23-2003
IP: Logged
icon Re: The incredible edible programming thread (0)  
I finally managed to post in this thread again. I tend to solve my C problems while writing problem description here. Then I abort sending reply and feel silly.
In cases of unclear specification I mailed my teacher twice, but it's late now and I'm going to abuse people from diffrent timezones ;-).

I'll try to be brief.
We were asked to write yet again C client/server program (a pair of programs, in fact).
Data sent from client to server is supposed to look like this:
length   ID   $HOME
int      int  string


The string passed from client to server is last name of an user. If there's such user in the database... blah blah. The point is that it's of variable size.
First int is supposed to be lenght of the rest of the "message" - int + space + string + \\0.

I'm thinking about sizeof versus strlen.
The teacher suggested sizeof, but I suspect he made a mistake/confused me with someone else. That's because I found an article comparing strlen to sizeof... and it says sizeof works on compile time, while strlen on run time and may therefore be less efficient.
http://www.devx.com/tips/Tip/12682

Wait a second. Does it mean sizeof is useless for anything that could be of variable lenght ?
I'm going to finish the program today using strlen, but I'm somewhat confused and would like to see your point of view.

____________________________

http://www.gamasutra.com/features/20051128/adams_01.shtml

[Last edited by b0rsuk at 11-24-2005 08:30 PM]
11-24-2005 at 08:29 PM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
trick
Level: Legendary Smitemaster
Rank Points: 2580
Registered: 04-12-2003
IP: Logged
icon Re: The incredible edible programming thread (+1)  
b0rsuk wrote:
I'm thinking about sizeof versus strlen.
The teacher suggested sizeof, but I suspect he made a mistake/confused me with someone else. That's because I found an article comparing strlen to sizeof... and it says sizeof works on compile time, while strlen on run time and may therefore be less efficient.
This is correct (although the compiler may optimize away strlens on any constant strings it finds .. this of course depends on the compiler, optimization settings, etc)
Wait a second. Does it mean sizeof is useless for anything that could be of variable lenght ?
Yes. It's impossible for the compiler to find the length of something with unspecified length.

Also .. here, try this:
#include <stdio.h>
#include <string.h>

void hohum (char arg[])
{
    char foo[] = "fun with sizeof";
    char *bar = "fun with sizeof";
    printf("sizeof(foo) = %i, strlen(foo) = %i\\n", sizeof(foo), strlen(foo));
    printf("sizeof(bar) = %i, strlen(bar) = %i\\n", sizeof(bar), strlen(bar));
    printf("sizeof(arg) = %i, strlen(arg) = %i\\n", sizeof(arg), strlen(arg));
}

int main (int argc, char *argv[])
{
    hohum("fun with sizeof");
    return 0;
}

With gcc, I get this:
sizeof(foo) = 16, strlen(foo) = 15
sizeof(bar) = 4, strlen(bar) = 15
sizeof(arg) = 4, strlen(arg) = 15


- Gerry

[Last edited by trick at 11-24-2005 11:45 PM]
11-24-2005 at 11:31 PM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
jamie
Level: Smiter
Rank Points: 365
Registered: 04-15-2005
IP: Logged
icon Re: The incredible edible programming thread (+1)  
Eeek, That surprises me. I expected :

char foo[] = "fun with sizeof";
char *bar = "fun with sizeof";

to both be 16, seeing as the second is statically set tooo, but obviously it's showing the size of the pointer itself.

As for b0rsuk's problem I tend to only use sizeof to set the bounds on a variable size, within a snprintf, or strncpy, such as :

char bigstring[100];

.
.
.

strncpy (bigstring, whatever, sizeof(bigstring));

That's a neat way to avoid buffer overflows, because at compile time, the size is set to the size of the defined variable, so even if someone alteres the definition of bigstring, everything else will follow accordingly.

Then, strlen is used to find the length of some variable-length string during runtime - so the 2 are really quite different in usage.

____________________________
#f3i2g# Disclaimer: I'm Welsh, left-handed, and stupid. #f3i2g#

[Last edited by jamie at 11-25-2005 05:18 AM : Lemmings!]
11-25-2005 at 05:14 AM
View Profile Send Private Message to User Send Email to User Visit Homepage Show all user's posts Quote Reply
b0rsuk
Level: Smiter
Avatar
Rank Points: 489
Registered: 11-23-2003
IP: Logged
icon Re: The incredible edible programming thread (0)  
Bonk ! Ugh ! - sounds of B0rsuk hitting a major obstacle

I just realised that I need to write an integer to a buffer. None of string.h etc functions supports this. Judging by results of my googling, this is fairly tricky thing to do.

I know snprintf or similar function would work, but we're encouraged to use functions from read/write/open family. On top of that I already spent long hours mastering their usage.
I suppose fprintf would work, too. I'm afraid writing such a function (integer to buffer) is beyond my ability, so I'll have to accept some workaround.

What would you suggest as easiest/most reliable/foolproof workaround ? Rebuild the program and use fprintf ?

____________________________

http://www.gamasutra.com/features/20051128/adams_01.shtml

[Last edited by b0rsuk at 11-25-2005 12:06 PM]
11-25-2005 at 12:05 PM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
trick
Level: Legendary Smitemaster
Rank Points: 2580
Registered: 04-12-2003
IP: Logged
icon Re: The incredible edible programming thread (+1)  
Well, *printf is the easiest. You don't have to switch your program to use fprintf, you could just write the integer to a string using snprintf or somesuch, and then write this string using write. Mixing stdio and posix like this works fine as long as you don't attempt to read/write files using both interchangeably, although it might not be considered very "clean", of course :)

There are various other functions that convert integers to strings, but none of them are standard, that I know of at least. Windows has _itoa, for example, but that doesn't exist in glibc. There's plenty to choose from if you want to go the other way (atoi, strtol, etc)... Also, in C++ you can use streams to convert your integers easily. Of course, none of this helps you, so never mind.

Anyway, writing your own integer-to-string converter isn't too hard to do. Start at the end, get the remainder of the base and add '0' to get the ascii char, then integer divide the number by the base and repeat. Using the number 123 as an example:
   123 % 10 = 3   ->  3 + '0' = '3'
   123 / 10 = 12
    12 % 10 = 2   ->  2 + '0' = '2'
    12 / 10 = 1
     1 % 10 = 1   ->  1 + '0' = '1'
     1 / 10 = 0   ->  done

If you store this as increasing characters in a string, you get "321" -- so, instead you use a fixed string length (maximum possible for an integer of sizeof(int)*8 bits) and start storing the chars at the end of this string, decreasing the index as you go. When you're done, the string starts at (start of full string) + index, so you return that, and lo! behold! you've got your string.

Here's an example of this for base 10. Give it a buffer of at least the minimum size and the number to stringify.
char * int2str (char *buf, int number)
{
    unsigned int i = 20;  /* max for 64-bit */
    unsigned int negative = number < 0;
    if (negative)
        number = -number;
    buf[i] = 0;
    do buf[--i] = number % 10 + '0';
    while (number /= 10);
    if (negative)
        buf[--i] = '-';
    return buf + i;
}

- Gerry

[Last edited by trick at 11-25-2005 03:20 PM : typo]
11-25-2005 at 03:20 PM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
b0rsuk
Level: Smiter
Avatar
Rank Points: 489
Registered: 11-23-2003
IP: Logged
icon Do My Homework For Me (0)  
Hey
I'm having hard time doing my last howework. It's supposed to be Tic Tac Toe/Noughts And Crosses implementation using semaphores and shared memory. I vaguely understand shared memory and semaphores, but can't get past supposingly simple task: placing an array (say.. arr[9] ) in shared memory. I admit I don't fully understand pointers etc, but I hoped I understood enough to edit it. I guess I was wrong.
I need help with only 1 thing: how to place an array in shared memory ? I would figure the rest on my own (semaphores, state checking etc) The example below places a string in a segment.
The deadline is saturday, so don't bother repplying afterwards.
I know I'm supposed to use ftok and 3x3 array, but I'd rather solve simpler problems first. As it is, I can't move forward.
(3:58 am)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define klucz 10

int pamiec,wybor;
char *adres;

void koniec()
   {
      shmdt(adres);
      shmctl(pamiec,IPC_RMID,0);
      exit(0);
   }   

void odczyt()
   {
      printf("Jest zapisane: %s\\n",adres); // it's written:
   }
   
void zapis()
   {
      printf("Podaj lancuch: "); 
      scanf("%s",adres);
   }         

main()
   {
      pamiec=shmget(klucz,256,0700|IPC_CREAT);
      adres=shmat(pamiec,0,0);
      for (;;)
         {
            printf("M E N U :\\n");
            printf("\\n");
            printf("0 - odczyt\\n");
            printf("1 - zapis\\n");
            printf("2 - koniec\\n");
            printf("\\n");
            printf("Co wybierasz ? ");
            scanf("%d",&wybor);
            switch(wybor)
               {
                  case 0: odczyt(); break; // read
                  case 1: zapis(); break;  // write
                  case 2: koniec(); break; // end
                  default: printf("Nie ma takiej opcji ! \\n"); break; // invalid choice
               }
         }         
   }


____________________________

http://www.gamasutra.com/features/20051128/adams_01.shtml

[Last edited by b0rsuk at 01-13-2006 02:58 AM]
01-13-2006 at 02:57 AM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
trick
Level: Legendary Smitemaster
Rank Points: 2580
Registered: 04-12-2003
IP: Logged
icon Re: The incredible edible programming thread (+1)  
You can't place an array in shared memory. You can set up memory for it and access that shared memory, but the memory doesn't have a structure unless you give it one. So, you can do:
typedef struct {
    int arr[9];
} tictactoedat_t;

(..)

tictactoedat_t *sharedarray = (tictactoedat_t*)sharedmemorypointer;
sharedarray->arr[7] = 42;

Hope this helps.

- Gerry
01-13-2006 at 08:11 AM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
b0rsuk
Level: Smiter
Avatar
Rank Points: 489
Registered: 11-23-2003
IP: Logged
icon Re: The incredible edible programming thread (0)  
Just curious, what editor do you use for programming ?

My teacher is one of Emacs contributors, so it gives you an idea.
Myself, I used to use mc (midnight commander), but it's not so great. I learned to like Kate, it does everything mcedit does and more, I especially like ability to minimise/expand functions so that they occupy only single line, makes code much more readable (for you). Has some very cool coloring, too, helps you count empty spaces etc. It would be perfect-ish if there were some hotkeys to switch between main and shell windows...
I acknowledge power of Emacs, but it's a bit hard to start with all these weird hotkeys; it takes some getting used to. But it's got built-in psychatrist and you can play Nethack without closing Emacs, not that I like Nethack much.

____________________________

http://www.gamasutra.com/features/20051128/adams_01.shtml

[Last edited by b0rsuk at 02-21-2006 10:57 PM]
02-21-2006 at 10:55 PM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
Maurog
Level: Smitemaster
Avatar
Rank Points: 1501
Registered: 09-16-2004
IP: Logged
icon Re: The incredible edible programming thread (0)  
I really like EditPlus. It does coloring, has extermely easy to use interface, and you can set it to run the compilation commands from buttons (using user tool option). It's very light and powerful, but only works under Windows I think, that may be a disadvantage.

____________________________
Slay the living! Raise the dead!
Paint the sky in crimson red!
02-22-2006 at 01:21 PM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
Schik
Level: Legendary Smitemaster
Avatar
Rank Points: 5454
Registered: 02-04-2003
IP: Logged
icon Re: The incredible edible programming thread (+1)  
If I'm writing C++ code that will be compiled in Visual Studio, I just use Visual Studio's editor. Otherwise, I use Vim. Vim is responsible for 100% of my changes/additions to the forum.

A googlefight between emacs and vi proves that vi (or vim, an improved version of the original vi) is much better than emacs. But yeah, both vi and emacs have a steep learning curve in order to become proficient.

____________________________
The greatness of a nation and its moral progress can be judged by the way it treats its animals.
--Mahatma Gandhi
02-22-2006 at 01:30 PM
View Profile Send Private Message to User Show all user's posts High Scores Quote Reply
trick
Level: Legendary Smitemaster
Rank Points: 2580
Registered: 04-12-2003
IP: Logged
icon Re: The incredible edible programming thread (+1)  
For DROD I mostly use KDevelop. I'm not 100% satisfied with it, but other editors I've tried have just annoyed me more. The Gnome equivalents (I currently use Gnome as desktop) certainly aren't any good. On the sideline, Code::Blocks is more lightweight and could have been nice, but has some (for me) critical flaws. (Among other things, the tab switcher is just braindead, and it doesn't have ctags support. Ctags is a must.)

For simpler stuff, I like Kate. Nedit is also nice, once you tweak the interface a bit. In the terminal, I mostly use .. (Drumroll, please ..)

Click here to view the secret text


- Gerry
02-22-2006 at 03:27 PM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
1
Page 2 of 2
New Topic New Poll Post Reply
Caravel Forum : Other Boards : Anything : The incredible edible programming thread
Surf To:


Forum Rules:
Can I post a new topic? No
Can I reply? No
Can I read? Yes
HTML Enabled? No
UBBC Enabled? Yes
Words Filter Enable? No

Contact Us | CaravelGames.com

Powered by: tForum tForumHacks Edition b0.98.9
Originally created by Toan Huynh (Copyright © 2000)
Enhanced by the tForumHacks team and the Caravel team.