944,111 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 5257
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 8th, 2005
0

Run-time Error when printing Array Contents.

Expand Post »
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.


  1. / Defining Principal known keys table
  2.  
  3. static struct ky {
  4. char *ID1;
  5. char *ID2;
  6. int Inv;
  7. };
  8.  
  9. typedef struct ky Keys;
  10.  
  11. struct prnc {
  12. char *ID;
  13. int nk;
  14. Keys kys[0];
  15. };
  16.  
  17. static struct prnc principal_know_key[0];
  18.  
  19.  
  20. //********************************************************
  21. // Traverse the tree to check a number of constraints
  22. //********************************************************
  23.  
  24. void traverse_sem(Ast a){
  25. Ast *t;
  26. int i;
  27. int n;
  28. int sflag;
  29. int rflag;
  30. char *message_sr_id;
  31. char *message_rc_id;
  32.  
  33.  
  34. switch(NKIND(a))
  35. {
  36. case PROTOCOL:
  37. t = PROTOCOL_PRINCIPALS(a);
  38. num_principals = PROTOCOL_NB_PRINCIPALS(a);
  39. fprintf(outS,"\tNumber: %d\n", num_principals);
  40. // Traverse Principals
  41. for (i=0;i<num_principals;i++)
  42. traverse_sem(t[i]);
  43. // Ensure that at least two principals are present
  44. fprintf(outS,"\tViolations: ");
  45. if (principal_counter < 2 )
  46. printf("Yes \n\t\t Less than two principals are declared.\n\n");
  47. else
  48. if (num_principals > principal_counter)
  49. fprintf(outS,"\nExcessive declaration of principals");
  50. else
  51. fprintf(outS," No \n\n");
  52.  
  53. // Traverse Messages
  54. fprintf(outS,"Messages:\n");
  55. num_messages = PROTOCOL_NB_MESSAGES(a);
  56. fprintf(outS,"\tNumber: %d\n", num_messages);
  57. t = PROTOCOL_MESSAGES(a);
  58. for (i=0;i<num_messages;i++)
  59. traverse_sem(t[i]);
  60.  
  61.  
  62. break;
  63.  
  64. case PRINCIPAL:
  65. principals_list[principal_counter]= PRINCIPAL_ID(a);
  66. principal_know_key[principal_counter].ID = PRINCIPAL_ID(a);
  67. printf("\n Principal Name: %s", principal_know_key[principal_counter].ID); //delete here
  68. fprintf(outS,"\tPrincipal %d : %s \n", principal_counter+1, principals_list[principal_counter]);
  69. t = PRINCIPAL_KNOW(a);
  70. n = PRINCIPAL_NB_KNOWS(a);
  71. principal_know_key[principal_counter].nk= n;
  72. for (i=0;i<n;i++)
  73. traverse_sem(t[i]);
  74. key_counter =0;
  75. ++principal_counter;
  76. break;
  77.  
  78. case KEY:
  79. printf("\n\t Key # %d", key_counter+1);
  80. principal_know_key[principal_counter].kys[key_counter].ID1 = KEY_ID1(a);
  81. printf("\n\t\t\t ID1 : %s", principal_know_key[principal_counter].kys[key_counter].ID1);
  82. principal_know_key[principal_counter].kys[key_counter].ID2 = KEY_ID2(a);
  83. printf("\n\t\t\t ID2 : %s", principal_know_key[principal_counter].kys[key_counter].ID2);
  84. principal_know_key[principal_counter].kys[key_counter].Inv = KEY_INV(a);
  85. printf("\n\t\t\t Inv : %d", principal_know_key[principal_counter].kys[key_counter].Inv);
  86. ++key_counter;
  87. break;
  88.  
  89.  
  90. case MESSAGE:
  91. t = MESSAGE_SENT_DATA(a);
  92. n = MESSAGE_NB_SENT_DATA(a);
  93. ++message_counter;
  94. fprintf(outS,"\n\tMessage %d\n", message_counter);
  95.  
  96. message_sr_id = MESSAGE_SENDER_ID(a);
  97. fprintf(outS,"\t\tSender: %s\n", message_sr_id);
  98. message_rc_id = MESSAGE_RECEIVER_ID(a);
  99. fprintf(outS,"\t\tReceiver: %s\n",message_rc_id);
  100.  
  101. fprintf(outS,"\t\tViolations: ");
  102.  
  103. //Check for sender validity
  104. sflag = PrincipalPresense("Sender", message_sr_id,0);
  105.  
  106. //Check for reciever validity
  107. rflag = PrincipalPresense("Receiver", message_rc_id,sflag);
  108.  
  109. if ((sflag ==0) && (rflag == 0))
  110. fprintf(outS," No \n\n");
  111.  
  112. for (i=0;i<n;i++){
  113. traverse_sem(t[i]);
  114. }
  115.  
  116. break;
  117.  
  118. }
  119. }
  120.  
  121.  
  122. / A test function to display the contents of the array principal_know_key
  123.  
  124. void printkey(){
  125. int i;
  126. int j;
  127. int numkeys;
  128.  
  129. for(i =0; i < principal_counter ; i++)
  130. {
  131. printf("\n Principal Name: %s", principal_know_key[i].ID);
  132. numkeys = principal_know_key[i].nk;
  133. printf("\n\t Number of known keys: %d", numkeys);
  134.  
  135. for (j=0; j < numkeys ; j++)
  136. {
  137. printf("\n\t\t Key # %d: ", j+1);
  138. printf("\n\t\t\t ID1 : %s", principal_know_key[i].kys[j].ID1);
  139. printf("\n\t\t\t ID2 : %s", principal_know_key[i].kys[j].ID2);
  140. printf("\n\t\t\t Inv : %d", principal_know_key[i].kys[j].Inv);
  141. }
  142. }
  143. }
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Layla_2401 is offline Offline
22 posts
since Sep 2004
Mar 9th, 2005
0

Re: Run-time Error when printing Array Contents.

Do you have a small, but complete, example? I cannot compile the code you have posted, and this makes offering assistance much more difficult.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 10th, 2005
0

Re: Run-time Error when printing Array Contents.

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.
  1. case PRINCIPAL:
  2. principals_list[principal_counter]= PRINCIPAL_ID(a);
  3. principal_know_key[principal_counter].ID = PRINCIPAL_ID(a);
  4. printf("\n Principal Name: %s", principal_know_key[principal_counter].ID); //delete here
  5. fprintf(outS,"\tPrincipal %d : %s \n", principal_counter+1, principals_list[principal_counter]);
  6. t = PRINCIPAL_KNOW(a);
  7. n = PRINCIPAL_NB_KNOWS(a);
  8. principal_know_key[principal_counter].nk= n;
  9. for (i=0;i<n;i++)
  10. traverse_sem(t[i]);
  11. key_counter =0;
  12. ++principal_counter;
  13. break;
  14.  
  15. case KEY:
  16. printf("\n\t Key # %d", key_counter+1);
  17. principal_know_key[principal_counter].kys[key_counter].ID1 = KEY_ID1(a);
  18. printf("\n\t\t\t ID1 : %s", principal_know_key[principal_counter].kys[key_counter].ID1);
  19. principal_know_key[principal_counter].kys[key_counter].ID2 = KEY_ID2(a);
  20. printf("\n\t\t\t ID2 : %s", principal_know_key[principal_counter].kys[key_counter].ID2);
  21. principal_know_key[principal_counter].kys[key_counter].Inv = KEY_INV(a);
  22. printf("\n\t\t\t Inv : %d", principal_know_key[principal_counter].kys[key_counter].Inv);
  23. ++key_counter;
  24. 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.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Layla_2401 is offline Offline
22 posts
since Sep 2004
Mar 10th, 2005
0

Re: Run-time Error when printing Array Contents.

Quote 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.
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.

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.

Quote originally posted by Layla_2401 ...
I'm getting the run time error + it seems the array doesn't hold the data any longer.
For a runtime error, I'd suspect wandering off the array bounds with or without the help of a pointer.

Quote 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.
I like to use tools such as lint to help with a static analysis of the code. Unfortunately, it won't do much on piecepart fragments.

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.

Quote 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.
I've been meaning to, but haven't gone there yet.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 11th, 2005
0

Re: Run-time Error when printing Array Contents.

Dave,
Thanks for your prompt comment, I really appreciate it.

Quote ...
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 ...
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.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Layla_2401 is offline Offline
22 posts
since Sep 2004
Mar 11th, 2005
0

Re: Run-time Error when printing Array Contents.

static struct prnc principal_know_key[0];
Was this a typo? Or could you expand on your intentions here a little more?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 12th, 2005
0

Re: Run-time Error when printing Array Contents.

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:

  1. 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:

  1. static struct prnc *principal_know_key[0];

and addressed this data structure in the program as follows:

  1. (*principal_know_key).ID
... 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.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Layla_2401 is offline Offline
22 posts
since Sep 2004
Mar 12th, 2005
0

Re: Run-time Error when printing Array Contents.

Quote 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?
I don't think it should compile. I get syntax errors in BC5.5 as both C and C++, and lint does not care for it. So if it does compile, you may be using some nonstandard extension.

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 13th, 2005
0

Re: Run-time Error when printing Array Contents.

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!
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Layla_2401 is offline Offline
22 posts
since Sep 2004
Mar 13th, 2005
0

Re: Run-time Error when printing Array Contents.

Quote 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.
Ah, going back to this, I think I may see something.
Quote originally posted by Layla_2401 ...
do you know of a reason causing a data structure to store data but then lose it?!
If you are not properly allocating memory, you may be getting "lucky" when you are printing it immediately. That is, you may be writing to memory you don't own, but there is no immediate crash.

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
[edit]The following is an attempt to demonstrate something or other.
#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 6:55 pm. Reason: Added canned example.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: WHo can explain this code to me??
Next Thread in C Forum Timeline: Help with program that draws two rectangles and two diagonals





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC