473,399 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

memory access error

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;
}

Feb 4 '07 #1
6 1816
Amira wrote:
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.
Feb 4 '07 #2
In article <11**********************@p10g2000cwp.googlegroups .com>,
pa********@gmail.com says...
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.
Feb 4 '07 #3
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.

Feb 4 '07 #4
In article <11**********************@j27g2000cwj.googlegroups .com>,
pa********@gmail.com says...

[ ... ]
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Feb 4 '07 #5
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);

"Amira" <pa********@gmail.comwrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...
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;
}

Feb 5 '07 #6
Pronova wrote:
I can tell you exactly whats wrong.
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>
Feb 5 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joe Joe | last post by:
I write my C program in Visual C. After a malloc, I loop for 100 times and amend the memory contents which I have malloced (but I have never change the ptr or its address). After that I free the...
10
by: chinabooter2003 | last post by:
Dear members: I am programming in C. I declare as follows but when running program, there is a fatal error appear: double a; double b;
10
by: eyh5 | last post by:
Hi, My C code (running on Soalris Unix) has some "segmentation fault" that I wish to use purify to do it. I poked around the web, and found some information about adding some lines in a Makefile...
3
by: hari krishna | last post by:
hi, I am generating excel reports through vb.Net. After creating excel.application and the report is generated, each report leaves Excel in memory. I can see them in task manager in Process tab...
9
by: Roshni | last post by:
Hi, I wanted to know how do function pointers sometime access illegal memory access ? Could any one give me an example ? Thanks, Roshni
13
by: hurry | last post by:
In order to avoid declaring static variables in a function I was asked to write a scratch memory. Reserve a block of memory outside the function and assigning pointers to the memory locations as...
5
by: Mahendra Kumar Kutare | last post by:
I am trying to implement a webserver with boss-worker model thread pool implementation - I have a header declaration threadpool.h as - typedef struct threadpool_work { void (*routine) ();...
5
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS...
6
by: Bobby | last post by:
Hi, I'm not 100% sure that this is an Access problem. A friend of mine has an Access 2003 database. Occasionally he brings a copy to me so that I can import some of his data into my Access 2003...
7
by: robert.waters | last post by:
I have an Access database frontend linked via ODBC to a large (gigabytes) mysql database. I need to view a large amount of data in a a textbox (variable up to 300K), but I receive a 'there isnt...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.