473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Pointer problem: compiles but crashes

I've been fooling around with a test class and meanwhile I stumbled on a
pointer problem.

I have two classes, one being nested in the other. I've wrote a method in
the nested class that would set a nested class's member pointer as a
pointer to an instance of the other class. Here is the code:

<code>
template <class tData>
class Foo
{
* * * * // snip
public:
class Bar
{
protected:
Foo<tData> *m_cursor;
public:
//snip
void point_to(Foo<tData> &);
}; * *
};

// snip

template <class tData>
void Foo<tData>::Bar::point_to(Foo<tData> &pointed_to)
{
this->m_cursor = &pointed_to;
}
</code>
This code compiles under GCC without a single error or warning, even with
the -pedantic and -Wall flags.

The problem lies when I run the following test cod:

<code>
int main(int argc, char *argv[])
{
Foo<int> a;
Foo<int>::Bar b;
* * * *
b.point_to( a);
return EXIT_SUCCESS;
}
</code>
That code also compiles withou a single problem. Yet, when I run the test
application, it crashes and the following message pops up:

<shell>
*** glibc detected *** free(): invalid pointer: 0xbfb01a10 ***
/bin/sh: line 1: *9703 Aborted * * * * * * * *./mytree
Press Enter to continue!
</shell>
I can't see where the problem lies. Not only does the code compile but I
also the class points it's member pointer to an already declared variable.
I can't see what's going wrong in here.

So, can anyone shed some light on this problem?
Thanks in advance
Rui Maciel
--
Running Kubuntu 5.10 with KDE 3.5.1 and proud of it.
jabber:ru********@jabber.org
Feb 13 '06 #1
2 1845
Rui Maciel wrote:
I've been fooling around with a test class and meanwhile I stumbled on a
pointer problem.

I have two classes, one being nested in the other. I've wrote a method in
the nested class that would set a nested class's member pointer as a
pointer to an instance of the other class. Here is the code:

<code>
template <class tData>
class Foo
{
// snip
public:
class Bar
{
protected:
Foo<tData> *m_cursor;
public:
//snip
void point_to(Foo<tData> &);
};
Is this really all there is?
};

// snip

template <class tData>
void Foo<tData>::Bar::point_to(Foo<tData> &pointed_to)
{
this->m_cursor = &pointed_to;
}
</code>
This code compiles under GCC without a single error or warning, even with
the -pedantic and -Wall flags.
There is nothing to compile. It's a template definition. Unless you
actually try to instantiate it, all the compiler does is checking the
syntax.

The problem lies when I run the following test cod:

<code>
int main(int argc, char *argv[])
What's the point of having 'argc' and 'argv' if you're not using them?
{
Foo<int> a;
'Foo' is undefined.
Foo<int>::Bar b;
'Bar' is undefined.

b.point_to( a);
return EXIT_SUCCESS;
'EXIT_SUCCESS' is undefined.
}
This does not seem to be a complete program. Perhaps if you posted the
right code, we could actually be on the same page...
</code>
That code also compiles withou a single problem. Yet, when I run the test
application, it crashes and the following message pops up:

<shell>
*** glibc detected *** free(): invalid pointer: 0xbfb01a10 ***
/bin/sh: line 1: 9703 Aborted ./mytree
Press Enter to continue!
</shell>
'free'? I didn't see any call to 'free' in your code. Neither did I see
any call to 'new' or anything like that. You're not showing the whole
code.
I can't see where the problem lies.
Neither can I. Most likely because you didn't post the code that actually
has the problem.
Not only does the code compile but I
also the class points it's member pointer to an already declared variable.
I can't see what's going wrong in here.

So, can anyone shed some light on this problem?


Post the _complete_, _compilable_, minimal code that demonstrates the
problem as you describe it. Then we can talk.

V
--
Please remove capital As from my address when replying by mail
Feb 13 '06 #2
I've followed your advice and I started chopping up my source code to
post a working example. While doing it, I've stumbled into what I
believe was the cause of the problem: a forgotten delete statement in
the Bar class' destructor...

Better look more closely next time
Once again thanks for the help, Victor
Rui Maciel

Feb 13 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Harry | last post by:
Hi all, I am unable to understand the difference in the following codes: First Example void func(char **ch) { char *ptr; ptr=new char;
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
12
by: Andreas Schmidt | last post by:
I am trying to understand the behavior of void pointers. Can someone explain to me why I get a segfault for the following program? #include <stdio.h> void* plusone(void* i){ int* arg =...
5
by: shan_rish | last post by:
Hi Group, When i compile the following program it compiles successfully, but crashes while executing. I am trying to assign a NULL char pointer to a string. The error message is ...
6
by: Simple Simon | last post by:
[posted separately to comp.unix.programmer and Can't figure out why this code generates a SIGSEGV in fillGlobalBuffer() (below). globalBuffer is allocated in caller.c, then its address is...
8
by: nsharma78 | last post by:
Hi, I have a code as follows: class A { public: void print(){cout << "Magic" << endl;} };
49
by: elmar | last post by:
Hi Clers, If I look at my ~200000 lines of C code programmed over the past 15 years, there is one annoying thing in this smart language, which somehow reduces the 'beauty' of the source code...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
4
by: Milan Krejci | last post by:
pocet_prac=new int; *pocet_prac=0; std::vector <SD >::iterator i; for (i=vec_prace->begin();i!=vec_prace->end();i++) { from=(*i).vrat_from(); to=(*i).vrat_to();...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.