943,794 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 259
  • C++ RSS
May 23rd, 2009
0

Dynamic Memory Question

Expand Post »
Hello!
I'm new in C++, and I have a basic question:

When I have a function:

C++ Syntax (Toggle Plain Text)
  1. void foo(str* s){
  2. char* p;
  3. p = new char[strlen(s)];
  4. strcpy(p, s);
  5. cout << s <<endl;
  6.  
  7. }

This function will take the length of s and in bytes and will allocate that much space in memory for p.
I want to do the same but ask the user to enter a string and then dynamicly allocate memory for the entered string, but it wouldnt compile for some reason, and i'd get a segmentation error. I know I can print s right away, but i want to allocate memory dynamicly in this exersize.
Here is the code I have so far:
C++ Syntax (Toggle Plain Text)
  1. int main(void){
  2.  
  3. char* s;
  4. char* p;
  5. cout << "Please Enter a string: ";
  6. cin >> s;
  7. p = new char[strlen(s)];
  8. strcpy(p, s);
  9. cout << p <<endl;
  10. return 0;
  11. }

Any help would be greatly appreciated.
Thank you!
Last edited by atman; May 23rd, 2009 at 7:05 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atman is offline Offline
50 posts
since Oct 2008
May 23rd, 2009
0

Re: Dynamic Memory Question

You forgot to allocate +1 byte for zero char terminating C-string:
C++ Syntax (Toggle Plain Text)
  1. p = new char[strlen(s)+1];
Now you have memory leak: you forgot to deallocate this memory:
C++ Syntax (Toggle Plain Text)
  1. delete [] p;
Well, it's evidently useless function...

The 2nd snippet:
C++ Syntax (Toggle Plain Text)
  1. char* s;
  2. ...
  3. cin >> s;
Pointer variable s is not initialized, it points to nowhere, but you are trying to get a string in this undefined memory. The 2nd error: the same as in the 1st snippet.

It's C++ program, use C++ constructs (more robust and comfortable):
C++ Syntax (Toggle Plain Text)
  1. cout << "Please Enter a string: ";
  2. string line;
  3. getline(cin,line);
  4. cout << line << endl;
Feel the difference
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
May 23rd, 2009
0

Re: Dynamic Memory Question

You cannot get input to pointer s without first allocating it some memory. What you're doing is attempting to store to whatever memory address s points to, which could be pretty random!

If you've not allocated the memory, you don't control it. If you don't control it, you should not be trying to modify it.

And, when you allocate memory to hold a copy of a string, you need to use strlen( source ) +1 as the size of the new array - you've got to allow for the NULL terminator character.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with function and pointers
Next Thread in C++ Forum Timeline: Employees





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC