| | |
Dynamic Memory Question
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 48
Reputation:
Solved Threads: 0
Hello!
I'm new in C++, and I have a basic question:
When I have a function:
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:
Any help would be greatly appreciated.
Thank you!
I'm new in C++, and I have a basic question:
When I have a function:
C++ Syntax (Toggle Plain Text)
void foo(str* s){ char* p; p = new char[strlen(s)]; strcpy(p, s); cout << s <<endl; }
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)
int main(void){ char* s; char* p; cout << "Please Enter a string: "; cin >> s; p = new char[strlen(s)]; strcpy(p, s); cout << p <<endl; return 0; }
Any help would be greatly appreciated.
Thank you!
Last edited by atman; May 23rd, 2009 at 7:05 pm.
You forgot to allocate +1 byte for zero char terminating C-string:
Now you have memory leak: you forgot to deallocate this memory:
Well, it's evidently useless function...
The 2nd snippet:
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):
Feel the difference
C++ Syntax (Toggle Plain Text)
p = new char[strlen(s)+1];
C++ Syntax (Toggle Plain Text)
delete [] p;
The 2nd snippet:
C++ Syntax (Toggle Plain Text)
char* s; ... cin >> s;
It's C++ program, use C++ constructs (more robust and comfortable):
C++ Syntax (Toggle Plain Text)
cout << "Please Enter a string: "; string line; getline(cin,line); cout << line << endl;
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.
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Similar Threads
- Memory question (Motherboards, CPUs and RAM)
- memory question (Motherboards, CPUs and RAM)
- is there a program where there is only 32k in the main memory. (C++)
- How to share memory between applications? (C)
- Dual Channel memory question (Motherboards, CPUs and RAM)
Other Threads in the C++ Forum
- Previous Thread: Help with function and pointers
- Next Thread: Employees
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int integer java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






