memory access error

A

Amira

Could somebody possibly find out why I do not manage to compile this
code with g++ under linux? For any help I am really greatful! Cheers,
pa

#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
#include <cassert>
using namespace std;
using std::cout;

//using std::cout;
//using std::cin;
//using std::endl;

int molWeight_;
//char name_[10], family_[10];
char *protName_;
char *protFamily_;
const char *protName, *protFamily;

int main(){

protName_ = new char[strlen( protName ) + 1 ];
assert(protName_ != 0 );
strcpy(protName_, protName);

protFamily_ = new char[strlen( protFamily ) + 1 ];
assert(protFamily_ != 0 );
strcpy(protFamily_, protFamily);




cout << "Enter protein name: ";
cin >> protName_;
cout << "Enter molecular weight: ";
cin >> molWeight_;
cout << "Enter protein family: ";
cin >> protFamily_;

//delete [] protName_;
//delete [] protFamily_;

ofstream myfile;
myfile.open ("protein.txt");
myfile << "<Protein>\n" << " <molWeight>" << molWeight_ << "</
molWeight>"
<< "\n</Protein>" << endl;
myfile.close();

return 0;
}
 
I

Ian Collins

Amira said:
Could somebody possibly find out why I do not manage to compile this
code with g++ under linux? For any help I am really greatful! Cheers,
pa

#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
#include <cassert>
using namespace std;
using std::cout;

int molWeight_;
//char name_[10], family_[10];
char *protName_;
char *protFamily_;
const char *protName, *protFamily;

int main(){

protName_ = new char[strlen( protName ) + 1 ];

Fix this and see where you get. Hint, what does protName point to?

Ian Collins.
 
J

Jerry Coffin

Could somebody possibly find out why I do not manage to compile this
code with g++ under linux? For any help I am really greatful! Cheers,
pa

#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
#include <cassert>
using namespace std;
using std::cout;

//using std::cout;
//using std::cin;
//using std::endl;

int molWeight_;
//char name_[10], family_[10];
char *protName_;
char *protFamily_;
const char *protName, *protFamily;

These are both globals, so they're initialized as null pointers.
int main(){

protName_ = new char[strlen( protName ) + 1 ];

....and here you're dereferencing one of those null pointers, attempting
to treat it as a string.
assert(protName_ != 0 );
strcpy(protName_, protName);

protFamily_ = new char[strlen( protFamily ) + 1 ];

....and here you're dereferencing the other one.

I'd advise using std::string instead of attempting to deal with the
memory management directly:

#include <iostream>
#include <string>
#include <fstream>

int main() {
std::string name;
std::string weight;

std::cout << "Please enter the name: ";
std::getline(std::cin, name);

std::cout << "Please enter the weight: ";
std::getline(std::cin, weight);

....and so on. If you do (for whatever reason) want to deal with the
memory management directly, you're going to have to overhaul the logic
considerably. Right now, you're attempting to allocate a fixed amount of
storage, then read the string. To make things work, you need to allocate
some storage and read some data into it. Then, if there's more data, you
need to allocate more storage, read data into it, and repeat until
you've read all the data. The other possibility is to simply allocate a
fixed amount of storage, and if the user attempts to enter more, you
reject it (the extra, reject the whole input, or whatever seems most
suitable).
 
A

Amira

Hi Jerry,

thanks so much for your assistance. I made the changes and now the
code is running perfectly.

Only one question. Shall I write each time std::string .... isn't it
possible to define a namespace? What exactly does std::string?

And what does mean that "they're initialized as null pointers"?

Thanks for any reply.

Cheers,
Paul


Could somebody possibly find out why I do not manage to compile this
code with g++ under linux? For any help I am really greatful! Cheers,
pa
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
#include <cassert>
using namespace std;
using std::cout;
//using std::cout;
//using std::cin;
//using std::endl;
int molWeight_;
//char name_[10], family_[10];
char *protName_;
char *protFamily_;
const char *protName, *protFamily;

These are both globals, so they're initialized as null pointers.
int main(){
protName_ = new char[strlen( protName ) + 1 ];

...and here you're dereferencing one of those null pointers, attempting
to treat it as a string.
assert(protName_ != 0 );
strcpy(protName_, protName);
protFamily_ = new char[strlen( protFamily ) + 1 ];

...and here you're dereferencing the other one.

I'd advise using std::string instead of attempting to deal with the
memory management directly:

#include <iostream>
#include <string>
#include <fstream>

int main() {
std::string name;
std::string weight;

std::cout << "Please enter the name: ";
std::getline(std::cin, name);

std::cout << "Please enter the weight: ";
std::getline(std::cin, weight);

...and so on. If you do (for whatever reason) want to deal with the
memory management directly, you're going to have to overhaul the logic
considerably. Right now, you're attempting to allocate a fixed amount of
storage, then read the string. To make things work, you need to allocate
some storage and read some data into it. Then, if there's more data, you
need to allocate more storage, read data into it, and repeat until
you've read all the data. The other possibility is to simply allocate a
fixed amount of storage, and if the user attempts to enter more, you
reject it (the extra, reject the whole input, or whatever seems most
suitable).

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
J

Jerry Coffin

[ ... ]
Only one question. Shall I write each time std::string .... isn't it
possible to define a namespace? What exactly does std::string?

And what does mean that "they're initialized as null pointers"?

By my count, that's three questions, not one! :)

1) The 'std' is already a defined namespace. You can a namespace
declaration or a namespace directive to make the name visible without
qualification:

using std::string; // declaration -- makes 'string' visible

using namespace std; // directive -- makes everything in 'std' visible

Generally you only want one or the other. I have no problem with just
typing 'std::string', but I guess that's mostly a matter of taste.

2) I'm not sure what you're asking -- specifically, I'm not sure whether
you're asking how std::string works, or about the difference between
using the fully qualified name vs. a using declaration/directive like
above.

3) It means the pointer is set to a value that is not a valid address,
and you're not supposed to look at. A typical use would be something
like a linked list -- you have some nodes, each of which contains some
data and a pointer to the next node in the list. At the end of the list,
you have a null pointer, to signal that there are no more nodes in the
list.
 
P

Pronova

I can tell you exactly whats wrong.

When you wrote:
protName_ = new char[strlen( protName ) + 1 ];

that would have been unpredictable because protName hasn't been initialized
to point to anything yet. In fact, it might be pointing to some areas of
your code that was unintended and causing God knows what. Of course, this
would be a worse case scenario. Most programs that attempt to do this just
crash unexpectatly.

Everytime you access protName_ or protName or protFamily, etc., it would
have caused that same thing to happen.

In summary:

1. Don't use pointers until they are initialized and pointing to valid
areas of memory.
2. Don't use the c string functions like strcpy, strlen, etc. until you
know the variable actually contains a zero-terminated string.

There are many ways to fix this.

1. Follow the advice of that guy who suggested using std::string.
2. Allocate a fixed-amount of characters (buffer).

I don't have to explain to you why using std::string is the better choice,
but if you ever wanted to experiment, try creating fixed-sized buffers.

ex.

const int BUFFER_SIZE = 255;

char protName[BUFFER_SIZE];

cin.getline(protName);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

c++ objects 4
Cannot find my infinite loop 1
c++ objects 16
First time question 1
Codeforces problem 0
A cheat sheet for circles 6
Calculater code 0
Trying to understand "&&" 4

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top