| | |
HEAP Corruption Error
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Solved Threads: 1
I'm trying to calculate the length of three individual arrays and plug that length into a function to dynamically allocate memory to a new array and delete it. I keep overwriting my HEAP and I'm not sure where the problem is
Main looks like this
The calling functions look like this
C++ Syntax (Toggle Plain Text)
void punchmeinthehead() { const int maxin = 16; char lastName[maxin] ; char firstName[maxin] ; char midName[maxin] ; size_t length = strlen(lastName) + strlen(firstName) + strlen(midName) + 1; char *fullName = new char[length]; strcpy (fullName, lastName); strcat (fullName, " "); strcat (fullName, firstName); strcat (fullName, " "); strcat (fullName, midName); cout << fullName << endl; delete [] fullName; }
Main looks like this
C++ Syntax (Toggle Plain Text)
void main() { const int maxin = 16; char lastName[maxin]; char firstName[maxin]; char midName[maxin]; getlast(lastName, maxin); getfirst(firstName, maxin); getmid(midName, maxin); punchmeinthehead(); }
The calling functions look like this
C++ Syntax (Toggle Plain Text)
void getlast( char lastName[] , int maxin) { cout << " Please enter your last name up to 15 characters " << endl; cin.getline(lastName, maxin, '\n' ); cin.ignore (cin.rdbuf()->in_avail(), '\n'); } //Input first name void getfirst( char firstName[] , int maxin) { cout << " Please enter your first name up to 15 characters " << endl; cin.getline(firstName, maxin, '\n' ); cin.ignore (cin.rdbuf()->in_avail(), '\n'); } //Input middle name void getmid( char midName[] , int maxin) { cout << " Please enter your middle name up to 15 characters " << endl; cin.getline(midName, maxin, '\n' ); cin.ignore (cin.rdbuf()->in_avail(), '\n'); }
Last edited by henpecked1; Mar 14th, 2008 at 2:16 am.
•
•
•
•
C++ Syntax (Toggle Plain Text)
void punchmeinthehead() { const int maxin = 16; char lastName[maxin] ; char firstName[maxin] ; char midName[maxin] ; size_t length = strlen(lastName) + strlen(firstName) + strlen(midName) + 1; char *fullName = new char[length]; strcpy (fullName, lastName); strcat (fullName, " "); strcat (fullName, firstName); strcat (fullName, " "); strcat (fullName, midName); cout << fullName << endl; delete [] fullName; }
lastName will be, and how you expect strlen(lastName) to behave. •
•
Join Date: Nov 2007
Posts: 981
Reputation:
Solved Threads: 210
About the punchmeinthehead() function ...
Hmm .. you could rename punchmeinthehead() to punchmeintheheap() ...
C++ Syntax (Toggle Plain Text)
void punchmeinthehead() { const int maxin = 16; // Here you have three uninitialized arrays of char, use a // debugger to see what they contain here ... // (Shouldn't you instead be using the variables // that you have in your main() ?) char lastName[maxin] ; char firstName[maxin] ; char midName[maxin] ; // if your three arrays are OK at this point, the following works size_t length = strlen(lastName) + strlen(firstName) + strlen(midName) + 1; char *fullName = new char[length]; // fullname may also be NULL at this point <snip> // Did you account for the extra space character when // allocating memory? strcat (fullName, " "); <snip> }
Hmm .. you could rename punchmeinthehead() to punchmeintheheap() ...
Last edited by mitrmkar; Mar 14th, 2008 at 7:12 am.
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Solved Threads: 1
To Sarehu: I'm aware there are problems with it, but I'm very new to this so I expected there to be plenty wrong.
Originally, I'm trying to prompt the user for last, first, and middle names, storing them in fixed arrays. Then, output that name by dynamically allocating a "new" array where the names are all added together with appropriate spaces. After that, prompt the user for another input or to quit, I just haven't written the loop portion yet because I can't get past this.
I had the fixed arrays and array size declared in the header, but my instructor nixed that idea and made me change it all around to pass the arrays in. Now this part I'm even worse at than writing original functions. So I try to pass the array contents in, but it gives me the unknown identifier errors and all that. When that happened, I put the variables in the function (since I can no longer use globals).
Strlen at this point I'm hoping to calculate the contents of the fixed arrays lastName, firstName, and midName, and then add the lengths together so I know how large the dynamic allocation has to be and pass it into the dynamically allocated array. Obviously that's not happening and I've screwed it up from my original design.
The original design (see henpecked1 post on string help) had the string length being done as a separate function and pass it into punchmeinthehead, but when the instructor changed it all around, I no longer knew the syntax for passing it, especially using the size_t identifier.
Is that clear enough or did I just confuse things more?
Originally, I'm trying to prompt the user for last, first, and middle names, storing them in fixed arrays. Then, output that name by dynamically allocating a "new" array where the names are all added together with appropriate spaces. After that, prompt the user for another input or to quit, I just haven't written the loop portion yet because I can't get past this.
I had the fixed arrays and array size declared in the header, but my instructor nixed that idea and made me change it all around to pass the arrays in. Now this part I'm even worse at than writing original functions. So I try to pass the array contents in, but it gives me the unknown identifier errors and all that. When that happened, I put the variables in the function (since I can no longer use globals).
Strlen at this point I'm hoping to calculate the contents of the fixed arrays lastName, firstName, and midName, and then add the lengths together so I know how large the dynamic allocation has to be and pass it into the dynamically allocated array. Obviously that's not happening and I've screwed it up from my original design.
The original design (see henpecked1 post on string help) had the string length being done as a separate function and pass it into punchmeinthehead, but when the instructor changed it all around, I no longer knew the syntax for passing it, especially using the size_t identifier.
Is that clear enough or did I just confuse things more?
Last edited by henpecked1; Mar 14th, 2008 at 1:05 pm.
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Solved Threads: 1
To mitrmkar:
The uninitialized arrays are there because the program is making me identify the variables lastName, firstName, midName, and maxin. These are indeed the variables identified in main, unless you mean something that's obviously escaping me.
When I use the debugger and see what's in them, (as best I can being new) they pretty much contain the expected junk until the user enters a name, and then they contain the name.
I know I'm overwriting memory, and I believed it was because I wasn't passing the length properly, but if it's because the functions I created for user input are wrong, by all means, tell me what I did wrong because everything works fine up until the point of working with the dynamic allocation of the new array
The uninitialized arrays are there because the program is making me identify the variables lastName, firstName, midName, and maxin. These are indeed the variables identified in main, unless you mean something that's obviously escaping me.
When I use the debugger and see what's in them, (as best I can being new) they pretty much contain the expected junk until the user enters a name, and then they contain the name.
I know I'm overwriting memory, and I believed it was because I wasn't passing the length properly, but if it's because the functions I created for user input are wrong, by all means, tell me what I did wrong because everything works fine up until the point of working with the dynamic allocation of the new array
•
•
•
•
C++ Syntax (Toggle Plain Text)
void punchmeinthehead() { const int maxin = 16; char lastName[maxin] ; char firstName[maxin] ; char midName[maxin] ;
The idea being that you don't want to have to keep track of what names have been given for variables in other parts of your program, C code sees the existence of a given variable only with that variable's block. If you write your
punchmeinthehead function as the following, you'll get the same exact behavior. (Note that I've only changed the local names around.) C++ Syntax (Toggle Plain Text)
void punchmeinthehead() { const int maxin = 16; char lastBlah[maxin] ; char firstBlah[maxin] ; char midBlah[maxin] ; size_t length = strlen(lastBlah) + strlen(firstBlah) + strlen(midBlah) + 1; char *fullName = new char[length]; strcpy (fullName, lastBlah); strcat (fullName, " "); strcat (fullName, firstBlah); strcat (fullName, " "); strcat (fullName, midBlah); cout << fullName << endl; delete [] fullName; }
The rule is, for any local variable you declare, if you change its name and all the instances of its name below that declaration in the same block, you won't change the behavior of your code. So hopefully, you can see that local variable declarations don't refer to anything outside the function, and that they define variables that last only during the current instance of the function call, and cease to exist after the function returns. Even if you have a function call itself, a different piece of memory will be used for each instance of the function call.
Now let's look at the other problem.
•
•
•
•
C++ Syntax (Toggle Plain Text)
size_t length = strlen(lastName) + strlen(firstName) + strlen(midName) + 1; char *fullName = new char[length]; strcpy (fullName, lastName); strcat (fullName, " "); strcat (fullName, firstName); strcat (fullName, " "); strcat (fullName, midName); cout << fullName << endl; delete [] fullName; }
length is 2 less than what it should be, since you forgot to account for the two spaces you've included.Let's look at how some of the other code works and why it's different than
punchmeinthehead :•
•
•
•
C++ Syntax (Toggle Plain Text)
void getlast( char lastName[] , int maxin) { cout << " Please enter your last name up to 15 characters " << endl; cin.getline(lastName, maxin, '\n' ); cin.ignore (cin.rdbuf()->in_avail(), '\n'); }
getlast implementation. And unlike punchmeinthehead , the way you've used it, the character buffer declared in main ends up getting modified. You should ask, why is that?It has nothing to do with the name you've used. You could rewrite your
getlast function with a different variable name:•
•
•
•
C++ Syntax (Toggle Plain Text)
void getlast( char something[] , int maxin) { cout << " Please enter your last name up to 15 characters " << endl; cin.getline(something, maxin, '\n' ); cin.ignore (cin.rdbuf()->in_avail(), '\n'); }
getlast see the character array in the calling function, main ? The answer is: You've passed the memory address of this character buffer directly, as a parameter.When you write
getlast(lastName, maxin); , you're passing two values, both of which are numbers. The first number, lastName , is the memory address of the character array that sits in the local memory of the current instance of the main function. That number is unintelligible, and we call it a character array, since that's how we mean to use it. The second number is 16, the value of maxin .When this function call happens,
getlast gets executed within its own area of local memory, and the variables something and mixin get assigned the values of the arguments you've passed. In this case, something will be assigned whatever value was passed as the first argument, and mixin will be assigned whatever walue was passed as the second argument. (Again, the fact that the main function has a variable called mixin can be treated like nothing more than a coincidence.)So then
getlast is performing operations with a memory address pointing to an array in the instance of the main function.It must be annoying, having to deal with crap like this as a newbie. There's something wrong with the teacher if you have to write magic like that.
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Solved Threads: 1
Okay, so the heap error is gone and I feel really stupid for missing the spaces, and as your explanation would impart, it's printing out 50 elements of crap to the screen. So now the question begs because I am new, how do those arrays get passed (syntax) to the punchmeinthehead function?
And he gave us the cin.ignore part, I didn't have to figure that out thank goodness. He tells us it is to clear any remnants left from the getline in a nice clean manner
And he gave us the cin.ignore part, I didn't have to figure that out thank goodness. He tells us it is to clear any remnants left from the getline in a nice clean manner
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Solved Threads: 1
Main looks the same, and I moved punchmeinthehead to the same cpp file where the input functions are for ease of posting and printing. Here is what I have now:
Now of course I'm getting errors because I am painfully aware of my lack of skill in passing the arrays...what am I missing?
C++ Syntax (Toggle Plain Text)
#include "nameinp.h" #include <iostream> #include <string.h> using namespace std; //Input last name void getlast( char lastName[] , int maxin) { cout << " Please enter your last name up to 15 characters " << endl; cin.getline(lastName, maxin, '\n' ); cin.ignore (cin.rdbuf()->in_avail(), '\n'); } //Input first name void getfirst( char firstName[] , int maxin) { cout << " Please enter your first name up to 15 characters " << endl; cin.getline(firstName, maxin, '\n' ); cin.ignore (cin.rdbuf()->in_avail(), '\n'); } //Input middle name void getmid( char midName[] , int maxin) { cout << " Please enter your middle name up to 15 characters " << endl; cin.getline(midName, maxin, '\n' ); cin.ignore (cin.rdbuf()->in_avail(), '\n'); } void punchmeinthehead() { size_t length = strlen(getlast) + strlen(getfirst) + strlen(getmid) + 3; char *fullName = new char[length]; strcpy (fullName, getlast); strcat (fullName, " "); strcat (fullName, getfirst); strcat (fullName, " "); strcat (fullName, getmid); cout << fullName << endl; delete [] fullName; }
Now of course I'm getting errors because I am painfully aware of my lack of skill in passing the arrays...what am I missing?
Last edited by henpecked1; Mar 14th, 2008 at 1:52 pm.
![]() |
Similar Threads
- Call Stack (C++)
- realloc malloc and Heap problem. (C++)
- Merge Sort on a Array-Based List (C++)
Other Threads in the C++ Forum
- Previous Thread: need help in cin.get
- Next Thread: Another new data type issue
Views: 2590 | Replies: 14
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





