Thread: How to instantiate multiple objects C

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    74

    How to instantiate multiple objects C

    Hi,

    I have the following problem:

    I have a program Tree.c that reads a text file written in a particular format. From that information, it creates a tree.

    i.e. for the following text file:

    Code:
    16 17 18 0 
    17 0 0 0
    18 19 20 0
    19 0 0 0
    20 0 0 0
    It will create the following tree:



    16
    _____|_____
    17 18
    _____|_____
    19 20



    I have a function that returns me the pointer to the root of the tree: i.e. node 16

    I can invoke this function with different text files having the configurations written in them. And, I will create various trees, and get a pointer to the roots.

    Let us say,

    Code:
    // we have three files: 
    file1, file2, file3
    
    // and, we create three trees, 
    Tree root1 = create_tree(file1);
    Tree root2 = create_tree(file2);
    Tree root3 = create_tree(file3);
    The problem is the following:

    Although the tree is create, I lose control of the root node. In the caller program, only root3 is asserted to be the root. If I remove the third call, then root2 will be asserted as the root of the tree,
    i.e. the information for previous roots is overwritten.

    Is there a way in C, to make this act as a class, from which we can create objects, and each one retains its properties.

    Thanks,

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    How actually does your function "return" you a pointer to the root of the tree?

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by zahid990170 View Post
    Is there a way in C, to make this act as a class, from which we can create objects, and each one retains its properties.
    Yes, altho you don't need to do that, what you have should be fine.

    To be clear: you have a function, create_tree(), which returns a pointer to the root node of a tree created from a file. You call that three times, storing the pointer in 3 separate variables.

    No problem -- unless there is something wrong with create_tree(). Consider:

    Code:
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char *create_txt (char *txt) {
    	char *rv = malloc(strlen(txt)+1);
    	strcpy(rv,txt);
    	return rv;
    }
    
    
    int main(void) {
    	char *a = create_txt("one"),
    		*b = create_txt("two"),
    		*c = create_txt("three");
    
    	printf("%s\n%s\n%s\n", a, b, c);
    
    	free(a);
    	free(b);
    	free(c);
    
    	return 0;
    }
    These pointers retain their value and so should yours.

    Post the code for create_tree().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    74
    Hi,

    I read in the hierarchy line by line,
    The very first item in the file is the root,
    root is a global variable in the Tree.c file.
    The function keeps on reading lines, and inserting the nodes in the right places,
    Finally, when all the file is read, branches are linked,
    I get back a pointer to the root,

    Btw, I have the following structure;:

    Code:
    #define MAX_BRANCHES 4
    
    
    struct TreeNode;
    typedef struct TreeNode* T_Ptr;
    
    struct TreeNode{ 
            int val; 
    	T_Ptr myBranches[MAX_BRANCHES];
    	T_Ptr parent;
    	int num;
    };

    I guess, I will have to make a separate root variable for each file I read.

    Thanks,

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    74
    Hi,

    Thanks, your reply was helpful.

    You have given a nice similar example.
    What I understand is that you create a pointer from within the function "create_txt" that you finally return. So, it is different for different calls.
    I guess my problem comes from the fact that "root" is a global variable. And each new call can overwrite it.

    The root was made global so it becomes easy to implement different functions and refer to the root without having to pass the "root" to each of the functions.
    But, I guess, I will have to do that.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by zahid990170 View Post
    I guess my problem comes from the fact that "root" is a global variable. And each new call can overwrite it.

    The root was made global so it becomes easy to implement different functions and refer to the root without having to pass the "root" to each of the functions.
    But, I guess, I will have to do that.
    Yeah, it seems a hassle, but think about it in relation to OOP syntax: if I have an object, X, with it's own internal root variable, created:

    Code:
    obj X = new tree(filename);
    When use it, I have to do this:

    Code:
    X.somemethod();
    Sans OO, X might be a struct containing several variables, or just one, the root pointer. When I want to use it with a global function intended to work with variables of that type:

    Code:
    somefunction(X);
    Not much different, and in fact you will commonly find X referred to in C programming APIs as an object, and those global (or static) functions as constituting part of its class. I don't know whether this is borrowed from conventional OOP, or whether it historically preceeds it (ie, conventional OOP terms are derived from this sort of C abstraction).
    Last edited by MK27; 06-15-2011 at 10:28 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static member initialisation for multiple objects
    By kezman in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2011, 11:20 PM
  2. Question about multiple objects with the same name
    By shiroaisu in forum C++ Programming
    Replies: 9
    Last Post: 06-07-2010, 06:03 AM
  3. Replies: 4
    Last Post: 04-28-2008, 04:13 PM
  4. multiple (ctime) struct tm* objects
    By cjschw in forum C++ Programming
    Replies: 3
    Last Post: 10-08-2003, 01:25 PM