954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Access Violation (Segmentation Fault) + atol

Hello all,

i am new to C++ and im writing a little encryption program for fun. It requires the user to enter a 'passkey', which is a string (numbers, and letters). Then the program defines the ASCII value of all of the characters, and places them in the string - one after another. Then i want to use the "atol" function, to transfer the value of the string into the defined integer variable. This is where the program crashes and dispalys an error message "Access Violation (Segmentation Fault)".

Here is the code for that section:
--------------------------------

char* value;
int num_value;

cout << "\n" << "What would you like the PassKey to be? ";
cin >> passkey;

//**Convert to ASCII in this followiing Section**

int x = 0;

while (passkey[x] != '\0')
{
value += int(passkey[x]);
x++;
}

num_value = atol (value);


//**End of ASCII convert.**

-------------------------------------

The program crashes when it gets to "num_value = atol (value);". I am not sure what the problem is. Can you please help, im stuck.

cameronius
Newbie Poster
4 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 
char* value;


Where is the part that you have this pointer point to memory you can write to?

value += int(passkey[x]);


Can you describe in words what you believe this is doing?

num_value = atol (value);


Sinceatol requires a null-terminated string, where do you null terminate the "string"?

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

num_value = atol (value);

num_value is an int
atol returns a long int

solution: long num_value;

picogeek
Newbie Poster
2 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 
char* value;

Where is the part that you have this pointer point to memory you can write to?

value += int(passkey[x]);

Can you describe in words what you believe this is doing?

num_value = atol (value);
Sinceatol requires a null-terminated string, where do you null terminate the "string"?

Dave,

Thankyou for replying, as i stated i am new to this, and am not really sure of what i am doing.

I dont understand the first question you asked. Im confused, can you explain, and i might be able to help you.

Second - well, this while block takes each individual character of the string, and converts it to ASCII. The part that states:

value += int(passkey[x]);

I believe that this takes the selected character of the string, in ASCII form, and adds it to the string. eg. say the string was = "12658" and the value of the character i was adding was 10, the string would = "1265810"

Am i completly wron in thinking this??? Please help.

Thanks for you help, dave!

cameronius
Newbie Poster
4 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 
Am i completly wron in thinking this???


It doesn't work like that. Strings represented by character arrays are very low level. You have to do everything manually, including allocate enough memory and terminate the string with '\0'.

The way you're doing it, you really want to avoid appending characters to a string because the ASCII values you're working with are all double digits. Try this instead.

#include <iostream>

using namespace std;

int main()
{
  char passkey[100];
  int key = 0;

  cout<<"Enter a passkey: ";
  cin>> passkey;

  for (int i = 0; passkey[i] != '\0'; i++)
    key = 10 * key + int(passkey[i]);

  cout<<"The key value is "<< key <<endl;
}

The problem with this is that you're limiting a user to 99 characters for the passkey. Since this is C++, you should take advantage of the string class.

#include <iostream>
#include <string>

using namespace std;

int main()
{
  string passkey;
  int key = 0;

  cout<<"Enter a passkey: ";
  cin>> passkey;

  for (int i = 0; passkey[i] != '\0'; i++)
    key = 10 * key + int(passkey[i]);

  cout<<"The key value is "<< key <<endl;
}

Now the passkey can be any length that your memory can handle. The manual way of doing this is much harder.

Siersan
Light Poster
45 posts since Jan 2005
Reputation Points: 12
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You