How to fix stack overflow error in visual C/C++? [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I am writing a file in C/C++ which generates 100000 lines of records (name, int[5] grade, double[5].value). The code should generate 100000 random characters for name and integer for value. I am getting stackoverflow error. Can anyone pls help?

Do not use a recursive function to generate or process the records and do not allocate the records on the stack. Recursion can be replaced by iteration, and a stack array can be replaced by std::vector.

To avoid a stack overflow error, don't put so much data on the stack. Essentially: don't use local variables that are large arrays. Instead, create locals which are pointers, and use malloc() or new to allocate space for the actual data... this puts the pointer on the stack but the data elsewhere.
Finally, don't forget that you must free() anything you malloc(), and you must delete anything you new, once you're done with it (and while you still have the pointers!).

Related

how to completely free 2d multidimensional pointer array in c++ [duplicate]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I created an object which has few integer variables, and one char memory block say which is allocated with memory of 300-500 bytes as its members . After that this object was pushed in to vector by one thread, and then another thread which is running parallel will swap with one empty vector and start processing the vector which contains the object, after processing I used to delete the char block used in object and also finalized the object and deletd the object also. But it seems the memory was not freeing up. I ran this block of code with valgrind tool it doesn't show any leak. Please help me with the solution
But it seems the memory was not freeing up.
How are you determining that exactly? I assume incorrectly. Calling delete whatever marks that memory as deallocated and free for future use. The language doesn't specify exactly what that means. Your OS is better at managing memory than you are. It isn't forced to, for example, ensure that whatever tool (task manager?) you are using to measure memory usage now sees X more free bytes.
The memory you were using is now free to be allocated again. That's what you need to be concerned with, let the OS's memory manager worry about the details.

Visual C++ member variables changing unexpectedly and unexplainably [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm initializing an instance of PhysWorld class as shown here:
At this point the member variables are as follows:
This seems correct to me.
Then this line executes:
We step into:
And at this point, the member variables look like:
Can someone please help me understand what is going on here? This is one of my first attempts in c++ so I'm guessing it's something stupid on my part.
Thanks!
You probably loose variable value on assignment:
pw = PhysWorld(...);
This statement constructs a temporary object, and then makes a call: pw.operator=(const PhysWorld&);. Check how you implement it (if you do).
Also your function setRectDef contains a serious bug: you are storing a pointer to a stack variable, which would be invalid after leaving the function scope, and accessing it later most likely ruin your stack.
Edit: how to handle tmpS.
You need to allocate your structure on heap:
b2PolygoinShapre *tmpS = new b2PolygoinShape;
tmpS->SetAsTextBox(...);
this->rect = tmpS;

Implementation of vector of integers with overloaded operators, fails to run properly [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have huge problems with making this code work properly.
http://pastebin.com/Mi6gj188
There's an output from example program on the bottom. It simply crashes and doesn't deliver proper results too. It seems that none of the overloaded operators work as it should
You didn't write a copy constructor, or use RAII. As a result, every time your vector object is copied (and it is, a lot, because you make no use of references!!) your internal data pointer is copied, sharing it amongst multiple objects (each of which will attempt to delete it on destruction) causing a horrible bug.
Your book tells you about the rule of three, which you should now go ahead and work on following.

c++ cleaning the heap [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am having serious issues in the heap because in the previous programs, I did not delete the pointers variables. How do I clean the memory space/heap now to run the current programs?
Kill the offending processes, then the OS will clean up their memory for you.
For every allocation you make on the heap with new/malloc or other such functions you must delete/free the resource that you have used when you are finished with it.
C++ makes available some easy ways to do this with constructors allocating resources and destructors free'ing the resource.
Then there are lots of clever ways with std::auto_ptr or even other smart pointers that can help you manage this.
Basically you allocated it - so it is your job to free it.

Segmentation fault. 0xb7d9e768 in memmove () from /lib/libc.so.6 [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
GDB gives me the above error WRT my C++ program. Nowhere I have used any memory function including new and delete etc.
I want to understand the 'MEANING' of this error.
If run your program under gdb, you should be able to print out the backtrace and see what part of your code is causing the segmentation fault. memmove() may being called indirectly through a different system call.
It is possible that an array operation in your code gets optimized as a call to memmove: this is probably why the compiled code uses memmove, whereas your source code doesn't.
I think you should check that you are not accessing your arrays out of bounds.
memmove tried to access (read or write) a memory segment it shouldn't touch.
The reasons can be manifold, but probably pointer corruption. Check it with a debugger, valgrind, check stack trace, etc...