What is wrong with my code? C++ and segmentation fault
I'm trying to allocate an array of pointers dynamically. The array is allocated successfully in the routine "allocSpace" but when it returns to the main functions it is no longer available, it gives me 'Segmentation Fault'. Please help.
Code:
#include <iostream> |
arr is passed by value.
you can fix by adding reference (little confusing when near stars) or by adding one more star :-) Code:
#include <iostream> |
When you call allocSpace(), parameter x is a local copy (within allocspace()) of variable arr in function main(). allocspace() alters that local copy when it does a malloc(). Variable arr in main() is never actually set or altered by your program.
You want arr to be an array of pointers to structs, with the array dynamically allocated with malloc() and the pointers dynamically allocated with new, right? I present two solutions: an adequate one, and a better one. The adequate one: Code:
#include <iostream> Code:
#include <iostream> Hope this helps. |
I think you should decide if you want to learn C or C++. Your program, while it may compile as C++ code (it won't compile as C), is horrible mish-mash of both. A good C++ programmer knows where C and C++ differs and can handle raw arrays with confidence, but a good C++ programmer also puts what C++ offers to use too. And when you start learning C++, you should use std::string and std::vector, for example, from the start. These things were incorporated into C++ to assist the programmer and if you master the c++ standard library you've come far into the language.
|
Quoth hivemind:
Quote:
I use C mostly. C++'s fine additions can be used for a complete paradigm shift, as hivemind suggests. But they can also be used piecemeal in what would otherwise be a C program. That's the way I use C++. I'm sure I'm passing up many aids to productivity by using C++ in such an ugly manner, but my method works for me, quickly and easily. Be a purist if you want. That has many rewards, many of which are quite practical. Or just grab those elements of C++ which float your boat, and go with it. |
Unless you know what your doing and anybody using your code knows what they are doing do not mix malloc and new calls, they are not the same yet this instance makes no difference besides when freeing memory.
Personally I would have wrote it like so: Code:
#include <iostream> |
Quoth dmail:
Quote:
|
Quote:
|
malloc doesn't know about constructors, it allocates raw memory. If you're programming in C++ you use new and delete, if you program in C you use malloc (and its friends) and free.
This usenet post sums it up quite nicely: Quote:
|
Quote:
This is actually incorrect. void pointers can become any kind of pointer. But I do agree that you should use new instead of malloc in C++ |
Quote:
http://www.research.att.com/~bs/bs_faq2.html#void-ptr |
dmail beat me to it. :) This is one of the cases where C and C++ differs. In C++, there are no implicit conversions from pointer-to-void to, say, pointer-to-char, a cast is required. In C it's not only unnecessary to cast what malloc returns it's bad because it can mask a failure to include its header. Anyway, this has been covered so many times and is getting a bit off-topic for the thread.
|
Silly C++
Yeah it is getting off topic sorry :P |
All times are GMT -5. The time now is 09:56 AM. |