 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
fiatlux Guest
|
Posted: Tue Nov 22, 2005 1:17 am Post subject: returning reference by dereferencing a pointer |
|
|
Hello,
Here is the short version of the problem (that might be enough!?):
The use of the following member function causes the program to
segfault:
double& operator[](unsigned int index)
{
double *ret = gsl_vector_ptr(vct_, index); // from gsl library
// std::cout << "foo" << std::endl;
return *ret;
}
The behavior changes with optimization (-O3), and it also doesn't
segfault when I uncomment the line in the middle.
Can someone help me understand what's going on?
Thanks,
Matt
Here is the longer version with the background.
I would like to use the notation m[i][j] = 42.0; to assign a matrix's
element.
I have built a class matrix around the gsl_matrix* struct of the gsl
library.
I have in there a member function
pseudo_vector_t operator[](unsigned int index){...} which returns a
vector corresponding to a row in the matrix.
The pseudo vector class is a wrapper around gsl_vector*, which has a
copy constructor, and overloads the operator[]. The pseudo vector is
just a way to allow the notation m[i][j].
double& operator[](unsigned int index)
{
double *ret = gsl_vector_ptr(vct, index);
return *ret;
}
I've tried the direct solution ( return *gsl_vector_ptr(vct, index); )
but it segfaults too.
I've tried double* const ret = ..., but it fails too
Someone talked to me about the return register pointing out of the
stack, but I don't quite understand what he's talking about, and can't
think of the beginning of an answer.
I'd greatly appreciate help on that matter (I've been very humbled by
that problem!)
Matt
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Stanimir Kabaivanov Guest
|
Posted: Tue Nov 22, 2005 4:05 pm Post subject: Re: returning reference by dereferencing a pointer |
|
|
Hi Matt,
| Quote: |
Here is the short version of the problem (that might be enough!?):
The use of the following member function causes the program to
segfault:
double& operator[](unsigned int index)
{
double *ret = gsl_vector_ptr(vct_, index); // from gsl library
// std::cout << "foo" << std::endl;
return *ret;
}
|
Well, first of all, as far as I'm acquainted with gsl docs,
gsl_vector_prt can also return null pointer (and invoke error handler)
if your index points out of the 0..n-1 vector index range.
Perhaps it would be a nice idea to handle such situation and report an
error (throw an exception). Current code will try to dereference null
pointer otherwise.
| Quote: | The behavior changes with optimization (-O3), and it also doesn't
segfault when I uncomment the line in the middle.
Can someone help me understand what's going on?
|
Is your compiler GCC? with a better optimization (that is larger N after
-0) it can skip part of your code, or rearange it the way it likes, but
I don't belive optimization is your problem.
| Quote: |
Here is the longer version with the background.
I would like to use the notation m[i][j] = 42.0; to assign a matrix's
element.
I have built a class matrix around the gsl_matrix* struct of the gsl
library.
I have in there a member function
pseudo_vector_t operator[](unsigned int index){...} which returns a
vector corresponding to a row in the matrix.
The pseudo vector class is a wrapper around gsl_vector*, which has a
copy constructor, and overloads the operator[]. The pseudo vector is
just a way to allow the notation m[i][j].
double& operator[](unsigned int index)
{
double *ret = gsl_vector_ptr(vct, index);
return *ret;
}
I've tried the direct solution ( return *gsl_vector_ptr(vct, index); )
but it segfaults too.
I've tried double* const ret = ..., but it fails too
|
Have you created (allocated) the vector in question) in accordance with
GSL requirements?
Also how is your GSL compiled/built? Are code generation settings
different from the settings you use for your class?
Best regards,
Stanimir Kabaivanov
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
fiatlux Guest
|
Posted: Tue Nov 22, 2005 7:28 pm Post subject: Re: returning reference by dereferencing a pointer |
|
|
Hello Stanimir,
I don't do much error checking (I will in the future), but I know that
the index is within range. I have verified (printf not shown) that the
index is strictly smaller than vector_->size.
| Quote: |
Well, first of all, as far as I'm acquainted with gsl docs,
gsl_vector_prt can also return null pointer (and invoke error handler)
if your index points out of the 0..n-1 vector index range.
Perhaps it would be a nice idea to handle such situation and report an
error (throw an exception). Current code will try to dereference null
pointer otherwise.
The behavior changes with optimization (-O3), and it also doesn't
segfault when I uncomment the line in the middle.
Can someone help me understand what's going on?
Is your compiler GCC? with a better optimization (that is larger N after
-0) it can skip part of your code, or rearange it the way it likes, but
I don't belive optimization is your problem.
|
I use g++
I looked at the gsl source, and it seems they use '-g -O2'. Using the
same parameters for compilation, I still have the segfault.
| Quote: |
Have you created (allocated) the vector in question) in accordance with
GSL requirements?
Also how is your GSL compiled/built? Are code generation settings
different from the settings you use for your class?
|
I have created the matrix using proper allocation methods
(gsl_matrix_calloc...)
The matrix class returns a pseudo_vector, whose purpose is just to
expose the matrix row in a convenient way.
The pseudo_vector just has a copy constructor, which looks like
pseudo_vector(const gsl_vector* _vector)
{ vector_ = _vector;}
where vector_ is a member variable of pseudo_vector of type gsl_vector*
The only other member functions of pseudo_vector are those {const}
double& operator[] functions that are causing me so much grief.
Thanks for your response,
Cheers,
Matt
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
fiatlux Guest
|
Posted: Wed Nov 23, 2005 1:53 pm Post subject: Re: returning reference by dereferencing a pointer |
|
|
I think the problem lies not in the return reference, but elsewhere
(gsl_matrix_view allocates in the stack and gets out of scope).
Thanks to those who provided comments,
Matthieu
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|