std::cout prevents a segfault in function? - c++

I'm using the finite element library for some calculations, and I've encountered a bizarre problem.
I basically have the following for loop:
MeshBase::const_node_iterator node_it = mesh.nodes_begin();
for (unsigned int i=0;i<n_nodes;i++ , node_it++){
const Node* node2 = *node_it;
Point dumpoint( (*node2)(0), (*node2)(1), (*node2)(2));
Number dumreal= (Number) mesh_data.get_data(node2)[0];
// std::cout << dumreal <<std::endl;
dummap[dumpoint] = mesh_data.get_data(node2)[0];
}
If I uncomment the line with cout, it works. Otherwise I get a segfault. It doesn't matter what I print:
std::cout << std::endl;
An important note is that dummap is a global
std::map<Point,Number>

Using valgrind showed that the problem was with some char* array I allocated somewhere else.
Thanks ^^

Related

Consecutive lines printing same pointer with "std::cout" produce completely different output

I apologize for the vague title of my question. I don't know a better way to phrase it. I've never asked a Stack Overflow question before but this one has me completely stumped.
A method in class Chunk uses the Eigen linear algebra library to produce a vector3f, which is then mapped to a C-style array with the following.
ColPivHouseholderQR<MatrixXf> dec(f);
Vector3f x = dec.solve(b);
float *fit = x.data();
return fit;
This array is returned and accessed in the main function. However, whenever I attempt to print out a value from the pointer, I get completely different results. A sample is below.
Chunk test = Chunk(CHUNK_SIZE, 0, 0, 1, poBand);
float* fit = test.vector; // Should have size 3
std::cout << fit[0] << std::endl; // Outputs 3.05 (correct)
std::cout << fit[0] << std::endl; // Outputs 5.395e-43
std::cout << fit[0] << std::endl; // Outputs 3.81993e+08
What makes this issue even more perplexing is that the incorrect values change when I end the lines with "\n" or ", ". The first value is always the expected value, no matter whether I print index 0, 1, or 2.
I have tried dynamically allocating memory for the fit variable, as well as implementing the code on this answer, but none of it changes this functionality.
Thank you in advance for any guidance on this issue.
Minimally Reproducible Example:
float* getVector() {
Eigen::Vector3f x;
x << 3, 5, 9;
float* fit = x.data();
return fit;
}
int main(void) {
float* fit = getVector();
std::cout << fit[0] << std::endl;
std::cout << fit[0] << std::endl;
std::cout << fit[0] << std::endl;
}
You create the vector x in the function on the stack. It is destroyed after the function exited. Hence your pointer is invalid.
Here an example with shared_ptr
ColPivHouseholderQR<MatrixXf> dec(f);
Vector3f x = dec.solve(b);
shared_ptr<float> fit(new float[3],std::default_delete<float[]>());
memcpy(fit,x.data(),sizeof(float)*3);
return fit;
Another possible way is
ColPivHouseholderQR<MatrixXf> dec(f);
Vector3f x = dec.solve(b);
return x;

Run-time error, using undeclared variable, C++

Im having this really weird error during run-time.
My program takes two parameters, does some math calculus with them and std::cout's the values in the end.
My program works if i input somevalues, but if i input other values it says that a variable is being used without being initialized, which i think it makes no sence.
Here is the code:
#include <iostream>
#include <stdio.h>
#include <cmath>
double align_nb(int n) { return { ceil(n / 512.0)*512.0 }; } // bytes
double align_pt(int k) { return { floor(k / 512.0)*512.0 }; } // pointer
int main(int argc, char * argv[])
{
int o_n = std::atoi(argv[1]); // original number
int o_p = std::atoi(argv[2]); // original pointer
int max_bytes, new_pointer; // max bytes to read, new pointer to point
float log = (std::log(o_n) / std::log(2));
if (log != floor(log))
{
max_bytes = align_nb(o_n); // bytes alinhados para a frente
new_pointer = align_pt(o_p); // ponteiro alinhado atrĂ¡s
}
else if (log == floor(log))
{
new_pointer = align_pt(o_p);
if (max_bytes + (o_p - new_pointer) >max_bytes)
{
max_bytes += 512;
}
}
std::cout << "Original bytes= " << o_n << std::endl;
std::cout << "Original pointer= " << o_p << std::endl;
std::cout << "Max_bytes= " << max_bytes << std::endl;
std::cout << "new_pointer= " << new_pointer << std::endl;
return 0;
}
Here are the values i tested it and it crashed, giving me that run-time error:
2048 513
1024 500
here is one example of values were the code doesnt give me that error and the program works:
513 520
Here is a print of the error it gives me.
I'd really appreciate someone explaining me why it gives me that error/how to fix it.
Regardless, thanks!
(PS: math tag is included cause it could be the math in the program that is causing it to crash. If annyone thinks it shoudlnt be used in this question, let me know in the comments and ill remove it.)
(PS 2: the variable it complains when it gives me the run time error is 'max_bytes'.)
If your code takes the else path at line 17, then your code doesn't initialize max_bytes, but uses it afterwards. That's the problem.
Notes:
comparing calculated floating point values for equality usually a bad practice
you don't need the additional if at line 23.
Make sure that for each path your code takes the values of the variables you use are initialized. If you don't you get what is called Undefined Behaviour. There could be anything in an uninitialized variable.
int max_bytes;
....
....
expression_involving_max_byte <- Dangerous!

Initializing a 2D Array within a function, calling by pointer. Getting Segmentation Fault

The objective of this program is of right now to set each array variable of the 2D array,
char mass_data_shift[9][9]; equal to 'T' . Which should equal 100 Ts overall.
This is done by calling a void function with this 2D array address as an argument. Calling it by a pointer to then be initialized in a loop.
Inside the loop is were the 2D array should be set to T.
*mass_data[mass_data_counter][mass_data_counter_two] = 'T';
However..... the program results in:
(Most Often) A Segmentation Fault after trying to initialize *mass_data[4][2]
(Sometimes) A Segmentation Fault after the program successfully(?) runs.
(Sometimes) The program successfully runs.
Meaning the program, somewhere, is writing out of bounds. Any help would be appreciated in both making the program run without a segmentation fault and/or fixing other mistakes.
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
void mass_data_define(char (*mass_data)[9][9]){
int mass_data_counter;
int mass_data_counter_two = 0;
for (mass_data_counter=0;mass_data_counter<9;mass_data_counter++){
do {
std::cout << "|Array #1 " << mass_data_counter
<< " ::: |Array #2 " << mass_data_counter_two
<< std::endl;
*mass_data[mass_data_counter][mass_data_counter_two] = 'T';
std::cout << *mass_data[mass_data_counter][mass_data_counter_two];
mass_data_counter_two++;
std::cout << "---------End of Counter 2 Init Code----------" << std::endl;
} while (mass_data_counter_two < 9);
mass_data_counter_two = 0;
}
}
int main()
{
char mass_data_shift[9][9];
mass_data_define(&mass_data_shift);
std::cout << "-END-" << std::endl;
return 0;
}
Final Edit: The main cause was solved by szym below. Sorry about the whitespaces and missing iostream , was a formatting issue when I made the post. Also changed the loop to fit the array length as suggested below.
*mass_data[mass_data_counter][mass_data_counter_two] = 'T';
Should be
(*mass_data)[mass_data_counter][mass_data_counter_two] = 'T';
Naturally, same goes for the line:
std::cout << *mass_data[mass_data_counter][mass_data_counter_two]
But really this pointer type is not necessary to pass array by reference in C/C++.
You should instead declare:
void mass_data_define(char mass_data[9][9]) {
// To read:
char z = mass_data[3][6];
// To write:
mass_data[2][1] = 'C';
}
// elsewhere
char my_mass_data[9][9];
mass_data_define(my_mass_data);
Short Answer: Here's a one-line fix to your code so may continue to use a pointer and still not get a segmentation fault:
(*mass_data)[mass_data_counter][mass_data_counter_two] = 'T';
Long Answer: Read Create a pointer to two-dimensional array.
For an in-depth understanding of how to pointers to access multi-dimensional arrays, read this.

Random behavior with Pointer de-referencing in C++

I have the following code:
#include <iostream>
using namespace std;
int main ()
{
int myvar = 5;
int * p;
cout << "Hello2" << endl;
*p = myvar;
cout << "Hello" << endl;
cout << p << endl;
//cout << &myvar << endl;
}
I know I am not doing the right thing by not initializing the pointer. I was just playing with pointers and noticed this. The issue is when I comment out the last line, the program executes normally. But as soon as I uncomment the line, I get a segmentation fault. I don't know why printing address of myvar is causing this? Has myvar been modified in any way because of pointer dereferencing? I am using C++11.
int* p;
*p = myvar;
You are creating an uninitialized pointer and then derferencing that pointer. This has undefined behavior because p has to point to something for it to be derferenced correctly. Therefore your program's behavior can't be reasoned with.
Segmentation Fault occurs when trying to access a virtual memory address that has no read permissions.
In your case, the local variable p holds uninitialized garbage from the stack.
you are dereferencing a memory address that might not be readable(e.g no read permissions, hence the segmentation fault when trying to access it).
I'm not entirely sure the purpose of your snippet, but the following code will work, and perhaps it will help:
int myvar = 5;
int *p = nullptr;
p = &myvar;
cout << myvar << endl;
cout << &myvar << endl;
cout << p << endl;
cout << *p << endl;
(Note: I used two lines for setting 'p' because that is how you did it in your snippet. You could easily just use: int *p = &myvar; )
Anyway, there are scope issues here as p will only be valid as long as myvar is in scope; however, this does illustrate the basics of pointers. myvar and *p will return the same value (the value being pointed to), and &myvar and p will return the same value (the location of value in memory.)

Strange behaviour with std::vector

consider this segment of codes:
std::vector<int> vecList;
...populate 3 elements into vecList...
if (!vecList.empty())
{
std::cout << "List count = " << vecList.size() << std::endl;
if (vecList.empty())
{
std::cout << "List is empty" << std::endl;
}
}
my printout is:
List count = 3
List is empty
I did not do anything to "vecList" other than printing out, yet after I printed the size of the vector, the size becomes 0. How is that possible? Any advice is appreciated.
This happened when I am running my build on an Iphone environment.
Thanks
Alexander
Since both std::vector<>empty() and std::vector<>::size() are const member functions and cannot alter the vector's content, the only ways I can see to get that result is by using multiple threads or invoking Undefined Behavior.
Likely candidates are other threads modifying the vector and messing up the vector's internals by buffer overflows and the like.
This
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> vecList;
vecList.push_back(1);
vecList.push_back(2);
vecList.push_back(3);
if (!vecList.empty())
{
std::cout << "List count = " << vecList.size() << std::endl;
if (vecList.empty())
{
std::cout << "List is empty" << std::endl;
}
}
return 0;
}
prints List count = 3 for me. I bet it does the same for you. If so, then there must be something messing things up in the code you don't show.
The only way to find out what it is (other than posting the exact right snippet here and have someone guess right) is to remove all extra code step by step until the problem disappears and then look at the code that triggered it.
You might also want to try Valgrind. Allot of times use of uninitialized values, especially with system calls can cause truly weird behavior.
For instance, a common mistake is the following ( yeah I've made this mistake myself ):
struct timeval tv;
tv.tv_sec = 5;
// This is supposed to sleep for 5 seconds
select(0, NULL,NULL,NULL, &tv);
What's missing? You need to init the second member of the struct as such tv.tv_usec = 0; otherwise it can cause seemingly random errors in completely unrelated sections of the program. Valgrind can help you catch some of these things.
Try debuging your code. It is easy and you will understand when exactly it becomes empty. Though, there is nothing ideal, anyway trust such constructs like STL and other well-kown libraries. In 99.99% cases the cause of the promblem is the programmer.
Looks your code like this? Note the semicolon after the second if.
std::vector<int> vecList;
...populate 3 elements into vecList...
if (!vecList.empty())
{
std::cout << "List count = " << vecList.size() << std::endl;
if (vecList.empty());
{
std::cout << "List is empty" << std::endl;
}
}
If this actually happens, it's most probably another thread modifying the vector.