Results 1 to 7 of 7

Thread: Problem with a pointer ( non qt question )

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    59
    Thanked 1 Time in 1 Post

    Default Problem with a pointer ( non qt question )

    Hi to all, sorry in advance if this is not a real qt-related problem, but I have some problem with a c source code, gui is still qt :-).
    I'm writing a basic decrypting routine that take a pointer to a const message.
    so:

    Qt Code:
    1. void decryptMessage( char* text, const char* );
    2. void main()
    3. {
    4. char* message = new char[1143];
    5. strcpy_s(message, 1143, "a big encrypted message");
    6.  
    7. const char* freqLang = "TEOIARNSHLMYUCWDGPFBVKJ";
    8.  
    9. decryptMessage( message, freqLang );
    10.  
    11. delete[] message;
    12. return 0;
    13. }
    To copy to clipboard, switch view to plain text mode 
    and then some code of the decryptMessage:
    Qt Code:
    1. /* decryptMessage function */
    2. void decryptMessage( const char *message, const char *freqLang )
    3. {
    4. char freqLangCrypt[sizeof(freqLang)];
    5. int freq[TOTAL_LETTER], pos, c, max, maxj;
    6. pos = c = 0;
    7.  
    8. for (int i = 0; i < TOTAL_LETTER; i++)
    9. freq[i] = 0;
    10.  
    11. //I read the message the first time to create freq[]
    12. int index = 0;
    13. while( (c = message[index]) != '\0' )
    14. {
    15. // CODE CODE
    16. } // UNTIL NOW I CAN SEE THE POINTER TO MESSAGE
    17.  
    18. while( (c = message[index]) != '\0' ) // <--- I CAN NOT SEE THE POINTER TO MESSAGE ANYMORE
    19. {
    20. //CODE
    21. }//while
    22. // MORE CODE
    23. }
    To copy to clipboard, switch view to plain text mode 
    I don't understand why the pointer to message disappear (I don't delete it).
    Passing it to the routine it should stay visible until the routine returns.
    I hope to get help.
    Regards,
    Franco Amato

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: Problem with a pointer ( non qt question )

    My guess is that index get to the range in the first loop, and for the second loop is out of range.

    Another possible mistake is that you closed one bracket too early, so check that you didn't end the function definition before you want to.

    And share more info about how you can't see the pointer, it's an compile error, a run-time error, a syntax highlight error? If you don't provide information we can only guess.

  3. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    59
    Thanked 1 Time in 1 Post

    Default Re: Problem with a pointer ( non qt question )

    Quote Originally Posted by Zlatomir View Post
    My guess is that index get to the range in the first loop, and for the second loop is out of range.

    Another possible mistake is that you closed one bracket too early, so check that you didn't end the function definition before you want to.

    And share more info about how you can't see the pointer, it's an compile error, a run-time error, a syntax highlight error? If you don't provide information we can only guess.
    Hi I don't get compiler error. I get en error at runtime and the index has been reset to zero before the last while loop.
    I paste here the whole code ( it's a very basic decryption algorithm )


    Qt Code:
    1. /* decryptMessage function */
    2. void decryptMessage( const char *message, const char *freqLang )
    3. {
    4. const char *ptr = message;
    5. char freqLangCrypt[sizeof(freqLang)];
    6. //printf("sizeof %d", sizeof(freqLang) );
    7. int freq[TOTAL_LETTER], pos, c, max, maxj;
    8. pos = c = 0;
    9.  
    10. //memset((void*)freq, 0, TOTAL_LETTER);
    11. for (int i = 0; i < TOTAL_LETTER; i++)
    12. freq[i] = 0;
    13.  
    14. //I read the message the first time to create freq[]
    15. int index = 0;
    16. while( (c = message[index]) != '\0' )
    17. {
    18. if (isalpha(c))
    19. {
    20. c = toupper(c);
    21. pos = c - 'A';
    22. freq[pos] = freq[pos] + 1; //freq[0] = number of 'a' and 'A' in the file
    23. }
    24. index++;
    25. }
    26.  
    27. //here I create freqLangCrypt[] from freq[]
    28. //freqLangCrypt[] frequency table of the crypted message
    29. for(int i = 0; i < TOTAL_LETTER; i++)
    30. {
    31. max = 0;
    32. maxj = -1;
    33. //I search the maximum values in freq
    34. for(int j = 0; j < TOTAL_LETTER; j++)
    35. {
    36. if(freq[j] > max)
    37. {
    38. max = freq[j];
    39. maxj = j;
    40. }
    41. }//for
    42. if(maxj > -1)
    43. {
    44. freqLangCrypt[i] = maxj + 'A';
    45. freq[maxj] = 0;
    46. }
    47. }//for
    48.  
    49. //read again the message to decrypt it
    50. index = 0;
    51. char *pdest = 0;
    52. while( (c = ptr[index]) != '\0' )
    53. {
    54. if (isalpha(c))
    55. {
    56. c = toupper(c);
    57. pdest = strchr(freqLangCrypt, c);
    58. if( pdest != NULL )
    59. {
    60. index = (int)(pdest - freqLangCrypt);
    61. putchar(freqLang[index]);
    62. }
    63. else
    64. putchar(c);
    65. }//if(isalpha(c))
    66. else
    67. putchar(c);
    68. index++;
    69. }//while
    70. }//end of decryptMessage
    To copy to clipboard, switch view to plain text mode 
    and the main:

    Qt Code:
    1. #define TOTAL_LETTER 26
    2.  
    3.  
    4. void decryptMessage( char* text, const char* );
    5.  
    6. int _tmain(int argc, _TCHAR* argv[])
    7. {
    8. char* message = new char[1143];
    9. strcpy_s(message,1143, "Uid nx, aex jcdjipx iu wzux zp, ta wxtpa jtdaws, ai etkx vis.\
    10. Dcos zyexdzaxr aex Jxdw jezwipijes iu etkzyg nidx aety iyx hts\
    11. ai ri aex ptnx aezyg. Z zyexdzaxr aeta jezwipijes udin Wtdds Htww,\
    12. hei zp ns exdi tqactwws. Z htya ai ntfx Dcos cpxdp udxx. Z htya ai\
    13. gzkx aexn aex udxxrin ai qeiipx. Jxijwx tdx rzuuxdxya. Jxijwx qeiipx\
    14. rzuuxdxya qdzaxdzt. Oca zu aexdx zp t oxaaxd hts tniyg ntys\
    15. twaxdytazkxp, Z htya ai xyqicdtgx aeta hts os ntfzyg za qinuidatowx.\
    16. Pi aeta\'p heta Z\'kx adzxr ai ri.\
    17. Z htya ai piwkx jdiowxnp Z nxxa zy aex rtzws wzux os cpzyg qinjcaxdp,\
    18. pi Z yxxr ai hdzax jdigdtnp. Os cpzyg Dcos, Z htya ai qiyqxyadtax aex\
    19. aezygp Z ri, yia aex ntgzqtw dcwxp iu aex wtygctgx, wzfx patdazyg hzae\
    20. jcowzq kizr pinxaezyg pinxaezyg pinxaezyg ai pts, \"jdzya exwwi hidwr.\"\
    21. Z vcpa htya ai pts, \"jdzya aezp!\" Z riy\'a htya tww aex pcddicyrzyg\
    22. ntgzq fxshidrp. Z vcpa htya ai qiyqxyadtax iy aex atpf. Aeta\'p aex otpzq\
    23. zrxt. Pi Z etkx adzxr ai ntfx Dcos qirx qiyqzpx tyr pcqqzyqa.\
    24. Scfzezdi Ntapcniai. (hhh.tdaznt.qin/zyak/dcos)\0");
    25.  
    26. /* in the main */
    27. const char* freqLang = "TEOIARNSHLMYUCWDGPFBVKJ";
    28.  
    29. decryptMessage( message, freqLang );
    30.  
    31. delete[] message;
    32. return 0;
    33. }
    To copy to clipboard, switch view to plain text mode 
    Regards
    Franco Amato

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Problem with a pointer ( non qt question )

    What if you use "message" instead of "ptr"? What's the use of ptr anyway if it points to the same thing as message and both those pointers are const? Also check that line #60 of your snippet is correct.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    59
    Thanked 1 Time in 1 Post

    Default Re: Problem with a pointer ( non qt question )

    Quote Originally Posted by wysota View Post
    What if you use "message" instead of "ptr"? Also check that line #60 of your snippet is correct.
    Hi wysota initially I got the error using message. So I did the test copyng the pointer to ptr just for test getting exactly the same error.
    My app crash at line 52.
    while( (c = ptr[index]) != '\0' ) // changing to while( (c = message[index]) != '\0' ) I get the same problem

    Debugging I get the message 'invalid pointer' ???? I'm becoming crazy
    Franco Amato

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Problem with a pointer ( non qt question )

    Check if "index" is within a valid range!
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    59
    Thanked 1 Time in 1 Post

    Default Re: Problem with a pointer ( non qt question )

    Quote Originally Posted by wysota View Post
    Check if "index" is within a valid range!
    I correct the last loop:

    Qt Code:
    1. while( (c = message[k]) != '\0' )
    2. {
    3. if (isalpha(c))
    4. {
    5. c = toupper(c);
    6. pdest = strchr(freqLangCrypt, c);
    7. if( pdest != NULL )
    8. {
    9. int index = (int)(pdest - freqLangCrypt);
    10. putchar(freqLang[index]);
    11. }
    12. else
    13. putchar(c);
    14. }//if(isalpha(c))
    15. else
    16. putchar(c);
    17. k++;
    18. }//while
    To copy to clipboard, switch view to plain text mode 

    Now it get the right index into the frecuency vector but.
    Thanx
    Franco Amato

Similar Threads

  1. Pointer Question related to QLineEdit
    By ChrisReath in forum Qt Programming
    Replies: 1
    Last Post: 23rd May 2008, 15:13
  2. base pointer question
    By mickey in forum General Programming
    Replies: 11
    Last Post: 11th March 2008, 15:43
  3. simple question on pointer-arrays
    By mickey in forum General Programming
    Replies: 2
    Last Post: 17th February 2007, 01:11
  4. Question about pointer to class
    By kaydknight in forum General Programming
    Replies: 1
    Last Post: 17th January 2007, 07:15
  5. simple pointer question
    By mickey in forum General Programming
    Replies: 6
    Last Post: 16th June 2006, 09:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.