--------------- FIDO MESSAGE AREA==> TOPIC: 239 C LANGUAGE Ref: F3K00020 Date: 03/14/98 From: TOM TORFS Time: 05:30pm \/To: ROGER SCUDDER (Read 2 times) Subj: Avoid disaster Roger Scudder wrote in a message to John Dumas: RS> I'm not an expert on PC architecture, but as I understand it, RS> free and malloc are calling the operating system. It's the OS who RS> is doing all the work. That may be true for some implementations, but most I've seen (at least for DOS) implement their own memory allocation scheme and just ask one big block of memory from the OS at startup. greetings, Tom tomtorfs@village.uunet.be --- timEd/2 1.10+ * Origin: 80X86 BBS 32-15-24.62.32 V.34/V.FC (24h/24h) (2:292/516) --------------- FIDO MESSAGE AREA==> TOPIC: 239 C LANGUAGE Ref: F3K00021 Date: 03/14/98 From: TOM TORFS Time: 05:35pm \/To: MICHAEL ENGELBRECHT (Read 2 times) Subj: DXF-parser Michael Engelbrecht wrote in a message to All: ME> Can anyone post me some source? (preferably C, but any other ME> language is ok too); But if it's to be posted in this echo it'd better be C. greetings, Tom tomtorfs@village.uunet.be --- timEd/2 1.10+ * Origin: 80X86 BBS 32-15-24.62.32 V.34/V.FC (24h/24h) (2:292/516) --------------- FIDO MESSAGE AREA==> TOPIC: 239 C LANGUAGE Ref: F3K00022 Date: 03/14/98 From: TOM TORFS Time: 05:37pm \/To: DARIN MCBRIDE (Read 2 times) Subj: CRC model [9/12] Darin McBride wrote in a message to Bob Stout: BS> Thanks - it all worked great! DM> This will be on-topic only insofaras I'm asking about the echo's DM> moderation. Who is supposed to tell Bob that "thank-you messages" DM> are off-topic, the moderator (Tom) or the zone's co-moderator DM> (Roger)? :-) As a matter of fact, I've sent out a message a few days ago to the zone moderators that everybody should do this sort of thing for their own zone. But in this case an acknowledgement was desirable, considering the previous problems. greetings, Tom tomtorfs@village.uunet.be --- timEd/2 1.10+ * Origin: 80X86 BBS 32-15-24.62.32 V.34/V.FC (24h/24h) (2:292/516) --------------- FIDO MESSAGE AREA==> TOPIC: 239 C LANGUAGE Ref: F3K00023 Date: 03/15/98 From: JOHN SPENCE Time: 10:03pm \/To: ALL (Read 2 times) Subj: pics HI, I was wondering is there a way to display a gif/jpg in a C program? John Spence spendel@escape.ca http://www.escape.ca/~spendel/dragon/dragons.htm --- GEcho 1.00 * Origin: -= The Dragon's Lair =-(204)269-2164...Wpg.Man (1:348/943) --------------- FIDO MESSAGE AREA==> TOPIC: 239 C LANGUAGE Ref: F3K00024 Date: 03/15/98 From: JOHN SPENCE Time: 10:05pm \/To: ALL (Read 2 times) Subj: difference Could someone elaborate on the difference on using -> instead . when using them in a struct? thanks John Spence spendel@escape.ca http://www.escape.ca/~spendel/dragon/dragons.htm --- GEcho 1.00 * Origin: -= The Dragon's Lair =-(204)269-2164...Wpg.Man (1:348/943) --------------- FIDO MESSAGE AREA==> TOPIC: 239 C LANGUAGE Ref: F3K00025 Date: 03/15/98 From: JOHN SPENCE Time: 10:21pm \/To: ALL (Read 2 times) Subj: cursor HI, I would like to be able to have the user use the arrow keys or tab key to move around on the input screen...could someone point me in the general direction...thanks John Spence spendel@escape.ca http://www.escape.ca/~spendel/dragon/dragons.htm --- GEcho 1.00 * Origin: -= The Dragon's Lair =-(204)269-2164...Wpg.Man (1:348/943) --------------- FIDO MESSAGE AREA==> TOPIC: 239 C LANGUAGE Ref: F3L00000 Date: 03/15/98 From: SIMON LAW Time: 11:27pm \/To: DARIN MCBRIDE (Read 2 times) Subj: atoi(const char *s) Darin McBride suddenly realized that he had nothing to do with Simon Law or "atoi(const char *s)." Hi Darin McBride, SL> Be warned... I am a "newbie" to C. (Please don't hurt me SL> too much...) DM> Cool... a user from my own BBS... I'll only hurt you locally. ;-> As opposed to globally huh? *chuckle* SL> char *sTemp; SL> int iTemp; SL> printf("Enter a number: "); SL> scanf("%s", sTemp); DM> Ooo.... so far, so bad. sTemp doesn't point anywhere. You need to DM> give it some memory. I suppose that was a big no-no, huh? All those dang C tutorials are USELESS I tell you USELESS. BTW, why DOES it have to point to a memory location? And why do you have to define a specific maximum length to the string? DM> #define TEMP_LEN 30 DM> char* sTemp = malloc(TEMP_LEN) * sizeof(char)); /* remember to free it DM> afterwards */ Okay. This means to allocate 30 chars with of 1 byte each, right? And I use free(sTemp) afterwards! (BTW, is there a difference between "char* sTemp" and "char *sTemp"?) DM> Next problem: scanf. If the user types in more characters than the DM> array has room for, you'll overwrite it. You could do the following: DM> scanf("%" STR(TEMP_LEN) "s", sTemp); DM> Where STR is: DM> #define _STR(x) #x DM> #define STR(x) _STR(x) DM> (A pair of defines are available in Snippets which are close to this DM> [perhaps a name change] - I should have Snippets available for DM> download, if not, bug me in private mail and I'll find it for ya.) DM> Anyway, this is getting ugly. However, there is a reason to this DM> madness, and it is a point on how to get really ugly code that works. DM> ;-) All right, you've gone and lost me here. Please explain those two previous #defines... DM> Let's try two different solutions at this point. DM> 1. Use scanf. Okay. I understood that. DM> 2. Parse everything manually Got this one too... DM> What a lot of work to do error-correction (error-protection?)! DM> Thus DM> the reason for so many generic user-entry modules... so we don't have DM> to continually do all of this... :-) Generic user-entry modules? You mean extra... dang, I don't know the C terminology... SL> Is there a way of doing this using a standard function, or SL> do I have to check for a zero (\0x30?) in sTemp if atoi returns a 0? DM> You could do this - the only reason for the longer solutions above is DM> to help you should you want more than just a single number... :-) Thank you very much, then! Scry you later! Fendolin... ... It's all fun and games till someone looses an eye, then it's a SPORT. --- * Origin: Tanktalus' Tower BBS (1:250/102) --------------- FIDO MESSAGE AREA==> TOPIC: 239 C LANGUAGE Ref: F3L00001 Date: 03/15/98 From: CAREY BLOODWORTH Time: 09:05pm \/To: JOHN DUMAS (Read 2 times) Subj: RE: AVOID DISASTER JD>Free() is just the counterpart of malloc(). JD>I know what free does but how does it do it. In C, the heap keeps additional information about any memory allocations. It plays a few tricks and is all 'behind the scenes' and you aren't supposed to know how it does it. (In fact, it can, and does, vary from compiler to compiler.) (This is different from some Pascal's that just keep a single 'high allocation mark' pointer, and you are forced to de-allocate in the reverse order you allocated.) Basically, though, when you do a malloc(10), the function malloc will actually allocate more memory than that. Internally, it will call it's own heap managment routines for a few extra bytes for its book keeping. It'll need at least a sizeof(unsigned int) for the size of memory you allocated, plus perhaps more if it keeps other information of some sort. It'll use the first few bytes for itself, and return a pointer to the memory right after its own memory data struct. For a 16 bit system, it might look somthing like this: 14 bytes allocated: Low memory address: 2 bytes: pointer to the next free block 2 bytes: length of memory allocated 10 bytes: it returns a pointer to here, for you to use. So, when you free(), it adjusts the pointer back a few bytes so its pointing at the internal memory allocation data structure, and it knows how many bytes were allocated, and so on. It does something like: struct {heapstruct *heapptr;unsigned int len;} heapstruct; void *malloc(unsigned int size) {heapstruct *h; h=InternalMalloc(size+4); h->heapptr=NextHeapAddress; /* and do other linked list or whatever stuff*/ h->len=size+4; return ((void*)h)+4; } free(void *ptr) { ptr-=4; /* back up over the heap structure */ /* the address to free, and how many bytes */ InternalFree(ptr,ptr->len); } There are many ways to do all that. That's just an example, so you can't actually use the stuff. And, as I said, it varies from compiler to compiler, so you certainly can't portably depend on any internal information. Anyway, to answer your question, it knows how because it keeps aditional information _ahead_ of the pointer it actually returns to you. And it keeps other heap data so it knows how to walk the heap looking for holes to allocate memory in, and so on. --- QScan/PCB v1.19b / 01-0162 * Origin: Jackalope Junction 501-785-5381 Ft Smith AR (1:3822/1) --------------- FIDO MESSAGE AREA==> TOPIC: 239 C LANGUAGE Ref: F3L00002 Date: 03/15/98 From: CAREY BLOODWORTH Time: 09:05pm \/To: DARIN MCBRIDE (Read 2 times) Subj: CRC model [9/12] DM>above. I was merely poking fun at an ex-moderator who has, many times, DM>expressed how off-topic his own message (almost) was. :-) DM>The only positive point in this fun-poking is that I can better understand w DM>a new person wants to write a "Thanks - it all worked great!" (and thus DM>off-topic) message... when those who have enforced the rule for years will It is rather curious that in most parts of the world, politeness and simple common courtesy (please, thank you, etc.) are an admirable trait (especially in public areas and forums) that parents 'beat' into their kids, but in Fidonet, it'll get you reprimanded and banned..... --- QScan/PCB v1.19b / 01-0162 * Origin: Jackalope Junction 501-785-5381 Ft Smith AR (1:3822/1) --------------- FIDO MESSAGE AREA==> TOPIC: 239 C LANGUAGE Ref: F3L00003 Date: 03/16/98 From: DARIN MCBRIDE Time: 08:37am \/To: SIMON LAW (Read 2 times) Subj: atoi(const char *s) SL> Be warned... I am a "newbie" to C. (Please don't hurt me SL> too much...) DM> Cool... a user from my own BBS... I'll only hurt you locally. ;-> SL> As opposed to globally huh? *chuckle* Or statically. :-) SL> char *sTemp; SL> int iTemp; SL> printf("Enter a number: "); SL> scanf("%s", sTemp); DM> Ooo.... so far, so bad. sTemp doesn't point anywhere. You need to DM> give it some memory. SL> I suppose that was a big no-no, huh? All those dang C SL> tutorials are USELESS I tell you USELESS. Yup - always use memory to store things in. :-) And only *some* tutorials are useless... most of them are marginally useful to extremely useful. :-) SL> BTW, why DOES it have to point to a memory location? And SL> why do you have to define a specific maximum length to the string? Where did you want to store the string? In memory, right? ;-) Therefore you must grab exclusive hold of that memory. Think about it for a second as this: char *sTemp; /* say it points to 8000:0000 by random fluke */ When you put something into sTemp, which points to video memory (I think), garbage appears on your screen. When you ask the runtime library to hand you memory, it will make sure you don't get memory that points to a hardware device. As for the maximum length: because you need to reserve the memory before it is used, thus you need to have a definable length to reserve. You choose whether you want 30, 80, or 512 (or more!). The tradeoff is obviously having less memory available for other tasks. DM> #define TEMP_LEN 30 DM> char* sTemp = malloc(TEMP_LEN) * sizeof(char)); /* DM> remember to free it DM> afterwards */ SL> Okay. This means to allocate 30 chars with of 1 byte each, SL> right? And I use free(sTemp) afterwards! Woops - make that "malloc(TEMP_LEN * sizeof(char))" - had an extra parenthesis there. :-) But, yes, that's what it means. SL> (BTW, is there a difference between "char* sTemp" and SL> "char *sTemp"?) To the compiler, no. I use the former simply because I like to think of my variables (even in C) as objects... in this case, sTemp is of type "char*". The way you type it says that *sTemp is a char. Both are correct, just different ways of looking at it. DM> #define _STR(x) #x DM> #define STR(x) _STR(x) SL> All right, you've gone and lost me here. Please explain SL> those two previous #defines... Those #defines are in snippets... with an explanation, I believe. :-) DM> Let's try two different solutions at this point. DM> 1. Use scanf. SL> Okay. I understood that. DM> 2. Parse everything manually SL> Got this one too... My prefered option is #2. DM> What a lot of work to do error-correction DM> (error-protection?)! DM> Thus DM> the reason for so many generic user-entry modules... so we don't have DM> to continually do all of this... :-) SL> Generic user-entry modules? You mean extra... dang, I SL> don't know the C terminology... I mean a function that you tell it what you want, it goes and gets it and validates it. For example, a function that gets a float between x and y. Or a function that gets a last name. This can be done in text mode, or in a windowing system (Windows, OS/2's PMShell, Unix's X-clones, etc.) via a text box or other input mechanism. --- * Origin: Tanktalus' Tower BBS (1:250/102)