•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 401,670 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,532 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 3082 | Replies: 13 | Solved
![]() |
•
•
Join Date: Sep 2004
Posts: 22
Reputation:
Rep Power: 4
Solved Threads: 0
Hello Everyone,
I'm a problem with a recursive C function. Before I explain the functionality of the code, I'd like to give a brief explination of what I'm trying to do. I'm working on a general tree data type which represents entities, each of which has a list -array- of arguments. What I'm trying to do is to traverse the tree in a recursive manner and retrieve and populated a new array with the entities and their arguments. The problem is as follows:
As I retrieve the information and print it right away I have no problem, but when I attempt to print the new array contents (using print key function) after the recursive function (Traverse_sem) exists, I not only get a runtime error, but the little cotents printed are wrong.
So I'm guessing either the array index isn't being updated as I populate the new data type; meaning it stores, prints and overwrites, thus the correct printout during population process, OR the array index is being updated but something related with the datatype definition is wrong causing this problem. I hope that I have explained the problem in a clear manner, I'd appreicate all the help I can get.
Thanks in Advance,
Layla.
I'm a problem with a recursive C function. Before I explain the functionality of the code, I'd like to give a brief explination of what I'm trying to do. I'm working on a general tree data type which represents entities, each of which has a list -array- of arguments. What I'm trying to do is to traverse the tree in a recursive manner and retrieve and populated a new array with the entities and their arguments. The problem is as follows:
As I retrieve the information and print it right away I have no problem, but when I attempt to print the new array contents (using print key function) after the recursive function (Traverse_sem) exists, I not only get a runtime error, but the little cotents printed are wrong.
So I'm guessing either the array index isn't being updated as I populate the new data type; meaning it stores, prints and overwrites, thus the correct printout during population process, OR the array index is being updated but something related with the datatype definition is wrong causing this problem. I hope that I have explained the problem in a clear manner, I'd appreicate all the help I can get.
Thanks in Advance,
Layla.
/ Defining Principal known keys table
static struct ky {
char *ID1;
char *ID2;
int Inv;
};
typedef struct ky Keys;
struct prnc {
char *ID;
int nk;
Keys kys[0];
};
static struct prnc principal_know_key[0];
//********************************************************
// Traverse the tree to check a number of constraints
//********************************************************
void traverse_sem(Ast a){
Ast *t;
int i;
int n;
int sflag;
int rflag;
char *message_sr_id;
char *message_rc_id;
switch(NKIND(a))
{
case PROTOCOL:
t = PROTOCOL_PRINCIPALS(a);
num_principals = PROTOCOL_NB_PRINCIPALS(a);
fprintf(outS,"\tNumber: %d\n", num_principals);
// Traverse Principals
for (i=0;i<num_principals;i++)
traverse_sem(t[i]);
// Ensure that at least two principals are present
fprintf(outS,"\tViolations: ");
if (principal_counter < 2 )
printf("Yes \n\t\t Less than two principals are declared.\n\n");
else
if (num_principals > principal_counter)
fprintf(outS,"\nExcessive declaration of principals");
else
fprintf(outS," No \n\n");
// Traverse Messages
fprintf(outS,"Messages:\n");
num_messages = PROTOCOL_NB_MESSAGES(a);
fprintf(outS,"\tNumber: %d\n", num_messages);
t = PROTOCOL_MESSAGES(a);
for (i=0;i<num_messages;i++)
traverse_sem(t[i]);
break;
case PRINCIPAL:
principals_list[principal_counter]= PRINCIPAL_ID(a);
principal_know_key[principal_counter].ID = PRINCIPAL_ID(a);
printf("\n Principal Name: %s", principal_know_key[principal_counter].ID); //delete here
fprintf(outS,"\tPrincipal %d : %s \n", principal_counter+1, principals_list[principal_counter]);
t = PRINCIPAL_KNOW(a);
n = PRINCIPAL_NB_KNOWS(a);
principal_know_key[principal_counter].nk= n;
for (i=0;i<n;i++)
traverse_sem(t[i]);
key_counter =0;
++principal_counter;
break;
case KEY:
printf("\n\t Key # %d", key_counter+1);
principal_know_key[principal_counter].kys[key_counter].ID1 = KEY_ID1(a);
printf("\n\t\t\t ID1 : %s", principal_know_key[principal_counter].kys[key_counter].ID1);
principal_know_key[principal_counter].kys[key_counter].ID2 = KEY_ID2(a);
printf("\n\t\t\t ID2 : %s", principal_know_key[principal_counter].kys[key_counter].ID2);
principal_know_key[principal_counter].kys[key_counter].Inv = KEY_INV(a);
printf("\n\t\t\t Inv : %d", principal_know_key[principal_counter].kys[key_counter].Inv);
++key_counter;
break;
case MESSAGE:
t = MESSAGE_SENT_DATA(a);
n = MESSAGE_NB_SENT_DATA(a);
++message_counter;
fprintf(outS,"\n\tMessage %d\n", message_counter);
message_sr_id = MESSAGE_SENDER_ID(a);
fprintf(outS,"\t\tSender: %s\n", message_sr_id);
message_rc_id = MESSAGE_RECEIVER_ID(a);
fprintf(outS,"\t\tReceiver: %s\n",message_rc_id);
fprintf(outS,"\t\tViolations: ");
//Check for sender validity
sflag = PrincipalPresense("Sender", message_sr_id,0);
//Check for reciever validity
rflag = PrincipalPresense("Receiver", message_rc_id,sflag);
if ((sflag ==0) && (rflag == 0))
fprintf(outS," No \n\n");
for (i=0;i<n;i++){
traverse_sem(t[i]);
}
break;
}
}
/ A test function to display the contents of the array principal_know_key
void printkey(){
int i;
int j;
int numkeys;
for(i =0; i < principal_counter ; i++)
{
printf("\n Principal Name: %s", principal_know_key[i].ID);
numkeys = principal_know_key[i].nk;
printf("\n\t Number of known keys: %d", numkeys);
for (j=0; j < numkeys ; j++)
{
printf("\n\t\t Key # %d: ", j+1);
printf("\n\t\t\t ID1 : %s", principal_know_key[i].kys[j].ID1);
printf("\n\t\t\t ID2 : %s", principal_know_key[i].kys[j].ID2);
printf("\n\t\t\t Inv : %d", principal_know_key[i].kys[j].Inv);
}
}
} Do you have a small, but complete, example? I cannot compile the code you have posted, and this makes offering assistance much more difficult.
•
•
Join Date: Sep 2004
Posts: 22
Reputation:
Rep Power: 4
Solved Threads: 0
Dave,
I appreciate your attempt to help, unfrotunately the entire code is scattered over 10 files! therefore I had to post the segments of code related to the problem. So my question is as follows,
1- from your experience, do you know of a reason causing a data structure to store data but then lose it?! cause when I print the cotents as I store the data things are OK, but when I attempt to print the contents of the array after the recursive function exists, I'm getting the run time error + it seems the array doesn't hold the data any longer.
2- My 2nd guess is that the index of the array isn't being updated, meaning as I'm storing and printing correctly, the next item of data overwrites the previous one as the index does not increase, again I can't see why this is happening, the logic seems correct to me.
3- Would you by any chance know how to use the debugging feature in the Dev - C++ compiler? I couldn't figure it out and I think if I use it i'd spot the source of error.
Again Dave thanks alot for your help.
Have a good one,
Layla.
I appreciate your attempt to help, unfrotunately the entire code is scattered over 10 files! therefore I had to post the segments of code related to the problem. So my question is as follows,
1- from your experience, do you know of a reason causing a data structure to store data but then lose it?! cause when I print the cotents as I store the data things are OK, but when I attempt to print the contents of the array after the recursive function exists, I'm getting the run time error + it seems the array doesn't hold the data any longer.
2- My 2nd guess is that the index of the array isn't being updated, meaning as I'm storing and printing correctly, the next item of data overwrites the previous one as the index does not increase, again I can't see why this is happening, the logic seems correct to me.
case PRINCIPAL:
principals_list[principal_counter]= PRINCIPAL_ID(a);
principal_know_key[principal_counter].ID = PRINCIPAL_ID(a);
printf("\n Principal Name: %s", principal_know_key[principal_counter].ID); //delete here
fprintf(outS,"\tPrincipal %d : %s \n", principal_counter+1, principals_list[principal_counter]);
t = PRINCIPAL_KNOW(a);
n = PRINCIPAL_NB_KNOWS(a);
principal_know_key[principal_counter].nk= n;
for (i=0;i<n;i++)
traverse_sem(t[i]);
key_counter =0;
++principal_counter;
break;
case KEY:
printf("\n\t Key # %d", key_counter+1);
principal_know_key[principal_counter].kys[key_counter].ID1 = KEY_ID1(a);
printf("\n\t\t\t ID1 : %s", principal_know_key[principal_counter].kys[key_counter].ID1);
principal_know_key[principal_counter].kys[key_counter].ID2 = KEY_ID2(a);
printf("\n\t\t\t ID2 : %s", principal_know_key[principal_counter].kys[key_counter].ID2);
principal_know_key[principal_counter].kys[key_counter].Inv = KEY_INV(a);
printf("\n\t\t\t Inv : %d", principal_know_key[principal_counter].kys[key_counter].Inv);
++key_counter;
break;3- Would you by any chance know how to use the debugging feature in the Dev - C++ compiler? I couldn't figure it out and I think if I use it i'd spot the source of error.
Again Dave thanks alot for your help.
Have a good one,
Layla.
•
•
•
•
Originally Posted by Layla_2401
unfrotunately the entire code is scattered over 10 files! therefore I had to post the segments of code related to the problem.
•
•
•
•
Originally Posted by Layla_2401
1- from your experience, do you know of a reason causing a data structure to store data but then lose it?!
•
•
•
•
Originally Posted by Layla_2401
I'm getting the run time error + it seems the array doesn't hold the data any longer.
•
•
•
•
Originally Posted by Layla_2401
2- My 2nd guess is that the index of the array isn't being updated, meaning as I'm storing and printing correctly, the next item of data overwrites the previous one as the index does not increase, again I can't see why this is happening, the logic seems correct to me.
Crank up the error/warning level, i.e., -Wall -ansi -pedantic, to see if that can help pick out other things; data types and wrong format specifiers may do bad things too.
•
•
•
•
Originally Posted by Layla_2401
3- Would you by any chance know how to use the debugging feature in the Dev - C++ compiler? I couldn't figure it out and I think if I use it i'd spot the source of error.
•
•
Join Date: Sep 2004
Posts: 22
Reputation:
Rep Power: 4
Solved Threads: 0
Dave,
Thanks for your prompt comment, I really appreciate it.
Actually I don't think that would help, as I'm sure that the problem is related to array population process, hence I know what segment of code needs to be debugged.
Would you know how to tackle such problem? I have declared the array and the two index variables (key_counter and principal_counter) as static variables but that doesn't seem to make much of a difference.
Again Dave thank you so much for your help, its highly appreciated.
Layla.
Thanks for your prompt comment, I really appreciate it.
•
•
•
•
With larger sets of code, either try cutting out what is known to be working -- using #if 0 ... #endif, for example; or try recreating a new side project and begin adding in the desired (but buggy) code until it breaks.
Actually I don't think that would help, as I'm sure that the problem is related to array population process, hence I know what segment of code needs to be debugged.
•
•
•
•
Quote:
Originally Posted by Layla_2401
1- from your experience, do you know of a reason causing a data structure to store data but then lose it?!
Yes, I've seen such things. It seems especially that I've seen such behavior when using recursion.
Would you know how to tackle such problem? I have declared the array and the two index variables (key_counter and principal_counter) as static variables but that doesn't seem to make much of a difference.
Again Dave thank you so much for your help, its highly appreciated.
Layla.
static struct prnc principal_know_key[0];•
•
Join Date: Sep 2004
Posts: 22
Reputation:
Rep Power: 4
Solved Threads: 0
Actually no its not a typo, assuming you're referring to the [0] part. I declared it this way cause the number of principals declared varies from one run to another. I didn't think this would cause a problem since I have another array in the program defined as:
and this declaration worked just fine even as it is populated using a recursive function.
So, last night I've tried something like this:
and addressed this data structure in the program as follows:
... etc
Though I was able to compile, I instantly got a run-time error and the program gave no results what so ever, unlike the previous run-time error which occured only when attempting to print.
So, what do you think?
As always Dave your help is deeply appreciated, thanks.
static char *principals[0];
and this declaration worked just fine even as it is populated using a recursive function.
So, last night I've tried something like this:
static struct prnc *principal_know_key[0];
and addressed this data structure in the program as follows:
(*principal_know_key).ID
Though I was able to compile, I instantly got a run-time error and the program gave no results what so ever, unlike the previous run-time error which occured only when attempting to print.
So, what do you think?
As always Dave your help is deeply appreciated, thanks.
•
•
•
•
Originally Posted by Layla_2401
Actually no its not a typo, assuming you're referring to the [0] part.
Though I was able to compile, I instantly got a run-time error and the program gave no results what so ever, unlike the previous run-time error which occured only when attempting to print.
So, what do you think?
If your intent is to declare a pointer such that you can allocate memory for a number of items determined at runtime, I think that is what you should do. Perhaps include the memory allocation code, and certainly keep track of the indexing: i.e., an array of 10 may be indexed from 0-9 only.
•
•
Join Date: Sep 2004
Posts: 22
Reputation:
Rep Power: 4
Solved Threads: 0
Yes you're right, its defined that way cause the number of array elements is only known at run-time, BUT! I'm not allocating array space explicitly, I'm simply increasing the index as shown in the code above.
I've even tried something else like declaring the array as:
static struct prnc principal_know_key[5];
but I still got the same error. NOTHING seems to be working!
I've even tried something else like declaring the array as:
static struct prnc principal_know_key[5];
but I still got the same error. NOTHING seems to be working!
•
•
•
•
Originally Posted by Layla_2401
BUT! I'm not allocating array space explicitly, I'm simply increasing the index as shown in the code above.
•
•
•
•
Originally Posted by Layla_2401
do you know of a reason causing a data structure to store data but then lose it?!
The bit about recursion makes me think you may be writing to some "stack" space and printing it; then "losing" this memory when the "stack" is freed up on the recursion.
Again, it's difficult to debug the fragment like this. Another thing that may need closer scrutiny is this.
void traverse_sem(Ast a){
Ast *t;
// ...
switch(NKIND(a))
{
case PROTOCOL:
t = PROTOCOL_PRINCIPALS(a);
// ...
for (i=0;i<num_principals;i++)
traverse_sem(t[i]);
// t may be different here from what is expected#include <stdio.h>
#include <string.h>
char *recurse(char *response)
{
printf("DEBUG1: response = \"%s\"\n", response);
if (!*response)
{
char buffer[10];
response = buffer;
strcpy(response, "Hello");
}
else
{
recurse(response + 1);
}
printf("DEBUG2: response = \"%s\"\n", response);
return response;
}
int main(void)
{
char text[10] = "Original";
puts(text);
puts(recurse(text));
return 0;
}
/* my output
Original
DEBUG1: response = "Original"
DEBUG1: response = "riginal"
DEBUG1: response = "iginal"
DEBUG1: response = "ginal"
DEBUG1: response = "inal"
DEBUG1: response = "nal"
DEBUG1: response = "al"
DEBUG1: response = "l"
DEBUG1: response = ""
DEBUG2: response = "Hello"
DEBUG2: response = "l"
DEBUG2: response = "al"
DEBUG2: response = "nal"
DEBUG2: response = "inal"
DEBUG2: response = "ginal"
DEBUG2: response = "iginal"
DEBUG2: response = "riginal"
DEBUG2: response = "Original"
Original
*/ Last edited by Dave Sinkula : Mar 13th, 2005 at 5:55 pm. Reason: Added canned example.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Run-Time Error 424 (Visual Basic 4 / 5 / 6)
- Run-time error '5' (Windows NT / 2000 / XP / 2003)
- "Run time error, do you wish to debug?" (Web Browsers)
- Run-Time Error..? (Windows NT / 2000 / XP / 2003)
Other Threads in the C Forum
- Previous Thread: WHo can explain this code to me??
- Next Thread: Help with program that draws two rectangles and two diagonals



Linear Mode