ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Weird behaviour I should mention is that when I check for NULL pointer in node->child[index], the unassigned values are not null anymore, they point to arbitary memory.
Sorry about the non - indentation. I did the strcpy bcoz everytime I used the parameters as arguments to another function like I do here,
Code:
printNode(newchild,temp,alphabets);
it become "". I have no idea why. Somewhere I read this happens with char pointers and copying them to char arrays should make the problem go away and it did.
I have an N-ary tree. I dont know the size of the children for each node. Its depends on the alphabets variable "ABC..$". That is why I declared a flexible array. This is my first time using them. Please let me know if I am declaring them wrong.
I might be completely naive & wrong about this, but does alphabetsize get initialised anywhere? I notice you have a asize variable, which is initialised to the length of alphabets.
it's also used here without having been initialised anywhere that I could see.
Code:
//create a new internal node
if(mismatch ==1)
{
int oldsuffid = child->suff_id;
int lblbeg = child->edgelbl_beg;
int lblend = child->edgelbl_end;
intlnode = malloc(sizeof(node *)+alphabetsize);
initializeNode(intlnode,globalnodeid++,alphabets);
and here
Code:
//insert new child
newchild = malloc(sizeof(node *)+alphabetsize);
initializeNode(newchild,globalnodeid++,alphabets);
I'm guessing you're trying to implement some sort of suffix tree. My current opinion is that this will prove to be a somewhat difficult task, because you seem to be missing some fundamental knowledge regarding the C programming language.
Why are you allocating memory to hold a pointer to node (rather than a node) + memory to hold the alphabet?
3. I believe you might be using a C++ compiler in order to compile C code (don't). gcc support for C99 can be turned on via `-std=c99`. If you're using gcc, I suggest trying to compile your code using (at least) the following command line flags:
Code:
-std=c99 -pedantic -Wall -Wextra
Make sure to get rid of all the errors and warnings (and understand why they were errors and warnings in the first place).
Last edited by posixculprit; 03-21-2011 at 03:06 AM.
Better but still not good. Now the problem is with +alphabetsize. You are trying to allocate memory for a structure containing a flexible array member. The array contains pointers to nodes. A pointer to a node has the size sizeof(struct node*). You want the array to hold alphabetsize of these pointers. The total memory occupied by the flexible array would thus be:
Code:
alphabetsize * sizeof(struct node*)
and thus the whole thing should look something along these lines:
Note that in C (as opposed to C++) there is an implicit conversion from void pointers to typed pointers (so that (node *) cast is (not only wrong but also) redundant). It is wrong because in C node is not an automatic synonim for struct node as in C++.
P.S.: Indeed, that was my objection to the strlen/strcpy thing.
i and j were both set to zero at the start of main. Your loop uses j, but the subscripts inside the loop use i, so the for loop continually tests alphabets[0]. This doesn't cause a compile error, so maybe in this case it might have better not to initialise j or declare i, because the for loop will do that any way ( the j I mean), and the compiler would have flagged an error about i. I try not to use i or j because they look too similar and sometimes make this sort of thing hard to see.
Also forgive me if I am particulary dense, I still don't see alphabetsize = 0 anywhere. Even if it were initialised to 0 somehow, it will still be 0 because of the for loop.
A good example of why one should try to declare variables as close to their initial point of usage and in an as limited scope as possible:
Code:
for (size_t i = 0; i < strlen(alphabet); ++i) {
// ...
}
There are also various memory leaks, useless statements, you test for failure to open a file but you carry on as if everything is alright (sure, you write a message to stdout, but that doesn't stop the execution flow) and who know what other problems.
Chances are a close inspection of the code will yield many bugs. Is this some form of homework? If you are writing this program in order to refresh / extend your knowledge of C, might I suggest a considerably simpler project first?
@David: Thank you for pointing out the alphabetsize errors. I pasted the wrong version of the driver prog. I am so sorry for the confusion. But I havent initialized alphabetsize though, I will change that.
@Posixculprit: I will look into the bugs closer. I just have a bad habit of leaving exception handling to the end, thats why my code is so messy. This is a test code I am writing for my research on Suffix trees.
The code seems to work so far with the earlier changes. Thanks you so much. My errors have been solved. Thanks you for the explanations too.
If indeed alphabetsize hasn't been initialised or set properly, then it will contain garbage, so using it with malloc will be really bad.
Also consider using unsigned int type for variables like alphabetsize. There might be some that could be const unsigned int, and others that could be size_t. E.g. strlen returns size_t.
Finally alphabetsize is a global variable, this is ok at the moment because I think you have all your code in 1 file. If you were to have main in it's own file then this would fail. It would be better anyway if alphabetsize was declared and initialised in main, then a pointer to it could be passed to whichever functions needed it.
Thank you David. I never thought about different data types assigning into alphabetsize. I just read about the difference between unsigned_int and size_t. I will make the changes you have suggested to alphabetsize.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.