?
Solved

Simple Program - Segmentation Fault

Posted on 2003-03-19
10
Medium Priority
?
187 Views
Last Modified: 2010-05-18
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
Comment
Question by:oslicko
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 2
10 Comments
 

Author Comment

by:oslicko
ID: 8167135
#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
 
LVL 6

Expert Comment

by:gj62
ID: 8167163
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
 
LVL 6

Expert Comment

by:gj62
ID: 8167203
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!

 
LVL 6

Expert Comment

by:gj62
ID: 8167255
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
 
LVL 6

Accepted Solution

by:
gj62 earned 200 total points
ID: 8167303
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
 
LVL 6

Expert Comment

by:gj62
ID: 8167336
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
 

Author Comment

by:oslicko
ID: 8167385
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
 
LVL 6

Expert Comment

by:gj62
ID: 8167405
My bad.  Change it to an int.  You should provide a return value for main...
0
 
LVL 5

Expert Comment

by:ecw
ID: 8167416
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
 
LVL 6

Expert Comment

by:gj62
ID: 8167755
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses
Course of the Month10 days, 1 hour left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question