• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • Last Modified:

Simple Program - Segmentation Fault

Hello, I am fairly new to C programming...and for my current program shown below, I compile successfully but get Segmentation Fault when running the program. Can anyone point me in the right direction? I would greatly appreciate any feedback. Thanks!
0
oslicko
Asked:
oslicko
  • 7
  • 2
1 Solution
 
oslickoAuthor Commented:
#include <errno.h>
#include <unistd.h>
#include <stdio.h>

void   hash_pw(unsigned char * key, int size, unsigned char * pw);

main() {

  char   *keyval;                      /*  Input buffer. */
  char   *pwval;                       /*  Query String env variable   */
  int    size;                         /* int content length          */

  printf("\nit works");

  keyval = "POID388631";
  size = 9;
  pwval = NULL;

  hash_pw(keyval, size, pwval);


}

  void hash_pw( unsigned char * key, int size, unsigned char * pw ) {
 
        unsigned long n, q;
 
        n=0;
 
        for ( q=0; q<strlen(key); q++ ) {
                n = n*1103515245+ (long)key[q];
        }
 
        for ( q=0; q<size; q++ ) {
                if (n%36 > 9 ) {
                        *(pw+q) = (char) (n%36+87);
                }
                else {
                        *(pw+q) = (char) (n%36+48);
                }

                n = n*1103515245+12345;
                if ((*(pw+q)=='0') || (*(pw+q)=='1') || (*(pw+q)=='l') || (*(pw+q)=='o')) q--;
       
        }
         
        *(pw+size) = (char)0;
        return;
}
0
 
gj62Commented:
First, you allocate no storage to your strings:

char   *keyval;                      /*  Input buffer. */

 keyval = "POID388631"; /*keyval is only a pointer - this will overrite memory*/


Instead, you can say

char *keyval = "POID388631*/
0
 
gj62Commented:
Also, in hash_pw, both pw and q initially = 0 (or NULL).

you try to dereference them with the statement

*(pw+q) - that will cause you to blow up every time, since you are trying to assign a character to that expression...

basically, what you are saying is *(0+0).
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
gj62Commented:
Oh, if you want to be able to modify keyval, define it as follows:

char keyval[] = "POID388631";

but understand that you cannot assign a string longer than 10 characters to keyval...
0
 
gj62Commented:
Try this:

#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>


void   hash_pw(char * key, int size, char * pw);

void main() {

 char   keyval[] = "POID388631";                      /*  Input buffer. */
 char   pwval[11];                       /*  Query String env variable   */

 printf("\nit works\n");
 
 strcpy(pwval, keyval);

 hash_pw(keyval, strlen(keyval), pwval);

 printf("String = %s, Hashed = %s\n", keyval, pwval);
}

 void hash_pw( char * key, int size, char * pw ) {
 
       unsigned long n, q;
 
       n=0;
 
       for ( q=0; q<strlen(key); q++ ) {
               n = n*1103515245+ (long)key[q];
       }
 
       for ( q=0; q<size; q++ ) {
               if (n%36 > 9 ) {
                       *(pw+q) = (char) (n%36+87);
               }
               else {
                       *(pw+q) = (char) (n%36+48);
               }

               n = n*1103515245+12345;
               if ((*(pw+q)=='0') || (*(pw+q)=='1') || (*(pw+q)=='l') || (*(pw+q)=='o')) q--;
       
       }
       
       *(pw+size) = (char)0;
       return;
}
0
 
gj62Commented:
Oh, by the way, you can't assign strings in C with the = operator - you can only do that during definition, e.g.

These are ok:

char myString1[] = "This is my string"; /*creates a writeable string*/

char *myString2 = "this is my 2nd string"; /* creates a static string (you can't change it) */

strcpy(myString1, "new text"); /* myString now = new text */

These are not ok:

myString1 = "new text"; /*can't do this*/

strcpy(myString1,"This is a really long string");  /* this is not ok, because myString1 can only hold 17 characters - this will overflow myString1 */

Read through the C-FAQ - it will help you...

http://www.eskimo.com/~scs/C-faq/top.html
0
 
oslickoAuthor Commented:
Ok I used the code you submitted, yet I had to remove the void before the main() in order for it to compile...not sure why. But it seems like its working now! Thanks a lot for your help, I really appreciate it.
0
 
gj62Commented:
My bad.  Change it to an int.  You should provide a return value for main...
0
 
ecwCommented:
The obvious problem is not allocating any storage for pwval before calling hash_pw.

Despite what gj62 says, one can make a pointer point to a different string using the syntax,
  char *str;
...
  str = "hello world";

But then the contents of the memory pointed to by str should be regarded as static.
0
 
gj62Commented:
Sorry, ecw - had a typo -

You can't do this:

myString2 = "new text";

myString2 was declared as a char array, not a char pointer, and therefore is not assignable.  

Subtle difference, but important in this case...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now