Thread: Definite Answer Needed

  1. #16
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    If you think that C++ is for lazy people, then try creating a linked list ADT that can support ALL data types. And I mean, ALL. (No compiler directives allowed, only language itself).
    That's easy, just use void* for the data so you linked list looks like

    Code:
    typedef struct Link {
          void *data;
          struct Link* next;
    } Link;
    I thought structures and functions were abstractions?

  2. #17
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Here's an example

    Code:
    typedef struct Node {
        void* data;
        struct Node* next;
        struct Node* prev;
    } Node;
    
    int list_insert(Node** header_ptr, void* data);
    void list_destroy(Node** header_ptr);
    Code:
    /* circle.h */
    #include <stdlib.h>
    #include <stdio.h>
    #include "circle.h"
    
    
    int list_insert(Node** header_ptr, void* data)
    {
        Node* header;
        Node* n;
        
        header = *header_ptr;
        n  = malloc(sizeof (Node));
        if (n == NULL)
            return 0;
        n->data = data;
        
        if (header == 0) {
            /* insertion into a zero node circular list */
            n->next = n;
            n->prev = n;
            *header_ptr = n;
        } else if (header == header->next) {
            /* insertion into a one node circular list */
            n->next = header;
            n->prev = header;
            header->next = n;
            header->prev = n;
        } else {
            /* insertion into a >= 2 node circular list */
            n->next = header->next;
            n->prev = header;
            header->next = n;
        }
        
        return 0;
    }
    
    void list_destroy(Node** header_ptr)
    {
        Node* curr = *header_ptr;
        Node* start = curr;
        
        if (curr != NULL) {
            do {
                Node* tmp = curr->next;
                free(curr->data);
                free(curr);
                curr = tmp;
            } while(curr != start);
        }
        *header_ptr = 0;
    }
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include "circle.h"
    
    int main(void)
    {
        Node* header = 0;
        Node* n;
        int* p;
        float* fp;
        int i;
        
        for (i = 0; i < 10; ++i)  {
             p = malloc(sizeof (int));
            *p = i;
            
            list_insert(&header, p);
        }
        
        n = header;
        for (i = 0; i < 30; ++i) {
            printf("%d ", *(int*)n->data);
            if ((i + 1) % 10 == 0)
                putchar('\n');
            n = n->next;
        }
        
        list_destroy(&header);
    
        for (i = 1; i <= 100; ++i) {
            fp = malloc(sizeof(float));
            *fp = 5.0 / i;
            list_insert(&header, fp);
        }
        
        n = header;
        for (i = 0; i < 300; ++i) {
            printf("%.3f ", *(float*)n->data);
            if ((i + 1) % 10 == 0)
                putchar('\n');
            if ((i + 1) % 100 == 0)
                putchar('\n');
            
            n = n->next;
        }
        
        list_destroy(&header);
    
        return 0;
    }

  3. #18
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    Ok I am not planning to use both cout and printf in the same program but I am just asking if it is possible to mix C and C++ code(that display thing was just an example)so thanks
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  4. #19
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    There are a few things that won't compile in c++ but will in c such as
    int* m = malloc(100);

    If you need to call functions from a c library compiled using c and you are using c++ you want to wrap the header files like

    #ifdef __cplusplus
    extern "C" {
    #endif

    /* code */

    #ifdef __cplusplus
    }
    #endif

    This is so that the linker knows to use c conventions.
    You can see an example by looking at stdio.h

  5. #20
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Post

    Shadow: Do you really even know what spaghetti code is? Shame on you. In case you don't know: if you decide to upgrade you 30,000 lines of C codes to C++, you need not have to change every single printf to cout. Doing so would be stupid. It looks to me like you still haven't realized that C is the father of C++ and that C++ has been the better son of C. They are one in blood that's why they get along very well.

    Nick: Yes void* is used to accommodate a lot of data types.

    But the catch is you can't create polymorphic objects by using void.
    Why? Simply because you can't dereference (not sure if this is the right term. i always forget) void* into void because there's no such thing (in C) as a void value. With void*, you ALWAYS have to deal with the address itself instead of concentrating mainly on the use of the object's value. But in polymorphism, you have the option to either deal with the address by referencing it (or using pointers) or you can directly manipulate the object itself.

    Such techniques as using only void* will hamper the value manipulations especially if you intend only to duplicate the value only and store it in another address and you try to free the memory used by the void* (deleting a node). Garbage will surface.

    Sure you might say that it is possible to copy the value by using memcpy() to create its twin in another address. I'm assuming you already know much of the documents about the memory mess that comes with both C and C++. As such, dealing directly with addresses can and will often result in memory leaks, which is very hard to detect and when detected, a lot more expensive (in software development terms) to solve.

    That's why C++ has the keyword 'template' to enable the creation of a 'type' that will mimic the void* at the address level plus also at the object level. It took me a lot of system crashes before I realized the template's usefulness over the void*. (f I had only immediately consulted the book I had hidden up the attic for so long, then I would not have to endure those problems hehehe. Dumb me.)

    Furthermore, C++ in theory is built on top of C. The compiler directives about is not part of the language (of course you know). You're talking about the operational capability of the compiler and not the language itself. Why create a whole new C++ compiler? You can just easily make a C++ preprocessor and connect it with the standard C compiler. This way the linker conventions won't have to be harmed in the remake.

    And lastly, structures are abstractions only in the sense that they carry the aggregated types. They need separate functions to manipulate them due an open-flow-of-data environment throughout the whole code. In other words, the abstract types created are not truly abstract because they are 'dumb' objects. The classes was built mainly to produce ADT's that can be considered 'intelligent' because they can contain methods which will be used by themselves to manipulate themselves. (Hmmm... kinda twirly.)

    Why don't you explore C++? As the increment of C, you will find out its true superiority over the pure C form itself.
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

  6. #21
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Shadow: Do you really even know what spaghetti code is? Shame on you. In case you don't know: if you decide to upgrade you 30,000 lines of C codes to C++, you need not have to change every single printf to cout. Doing so would be stupid. It looks to me like you still haven't realized that C is the father of C++ and that C++ has been the better son of C. They are one in blood that's why they get along very well.
    Spaghetti code:
    Sloppily written programming code with very poor form.

    > Do you really even know what spaghetti code is?
    Of course I do.
    I never said I didn't.

    > Shame on you.
    Your false assumption is embarassing.

    > In case you don't know:
    But I do.

    > if you decide to upgrade you 30,000 lines of C codes to C++, you need not have to change every single printf to cout.
    I wouldn't change a program that large.
    If I had to, I would rewrite the dam thing, or write a program to change the syntax for me. I would use a couple powerful and clever loops.

    Move on.
    There is no rock solid defense to your unwanted babblings.

    BTW, your spelling and grammar are terrible.
    The world is waiting. I must leave you now.

  7. #22
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Thumbs down

    >There is no rock solid defense to your unwanted babblings.

    If it wasn't a rock solid defense, then you should have been able to give me rock solid replies, not stupidity. You have not even breached through the armor of my presentations.

    >I wouldn't change a program that large.
    If I had to, I would rewrite the dam thing, or write a program to change the syntax for me. I would use a couple powerful and clever loops.

    Read your reply. It just shows to this whole board that you are a trying hard wanna-be. "Rewrite" --- is this your idea of upgrading? Looks to me like you really haven't seen the reality of software development and large project programming.
    Purists like you aren't as open-minded as you might have thought.

    >BTW, your spelling and grammar are terrible.
    Haha, why don't you view your own replies? It's a bane to the English language.


    Your supposedly "rock solid" replies are very lousy. It deserves a failing mark.
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

  8. #23
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > if you decide to upgrade you 30,000 lines of C codes to C++,
    The world is waiting. I must leave you now.

  9. #24
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Lightbulb

    That should have been "your".

    Look at yours:

    >If I had to, I would rewrite the dam thing, or write a program to change the syntax for me. I would use a couple powerful and clever loops.

    Any mistakes? Haha, I can point out two. But if I do, it might be too embarassing for you.

    PS. So you're online right now huh?
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

  10. #25
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Yes I am. I am online right now.

    Normally when I post replies to people like yourself, I don't care about my sentance and pargraph structure. I don't care about my English expression if you will. I just shoot off with whatever is on my mind, as sloppy as it comes off.

    That's all you deserve..
    The world is waiting. I must leave you now.

  11. #26
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Talking

    >as sloppy as it comes off

    Don't say this because this will someday reflect your personality.


    >I just shoot off with whatever is on my mind

    If you show your bad grammar to me, then why the hell do you even criticize the grammar of others?

    I'm not particularly strict in that because I'm not an English teacher. But I can't help it when I'm in such a hurry (I have time limit here in the internet cafe.)
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

  12. #27
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225
    Checking grammar is a waste of time especially when my time is running out.
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

  13. #28
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > Don't say this because this will someday reflect your personality.
    A very precise, nit-picking person.
    That's me.

    You misunderstood my reply. Normally, I would do my best to proof each of my posts, and detail things out clearly. As for you, I won't. For others, I won't. For others, I will. It depends on how much time you put into preparing your question / post. If you post something stupid, you'll get a stupid reply, and probably one that you'll be going around with for a hour or so defending yourself - just like what you're doing right now. I am not one to lead wild goose chases, but, there's a time for everything.

    > If you show your bad grammar to me, then why the hell do you even criticize the grammar of others?
    I don't. I did yours because through several of your posts, where you were clearly trying to help, you had poor grammar and form, not to mention spelling. That sort of "jumps out" at some people. It distracts them while reading. It also displays a lack of respect on your part.

    Most of the time, I have a spell checker, and dictionary in hand while replying. I don't now, because it's not needed..

    > But I can't help it when I'm in such a hurry
    Dont be.

    > (I have time limit here in the internet cafe.)
    My lord.

    DoneWithThread == TRUE
    The world is waiting. I must leave you now.

  14. #29
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    Hi!

    Most C-code can be compiled on C++ compilers, but there are incompatibilities, for example:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
       int class=1;
    
       return class;
    }
    This is perfectly legal C-code, but a C++ compiler will complain about parse errors. Most C++ compilers automatically determine, by looking at the filename extension, wether they should act as a C compiler (.c) or as a C++ compiler (.C, .cc, .cpp, ...).

    Hope this helps.

    greetinx,
    alex

  15. #30
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    Thanks for the reply Alex and as for shadow and bilholm, what exactly are you two fighting about?
    I lost you two somewhere after Shadow asked if bilholm knew what spagetti code is.
    And then it got into something like english grammar and such, so just calm down and try to make some sense.

    P.S. Grammer is nice but I just suck at it.
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  2. how would you answer this question?
    By smoking81 in forum Linux Programming
    Replies: 3
    Last Post: 09-08-2008, 03:53 AM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. getting weird answer when not using ".h"
    By CobraCC in forum C++ Programming
    Replies: 10
    Last Post: 05-07-2003, 06:21 AM
  5. Replies: 22
    Last Post: 11-08-2001, 11:01 PM