473,320 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Unique number sequencer problem

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct {int key; int digit; int string[2];}person;
  6. //int pass(man a[]);
  7.  
  8.  
  9. int main() {
  10.  
  11. int apo;
  12. int x;
  13.  
  14.  
  15. int digit_length = 3;
  16.  
  17. double max = 4;
  18.  
  19. FILE *fp;
  20.  
  21. int store = 1000000;
  22.  
  23. person *txPtr;
  24. if( (txPtr = calloc( store, sizeof(person) )) == NULL) {
  25.  
  26. printf("txPtr:Insufficient memory\n");
  27. abort();
  28.  
  29. } else {
  30. printf("txPtr:Sufficient Memory\n");
  31. memset(txPtr,0,sizeof(txPtr));
  32. }
  33. person *rxPtr;
  34. if( (rxPtr = calloc( store, sizeof(person) )) == NULL) {
  35.  
  36. printf("rxPtr:Insufficient memory\n");
  37. abort();
  38.  
  39. } else {
  40. printf("rxPtr:Sufficient Memory\n");
  41. memset(rxPtr,0,sizeof(rxPtr));
  42. }
  43.  
  44.  
  45.     int y;
  46.     int ao = 0;
  47.  
  48.     int key = 1;
  49.     char name[24];
  50.     int digit;
  51.     int z;
  52.  
  53.         char start[24];
  54.         int ad = 0;
  55.         int outputl = 0;
  56.         char buffer[24];
  57. int xyz;
  58. for(x = 0; x < max; x++) {
  59.  txPtr[x].key = x+1;
  60.  txPtr[x].digit = 1;
  61.  for(xyz = 0; xyz < digit_length; xyz++){
  62.   txPtr[x].string[xyz] = 0;
  63.  }
  64.  txPtr[x].string[0] = x+1;
  65. }
  66.  
  67.  
  68.  
  69.  
  70.  
  71. if (( fp = fopen( "unique2.db", "wt" )) == NULL){
  72.     perror( "Error opening data file\n" );
  73.      return -1;
  74. }
  75.  
  76. int digital = 0;
  77. int digitstore[digit_length];
  78. int zyx;
  79.  
  80.  
  81. for(x = key; x < max; x++) {
  82.     printf("\nOne\n");
  83.     y = 0;
  84.         key = txPtr[y].key;
  85.         digit = txPtr[y].digit;
  86.         memset(rxPtr,0,sizeof(rxPtr));
  87.         for(xyz = 0; xyz < digit_length; xyz++) {
  88.           digitstore[xyz] = txPtr[y].string[xyz];
  89.          }
  90.  
  91.         while(!key == 0) {
  92.                 digit = digit + 1;
  93.         for( z = key; z < max; z++ ) {
  94.             ad = z+1;
  95.             rxPtr[ao].key = ad;
  96.             rxPtr[ao].digit = digit;
  97.  
  98.             for(xyz = 0; xyz < digit_length; xyz++) {
  99.             if (xyz == digit - 1) {
  100.                 rxPtr[ao].string[xyz] = ad;
  101.             } else {
  102.                 rxPtr[ao].string[xyz] = digitstore[xyz];
  103.             }
  104.             }
  105.  
  106.  
  107.  
  108.  
  109.             for(xyz = digit; xyz < digit_length; xyz++){
  110.                 for(zyx = xyz; zyx < digit_length; zyx++) {
  111.                 printf("%d",rxPtr[ao].string[xyz]);
  112.             }
  113.             }
  114.             printf(" Key:%d, z: %d, Digit: %d,String:%d %d %d\n",ad,z,digit,rxPtr[ao].string[0],rxPtr[ao].string[1],rxPtr[ao].string[2]);
  115.             if(digit == digit_length) {
  116.             fwrite(rxPtr[ao].string, sizeof(rxPtr[ao].string),1,fp);
  117.             }
  118.                         ao++;
  119.             if(digit > digit_length) {
  120.             printf("\nExceeded digit limit.\n");
  121.             printf("\nFreeing Memory\n\n");
  122.             free(txPtr);
  123.             free(rxPtr);
  124.             abort();
  125.             }
  126.         }
  127.         y++;
  128.         key = txPtr[y].key;
  129.         digit = txPtr[y].digit;
  130.  
  131.         for(xyz = 0; xyz < digit_length; xyz++) {
  132.           digitstore[xyz] = txPtr[y].string[xyz];
  133.           }
  134.  
  135. }
  136.  printf("Mem cop");
  137.     memset(txPtr, 0, sizeof(txPtr));
  138.     memcpy(txPtr, rxPtr, store);
  139.     memset(rxPtr, 0, sizeof(rxPtr));
  140.     ao = 0;
  141. }
  142.  
  143. free(txPtr);
  144. free(rxPtr);
  145.  
  146. if ( fclose(fp) != 0)
  147. {
  148.     fprintf(stderr, "Error closing file.\n");
  149.     return -2;
  150. }
  151. return 0;
  152. }
  153.  
Feb 20 '08 #1
2 1359
Hi,

I have writtten this C script to produce a series of numbers. However, it seems there is an overflow somewhere and a storage buffer is being filled when it should not be filled.

The output is controlled by the variables DIGIT_LENGTH and MAX

Where DIGIT_LENGTH is the number of digits in a sequence and MAX is how high to count these digits in a sequence.

For example, DIGIT_LENGTH = 3;
and MAX = 3;

This outputs
12
13
23
123

But in this version of the program there are trailing numbers in the sequence...

122
132
233
123

Something like that -

Where are the rxPtr[ao].string[2] getting filled on or before the first pass?
Feb 20 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
Have you stepped though this with your debugger??
Feb 20 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Tony Clarke | last post by:
Hi, I'm trying to write a system thats used for about 50 clients that uses html forms and php to log details. The problem is that when a client loads the form page it's given a value which is...
0
by: Put 030516 in email subj to get thru | last post by:
I'm trying to learn about Java MIDI programming. Specifically, I'm interested in playing with Java MIDI sequencers (all puns intended). I'm looking at...
8
by: Joe Wong | last post by:
Hi, I need to implement a unique number generator that 1 or more processes on same or different machines will make use of it. Is there any library / project available already for this? Thanks...
26
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.)...
5
by: UJ | last post by:
Is there any number I can get that is truly unique for a computer that can't be changed? I want to have a system whereby I have a computer that accesses a web service based on some unique value...
5
by: Stanley Sinclair | last post by:
I have a medical records database. At this moment one server. Providence willing, and if things go well for me, I may have 9 other servers join in. Each patient will have, by GENERATE_UNIQUE(),...
2
by: D. Dante Lorenso | last post by:
I'm trying to build a table that will store a history of records by enumerating the records. I want the newest record to always be number ZERO, so I created a trigger on my table to handle the...
2
by: pstachy | last post by:
Hi again! I have another issue. I would like the attribute of the tag <invoice> to be unique. Made the following schema but unfortunately it doesn't validate. Could someone please indicate what is...
8
by: Marc | last post by:
Hi all, I have to generate and send to a printer many 6 digit alphanumeric strings. they have to be unique but I cannot check in a database or something like that if it have already been printed....
1
by: dez5000 | last post by:
I'm trying to get a report by location that would list the number of visits to the location for the month but also count the number of unique visitors to that location. I have a table of data with...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.