Dynamic Memory Question

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 48
Reputation: atman is an unknown quantity at this point 
Solved Threads: 0
atman atman is offline Offline
Light Poster

Dynamic Memory Question

 
0
  #1
May 23rd, 2009
Hello!
I'm new in C++, and I have a basic question:

When I have a function:

  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Dynamic Memory Question

 
0
  #2
May 23rd, 2009
You forgot to allocate +1 byte for zero char terminating C-string:
  1. p = new char[strlen(s)+1];
Now you have memory leak: you forgot to deallocate this memory:
  1. delete [] p;
Well, it's evidently useless function...

The 2nd snippet:
  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):
  1. cout << "Please Enter a string: ";
  2. string line;
  3. getline(cin,line);
  4. cout << line << endl;
Feel the difference
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Dynamic Memory Question

 
0
  #3
May 23rd, 2009
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.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC