The following code is causing a Segmentation Fault on the .ReadFromFile line:
int main()
{
// Load in.bmp
BMP * original;
cout << "line " << __LINE__ << ": Got here!" << endl;
original->ReadFromFile("in.bmp"); //Error HERE!
int width = original->TellWidth();
int height = original->TellHeight();
cout << "line " << __LINE__ << ": Got here!" << endl;
I'm using the EasyBMP library, which is included above the main function. I know it has something to do with memory and pointers, but I can't figure out what to use in place of "original->"... I've tried (*original). and (&original). but I can't seem to get it. Any help?
Thanks!
Your pointer original is pointing to a random memory location. Trying to use it will cause the undefined behavior. You need to allocate memory for BMP object and store the address in this pointer. If you want your object to persist even after it goes out of the function use original = new BMP();(don't forget to delete it later though) else you can directly do BMP original; and use it.
You have declared BMP as a pointer but never initialized it.
Try using:
BMP original;
or
BMP *original = new BMP();
The first method will create original on the stack, and you won't have to release it. The second method creates it in the free-store, and you need to use delete to free it.
delete original;
According to the tutorial, your code should be:
BMP Image;
Image.ReadFromFile( argv[3] );
It is not necessary to use a pointer try this:
// Load in.bmp
BMP original;
cout << "line " << __LINE__ << ": Got here!" << endl;
original.ReadFromFile("in.bmp"); //Error HERE!
int width = original.TellWidth();
int height = original.TellHeight();
cout << "line " << __LINE__ << ": Got here!" << endl;
or if you need to use the heap do:
BMP* original = new BMP();
and when you are done don't forget to free the memory
delete original;
Related
I'm quite new to the world of pointers in C/C++ so this may be quite an easy question for you:
The following C++-Code works normally
#include <iostream>
int main()
{
int theInt = 1337;
int & theReference = theInt;
int * thePointer = &theInt;
std::cout << "int: " << theInt << "\n";
std::cout << "referenz: " << theReference << "\n";
std::cout << "pointer: " << *thePointer << "\n";
std::cout << "pointer: " << *thePointer << "\n";
//std::cout << "foo" << "\n";
return 0;
}
but stops working when changing
//std::cout << "foo" << "\n";
to
std::cout << "foo" << "\n";
.
By "stops working" I mean: "is blocked by my norton security as a potential threat" (resulting in a return code of "0x76F531AF" if this is any help). Since norton normally doesn't interfere withe my programming I assume the double use of the int pointer in cout somehow results in a segfault...
Thx for your help!
PS:
I use Code::Blocks on Windows 8.1 with the GCC compiler and GDB debugger from TDM-GCC (version 4.7.1, 32 bit).
EDIT: removed deletion of pointer -> problem remains.
You can only delete objects created on the heap (using new or C-style malloc and such).
// allocate on the heap
int *intOnTheHeap = new int;
// do some stuff with it
*intOnTheHeap = 0;
(*intOnTheHeap)++;
std::cout << *intOnTheHeap << std::endl;
// deallocate
delete intOnTheHeap;
If you take a pointer to a local variable, it will point to an entry on the stack. You don't need to and shouldn't deallocate that memory yourself. The memory is "freed" by changing the stackpointer automatically when your variable runs out of scope (at the end of the function).
void myFunction() {
int localVariable;
int *pointerToLocalVariable = &localVariable;
// forbidden and unnecessary:
//delete pointerToLocalVariable;
// here (end of the block) the memory on the stack
// will be freed automatically
}
Since I got the same error after Norton-Interception in totally different contexts, this seems to be a case of Code::Blocks Norton incompatibility.
So, I have been trying to learn C++ and in the tutorial book that I am reading, I have gotten stuck on a problem of pointers-to-pointers. What I am trying to do change the pointer a pointer-to-pointer is pointing to without changing the value of the original pointer. Here's some code...
#include <iostream>
void testFunc(int **func_p_to_p) {
int *createdPointer = new int(10);
*func_p_to_p = createdPointer;
cout << **func_p_to_p << endl; //prints 10 as I expect
}
int main() {
int **main_p_to_p = NULL;
int *mainPointer = new int(5);
main_p_to_p = &mainPointer;
testFunc(main_p_to_p);
cout << **test << endl;//prints 10, I expect this...
cout << *mainPointer << endl; //prints 10 as well, I don't want that.
}
I asked a similar question about this earlier, here and I understand sort of what is going on. However, I can't seem to figure out how I would change where a pointer-to-pointer is pointing to without changing the original value. Could anyone explain how I would do this? Thanks.
In the line
*func_p_to_p = createdPointer;
you first follow your pointer-pointer to what it is pointing to: The address of int *mainPointer.
Next you assign a new value to this pointer, the value of createdPointer which is the address of new int(10).
So your mainPointer is now changed to target the same memory as createdPointer. Does this make any sense to you?
You can't do that. main_p_to_p points to mainPointer, so modifying *main_p_to_p modifies mainPointer, full stop.
You might be looking for this, though:
int main() {
int **main_p_to_p = NULL;
int *mainPointer = new int(5);
int *secondPointer = mainPointer;
main_p_to_p = &secondPointer;
// prints 5 5 5
cout << *mainPointer << " " << *secondPointer << " " << **main_p_to_p << std::endl;
testFunc(main_p_to_p);
// prints 5 10 10
cout << *mainPointer << " " << *secondPointer << " " << **main_p_to_p << std::endl;
}
Note that you don't need the main_p_to_p variable, but I left it in for consistency with the question.
I am trying to write a programme to read MRI data by using VTK and C++. But I can't get spacing of MRI raw data in main.
The "GetSpacing" only works in "ReadImageData" function. I think I made some mistake in C++ programming. But I don't know where it is.
vtkImageData* ReadImageData(string mri_imagedata_file)
{
vtkSmartPointer<vtkMetaImageReader> reader =
vtkSmartPointer<vtkMetaImageReader>::New();
reader->SetFileName(mri_imagedata_file.c_str());
reader->Update();
vtkImageData* metaimage = reader->GetOutput();
double sp[3];
metaimage->GetSpacing(sp);
cout << sp[0] << " " << sp[1] << " " << sp[2] <<endl; //<----------It works here.
return metaimage;
}
int main (int argc, char *argv[])
{
if(argc != 2)
{
cerr << "Usage: " << argv[0] << " MRI image data" <<endl;
return EXIT_FAILURE;
}
string mri_imagedata_file = argv[1];// Input "prost00.mhd"
vtkImageData* metaimage = ReadImageData(mri_imagedata_file);
double sp2[3];
metaimage->GetSpacing(sp2);
cout << sp2[0] << " " << sp2[1] << " " << sp2[2] << endl; //<-----It doesn't work here
}
Thank you for your attention.
Assuming vtkSmartPointer<vtkMetaImageReader> is a kind of smart pointer whatever, reader points to in the function is destructed when ReadImageData returns. That includes what metaimage is pointing to.
return metaimage;
// reader->~(); // Destructed here. Including what is pointed to by metaimage.
}
A good solution would be to return the smart pointer instead of metaimage. That way what is pointed to by reader will not be destructed when ReadImageData returns and will be available in main.
My assumption is that vtkMetaImageReader::GetOutput() returns pointer to some vtkMetaImageReader internal data, so when you exit ReadImageData your reader is destroyed and the pointer you return becomes invalid.
It looks like you forgot to pass the array as a parameter to GetSpacing in main, so it calls the overload that returns a double*, leaving your array untouched.
You're also printing an array called spacing although it looks like you want the spacing in sp2.
I found a function to resize an array and am having trouble understanding how it works (or if it's working properly). For testing I set the "temp" array to a new value and "startCounter" does get assigned to that value, however the memory location for startCounter doesn't change. Here is my code:
int * startCounter;
void resizeArray(int *&arraySent,int origSize,int newSize) {
output << "&arraySent " << &arraySent << endl;
output << "arraySent[0] " << arraySent[0] << endl;
int* temp = new int[newSize];
output << "&temp " << &temp << endl;
for (int i=0; i<origSize; i++) {
temp[i] = arraySent[i];
}
temp[0]=744;
delete [] arraySent;
arraySent = temp;
output << "&arraySent " << &arraySent << endl;
}
//....
startCounter = new int [3];
startCounter[0]=345;
output << &startCounter << endl;
resizeArray(startCounter,3,10);
output << "startCounter[0]" << startCounter[0] << endl;
output << "&startCounter" << &startCounter << endl;
Here is the output I get from this:
&startCounter 0x60fab8
&arraySent 0x60fab8
arraySent[0] 345
&temp 0x82cfe54
&arraySent 0x60fab8
startCounter[0] 744
&startCounter 0x60fab8
My question is, why does the memory location of startCounter not change from 0x60fab8 after deleting it and assigning it to the new "temp" array? Shouldn't it now become 0x82cfe54?
P.S. I understand about vectors and such but am mainly concerned with understanding how this particular function works.
void resizeArray(int *&arraySent,int origSize,int newSize) {
output << "&arraySent " << &arraySent << endl;
outputs the address of the pointer variable, not the address that it holds.
simply omit the address operator to get the (probably) intended effect
&startCounter is the address of the pointer, not the address it points to. it's value won't change. simply use startCounter.
I can't seem to find any solution for this.
I have a type 'route' that contains a matrix.if I do:
cout << route << endl;
it works it prints the memory
but if I try
cout << route[1][1] << endl;
program just ends without any error or anything.
debug says:
"(Suspended : Signal : SIGSEGV:Segmentation fault)"
here is the code:
//structure is a type I created
Structure ***route = list->searchRoute(startPoint, destination, time);
//should return a matrix
cout << "Avaible routes: \n" << endl;
for(int i = 0; i < 5;i++)
cout << route[1][1]->startPoint << endl;
Segmentation fault usually implies that you are accessing memory you are not supposed to access. What is probably happening is that our "matrix" is probably too small to have a block in the second row/ second column, so an error is thrown when you try to access that location(because you do not own it). Make sure you are allocating route correctly and at the right size.