0
101 Oct 02, 2006 at 21:30

So, I’ve been working on my LISP interpreter… I got it to compile on linux… And then… wham… segmentation fault. It works fine on windows, and I was able to trace the bug… To a bug in G++.

It seems that std::vector::resize() does NOT resize the vector. I am creating a vector of objects, and I then call resize, but its size still shows up as 0, and when I try to access the first element, I get a seg fault. I know that this is indeed a bug in G++ because it’s the second time I encounter it. The last time was on a different machine, on a different project, and I had reliably traced the bug to the same cause…

Yet, as much as I know alot of people use G++, and alot of C++ programmers use std::vector, nothing shows up anywhere on google about this problem! I’m using G++ 4.0.2, and well, the newer versions don’t seem to mention a patch for this in their changelogs… What the hell is going on? Can anybody with G++ on a linux machine try the vector resize function? Has anyone heard of this?

41 Replies

0
167 Oct 02, 2006 at 22:22

The following compiles and gives the correct output for me, with g++ 3.3.5:

#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> vec;
cout << "vec.size() == " << vec.size() << endl;
vec.resize(10);
cout << "vec.size() == " << vec.size() << endl;
return 0;
}


The output is:

vec.size() == 0
vec.size() == 10


Maybe you could test the above program and see what result you get?

0
101 Oct 02, 2006 at 22:50

A common mistake is to store a reference or a pointer to an element, when you do a resize this invalidates them.
For example:

vector<int> vec;
vec.resize(10);
int &a = vec[0];
vec.resize(100);


a is undefined

0
101 Oct 03, 2006 at 00:10

@dave_

A common mistake is to store a reference or a pointer to an element, when you do a resize this invalidates them.
For example:

vector<int> vec;
vec.resize(10);
int &a = vec[0];
vec.resize(100);


a is undefined

I’m not that newbish ;)

I actually found what the problem was. One of my classes was storing a static instance of an allocator, whose constructor initializes a pool (vector) of allocation units. But another class had another static member who required one of those units to be allocated before the constructor of the allocation pool was actually called.

0
101 Oct 19, 2006 at 08:12

An advice: Do not tell people how good a programmer you are unless they ask. At devmaster there are some very skilled programmers which makes the context of a good programmer very loose.

Just a friendly advice if you want to be taken serious.

0
101 Oct 19, 2006 at 11:22

@GroundKeeper

An advice: Do not tell people how good a programmer you are unless they ask. At devmaster there are some very skilled programmers which makes the context of a good programmer very loose. Just a friendly advice if you want to be taken serious.

0
101 Oct 19, 2006 at 12:42

Ahh, the dreaded static ordering problem of that damned beautiful language we all have a love and hate relationship with. Nyx, What technique are using alleviate your issue? Just changed the order or you using some kind of nifty trick or something…

For anyone who’s interested here’s what faq lite has to say about this stuff. Several “solutions” are provided as well.

0
101 Oct 19, 2006 at 17:18

Ahh, the dreaded static ordering problem of that damned beautiful language we all have a love and hate relationship with. Nyx, What technique are using alleviate your issue? Just changed the order or you using some kind of nifty trick or something… For anyone who’s interested here’s what faq lite has to say about this stuff. Several “solutions” are provided as well.

I changed the order the object files get passed to the gcc linker and that fixed it. There seemed to be no other way to fix this problem without bastardizing my code to unacceptable levels!

0
101 Oct 19, 2006 at 19:28

As a matter of fact, your code is already bastardized because you didn’t take order of initialization into account in the first place :huh:

Your fix works now, but what if you compile your code with another compiler? Or another version of the same compiler even? It’s an issue that is bound to hunt you until you make a proper standard C++ fix for it

0
101 Oct 21, 2006 at 11:09

@.oisyn

As a matter of fact, your code is already bastardized because you didn’t take order of initialization into account in the first place ;) Your fix works now, but what if you compile your code with another compiler? Or another version of the same compiler even? It’s an issue that is bound to hunt you until you make a proper standard C++ fix for it

Well, it works on GCC and MSVC, which is what I care about. If it doesn’t work on another compiler, the people who care can make a new makefile for that other compiler. As far as I’m concerned, my code is in a state where its rather clean and easy to understand. I agree that the problem stems from the C++ standard… I don’t think there should be such a thing as “undefined behavior” in a programming language… I know of at least two other cases in C++…

0
101 Oct 21, 2006 at 20:47

Actually almost every language has this problem, not just C++. And how would you standardize the order of construction? By ordering the filenames alphabetically? That’s just as arbitrary as any order you can think of.

You’d better learn how to deal with this from the ground up so you won’t run into these problems again :huh:

0
101 Oct 21, 2006 at 21:56

@.oisyn

Actually almost every language has this problem, not just C++. And how would you standardize the order of construction? By ordering the filenames alphabetically? That’s just as arbitrary as any order you can think of. You’d better learn how to deal with this from the ground up so you won’t run into these problems again :)

Actually, any order is good enough, if you can rely on it.

0
101 Oct 21, 2006 at 22:24

@Nyx

As far as I’m concerned, my code is in a state where its rather clean and easy to understand.

You swept an broken design under the rug. Your code as it stands is buggy code that just happens to work. If you try to do that in any serious programming job you’d be fired.

0
101 Oct 21, 2006 at 22:26

@Jare

You swept an broken design under the rug. Your code as it stands is buggy code that just happens to work. If you try to do that in any serious programming job you’d be fired.

ROFL.

I’d like to see you program a working, complete, extensible and stable LISP interpreter with a design half as clean as mine.

I don’t see what’s so bad about what I did. This problem results from a deficienty. in the C++ design, and I responded to it with a compiler-specific solution. Anyways, that statement shows you don’t know much about the programming job market ;) Microsoft doesn’t fire half its staff every year.

0
101 Oct 21, 2006 at 22:38

@Nyx

Actually, any order is good enough, if you can rely on it.

You didn’t quite get my point. What order would you choose, and why? And by the way, it would suit you to not be so offended by what others have to say. Clearly your design isn’t as clean as you claim it to be, otherwise you wouldn’t have had these problems. Jare makes a fair point. And I’m not talking about the getting fired part (which sadly enough is indeed not always true).

0
101 Oct 21, 2006 at 23:17

@.oisyn

You didn’t quite get my point. What order would you choose, and why? And by the way, it would suit you to not be so offended by what others have to say. Clearly your design isn’t as clean as you claim it to be, otherwise you wouldn’t have had these problems. Jare makes a fair point. And I’m not talking about the getting fired part (which sadly enough is indeed not always true).

I get one bug and my design is good for the trash? Considering it’s pretty much the only bug I’ve had, and I found a way to fix it pretty quickly, I’d say my design is pretty good.

As far as the order I would choose, I would choose the order of inclusion, or of declaration, because its the most obvious.

The only point Jare makes is that he’s an arrogant and offensive <insert word of your choice>. Honestly, his comment was rather insulting, and I don’t really have to prove anything about my programming skills. I don’t judge your programming practices, so don’t judge mine, because I’ve most likely been programming in C++ longer than you have, on a much higher scale than you have. Kthxbye.

As far as “sweeping a broken design under the rug goes”. Let me put it this way. My original code didn’t even compile on the latest microsoft compiler. And by that, I don’t mean it would report programming errors, I mean I got several “fatal compiler error” messages doing it. I’ve had to resort to some degree of code bastardization to get Visual C++ to compile it at all. As far as I’m concerned, the compiler is broken, so don’t blame me for refusing to make my code crappier.

0
101 Oct 21, 2006 at 23:29

The point Jare makes is that you temporarily hid a fault in your design. The fix you have is just a temporary one. When you add another object in another file that uses the static object before it’s inisitalized you’ll have the same problem (again). You havent fixed anything, you just hid it. That was the point.

No one said your design is good for the trash. If you can’t take criticism you’re going to have bigger problems then unknown static initialization order though.

0
101 Oct 21, 2006 at 23:34

The point Jare makes is that you temporarily hid a fault in your design. The fix you have is just a temporary one. When you add another object in another file that uses the static object before it’s inisitalized you’ll have the same problem (again). You havent fixed anything, you just hid it. That was the point.

Well, these two classes are part of the core of the interpreter, which is very much complete. The way it’s designed, there is no reason for that situation to arise again… And well, chances are, this bug will never surface again. Unless there is some huge change in GCC, which isn’t likely. But it could happen? Sure it could, but then it’s actually more likely some change in GCC will cause problems with some other, perfectly legit part of the code.

No one said your design is good for the trash. If you can’t take criticism you’re going to have bigger problems then unknown static initialization order though.

I have no problem with *constructive criticism*. I do have a problem with insults, however. I also have a problem with people who have nothing to show yet act like they know more than everyone else about everything, and people who pretend they can tell everyone else how to program. I don’t criticize the fact that Jare’s code is badly commented, of very poor overall quality, and that it looks like it was written by a C programmer who never fully understood C++ (I just checked his homepage!).

My point stands: I’ve made a thread to discuss my LISP interpreter a while ago, and none of you commented on any aspect of the product itself. You’re only here now, to post negative criticism about a bug that’s been solved over two weeks ago. Not because you want to help, but because you feel the need to believe you’re better than someone else at something. Honestly… I’m sure people tell me I should be fired because they have a very strong altruistic need to help others a là mother Teresa.

Since this thread is going nowhere fast, I suggest locking it.

0
101 Oct 22, 2006 at 00:35

@Nyx

As far as the order I would choose, I would choose the order of inclusion, or of declaration, because its the most obvious.

You don’t include source files, and order of construction within a sourcefile is very well-defined. When working with static constructor code (e.g., all code that get’s executed before main()), you have to make sure everything’s constructed in the right order. So create accessors for your global variables (you shouldn’t use global variables anyway) so you can ensure construction order. And this goes for ANY imperative language, not just C++.

The only point Jare makes is that he’s an arrogant and offensive <insert word of your choice>.

No, you just don’t seem to be able to take any form of critisism. It shows from almost every reply in your thread. And quite frankly, that makes YOU the arrogant person, not others.

Honestly, his comment was rather insulting, and I don’t really have to prove anything about my programming skills.

No you don’t, and nobody’s asking you to. But you seem to think that you do.

because I’ve most likely been programming in C++ longer than you have, on a much higher scale than you have. Kthxbye.

Another typical arrogant remark. Btw, experience alone doesn’t make you a good programmer. DISCLAIMER: I’m not saying that you are a lousy programmer (somehow I think this disclaimer is a necessary, given your earlier reactions), I’m just saying that “I’ve been coding way longer than you are” is just not a good argument.

However, the fact that you didn’t knew this bug could happen and that you didn’t take that into account in your design, and by the way you solved it, I can’t help to wonder how good a programmer you really are, or how much so called experience you’re actually having with C++. You just keep rambling about how it’s C++’s fault. No, it’s not the language’s fault, it’s your fault.

As far as “sweeping a broken design under the rug goes”. Let me put it this way. My original code didn’t even compile on the latest microsoft compiler. And by that, I don’t mean it would report programming errors, I mean I got several “fatal compiler error” messages doing it. I’ve had to resort to some degree of code bastardization to get Visual C++ to compile it at all. As far as I’m concerned, the compiler is broken, so don’t blame me for refusing to make my code crappier.

I’m sorry but this simply has got nothing to do with the problem you where having. Don’t make arguments that have no relevance to the discussion.

0
101 Oct 22, 2006 at 00:42

I’m side-stepping the whole arrogant/criticism thing here, so pardon me for poking my head in.

Nyx, you may already be aware of this, but if you want to get a ‘set’ order of initialization, try using a singleton pattern. If you end up doing this for your static accesses to global data, you’ll ensure that something is appropriately set the first time through.

That said, I’m not sure if that will help, since there’s no code provided, but reading the discussion this sounds like it might help.

0
101 Oct 22, 2006 at 00:46

@eddie

I’m side-stepping the whole arrogant/criticism thing here, so pardon me for poking my head in.

Nyx, you may already be aware of this, but if you want to get a ‘set’ order of initialization, try using a singleton pattern. If you end up doing this for your static accesses to global data, you’ll ensure that something is appropriately set the first time through.

That said, I’m not sure if that will help, since there’s no code provided, but reading the discussion this sounds like it might help.

0
101 Oct 22, 2006 at 10:04

@Nyx

Considering it’s pretty much the only bug I’ve had, and I found a way to fix it pretty quickly, I’d say my design is pretty good.

You haven’t fixed it, you have simply prevented it from making your program crash. Call it the programming equivalent of a band-aid or a tourniquet.

I have no idea if the rest of the program is well designed or not. Knowing that you are willing to “fix” design issues this way, there’s reason to think you have broken your design in other places as well, because it is a problem of philosophy, not of knowledge.

BTW you advertise your code as “Zero dependencies outside the standard C++, 100% portable!” You should remove that point until it is true.

0
101 Oct 25, 2006 at 08:10

@Nyx

You’re only here now, to post negative criticism about a bug that’s been solved over two weeks ago. Not because you want to help, but because you feel the need to believe you’re better than someone else at something.

Sorry, I’m with them. It wasn’t solved. I don’t pretend to be a good programmer (for my age maybe, but not in general) and I wouldn’t consider reordering the compilation a fix. I threw out a chunk of game engine because it suffered from static [de]initialisation order problems, partly due to poor choice of external libraries.

And they are trying to help, they’re trying to bash into you that it’s bad. You don’t have to fix it, of course, that’s your own decision. But it would be a good idea. Here’s a reason you may be able to relate to. Say I want to compile your LISP interpreter, but I use a different compiler to you. No problem, it’s portable, so I write my own make file, and guess what: it doesn’t work. It looks like there’s a problem with the standard library vector. Now this might be a fairly obscure platform, so rather than contacting you, I contact whoever implemented the standard library. They have no time/clue. Much time is wasted, and I eventually give up on getting your program to run.

One thing you really shouldn’t do, no matter how experienced, (I believe this is fairly well known) is jump to the conclusion that the fault lies with the compiler/language. It often doesn’t, even if it looks like it does. It’s much better to say “what am I doing wrong?” and be told “nothing, it’s actually a compiler error” than to say “this frigging copiler is broken” and be told something along the lines of “no, you’re retarded”.

0
101 Oct 25, 2006 at 17:40

@Nyx

I don’t judge your programming practices, so don’t judge mine, because I’ve most likely been programming in C++ longer than you have, on a much higher scale than you have. Kthxbye.

How can you say something like that when you’re a third year university student? You are completely disconnected from reality! Maybe you’re good compared to the other students, but you have no real professional experience in programming. You are the typical arrogant student, who has not yet realized how much left he has to learn. The first step would be to accept criticism and realize your mistakes.

0
101 Oct 25, 2006 at 17:56

:huh: thanks for the laugh!

0
101 Oct 26, 2006 at 00:11

In between name callings, i haven’t seen any reference to this useful attribute, http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html#C_002b_002b-Attributes

And please let the singleton pattern rest in peace in these multithreading days & age.

0
101 Oct 26, 2006 at 00:59

@Faelenor

How can you say something like that when you’re a third year university student? You are completely disconnected from reality! Maybe you’re good compared to the other students, but you have no real professional experience in programming. You are the typical arrogant student, who has not yet realized how much left he has to learn. The first step would be to accept criticism and realize your mistakes.

Avoid making judgements. You don’t really know who you’re talking to. As a matter of fact, I do have professional experience, whatever you put that up to be. You simply don’t know much about all the things I have done already, as a third year university student.

I am not arrogant. I simply know and appreciate my own talent. I was hoping this wouldn’t turn into a “who can pee the farthest” competition… but if you insist…

How many of you can claim to have made a LISP interpreter?

A 3D Global Illumination Renderer?

A 3D GI Renderer that distributes its processing load accross the net?

A complete 3D Game Engine with networking and physics?

How many have implemented their own virtual RISC VM?

How many can claim to have won a prize for their research work?

And how many did all that or more while maintaining good enough grades to win several scholarships and caming out to their family as gay or transsexual?

It’s simple. I have alot of crap to deal with all the time, but I still manage to do well. I consider myself to be talented. Is that wrong? All I ask for is a minimal amount of respect. You might disagree with some of my programming practices, but does it affect you? Not really. Stop pretending you’re trying to help someone when all you want is to try to make yourself look smarter. You may say I’m not being modest, but if you read your own posts, you’ll find you aren’t either. You talk alot, but do very little. I do both.

0
101 Oct 26, 2006 at 01:11

If you’re such a hotshot, why did you need to make a fool of yourself referring to a classical C++ ctor issue as a g++ bug?

Does not compute.

0
101 Oct 26, 2006 at 01:19

@tbp

If you’re such a hotshot, why did you need to make a fool of yourself referring to a classical C++ ctor issue as a g++ bug? Does not compute.

Read the post. Because I’ve actually faced a case where a similar problem was due to a faulty std::vector implementation. And as far as “making a fool” of myself goes, I did find the cause a few minutes later… So seriously, nobody’s perfect, so quit it before I go criticize every thread you posted for no reason… Just kidding, I actually have a life, I wouldn’t bother trying to make other people look bad in the hope of making myself look smarter.

0
101 Oct 26, 2006 at 01:30

@Nyx

Read the post. Because I’ve actually faced a case where a similar problem was due to a faulty std::vector implementation. And as far as “making a fool” of myself goes, I did find the cause a few minutes later…

10-02-2006, 10:30 PM / 10-03-2006, 01:10 AM. In that vein i guess WWII also ended a few minutes ago.

Anyway the thing is, being the self proclaimed hotshot you are, you should have known you were heading for a ctor ordering mess beforehand. Period.

I wonder why i’ve tried to contribute to this thread. At least those empty threats are amusing.

0
101 Oct 26, 2006 at 01:37

@tbp

10-02-2006, 10:30 PM / 10-03-2006, 01:10 AM. In that vein i guess WWII also ended a few minutes ago.

Lol. If I have to justify the delay as well. I essentially forgot about this thread until I checked the forum later on.

Anyway the thing is, being the self proclaimed hotshot you are, you should have known you were heading for a ctor ordering mess beforehand. Period.

I haven’t planned my LISP interpreter design at all. It was a “design as I go” project. Just me, my IDE, and the quest to end up with a working LISP interpreter. I had to go through numerous refactorings to eliminate some potential issues with the environment memory allocation and such (one of which involved implementing garbage collection). So since my only source of documentation was my memory of the project and the code itself, I haven’t foreseen everything. It was fun to code, for the most part, which was the main goal.

In a sense you could say that’s a “bad” way to design things, but the fact is, I couldn’t have predicted everything ahead of time. The code is simply too complex. I believe my “code relaxation” approach has actually allowed me to end up with a very natural design for what I wanted in the end.

0
101 Oct 26, 2006 at 01:42

Do you really think piling up excuses will get you anywhere at this point? :blink:
edit: LISP interpreter? too complex? /me faints.

0
101 Oct 26, 2006 at 01:48

@tbp

Do you really think piling up excuses will get you anywhere at this point? :blink:
edit: LISP interpreter? too complex? /me faints.

???

Do you really need to keep replying at this point?

Have you ever programmed a LISP interpreter? Implementing one *properly*, from scratch, is not so trivial.

0
101 Oct 26, 2006 at 02:04

Since i’ve already contributed a topical answer and that thread has long derailed, i feel entitled to have fun with you.

Now, is there a single CS student who hasn’t implemented a bunch of interpreter/compiler? I think not. Otherwise his licence needs to be revoked ASAP.

So it’s neither trivial nor complex, or original.

0
101 Oct 26, 2006 at 02:09

@tbp

Since i’ve already contributed a topical answer and that thread has long derailed, i feel entitled to have fun with you.

Now, is there a single CS student who hasn’t implemented a bunch of interpreter/compiler? I think not. Otherwise his licence needs to be revoked ASAP.

So it’s neither trivial nor complex, or original.

I fail to see your point. The only mandatory CS class in which “I” had to implement an interpreter was a Scheme class. They had us implement a scheme-ish language in scheme, which was indeed trivial… And well, I say “I” had to implement it because, for the most part, it was a matter of filling in the small gaps in code the teacher gave to us… So in short, nope, I never really *had* to do this as a CS project, and neither did anyone in my uni. There is also quite a difference between doing this in C++ versus doing it in LISP.

I’m taking an optional 500 level compilers class this semester, and we’re going over a simple project. It’s actually a web framework language. Again, alot of the implementation work is already done/explained in detail. So I don’t know how much CS students really do. I’ve made my LISP interpreter from nothing, on my own, before I took my compilers course.

Since you haven’t even checked out my LISP interpreter, I have to call BS on what you say anyways. XLSE is not like any LISP interpreter. It allows you to easily change the language in any way you like by defining your own functions, macros and primitives in C++, which can instantly be used within the language. This is not true of just any LISP interpreter.

But then again. I’ll ask. Since you like to say I’ve done so little. What have you done that is so great?

0
101 Oct 26, 2006 at 02:29

Nobody but you cares about your exact cursus. If nowadays, on your continent, you can get some CS degree without going through such classical patterns, then you should sue your university. That’s basic mandatory knowledge in my book.

Do you really expect me to dig through your ground breaking code? You can’t be serious. It’s a freaking LISP interpreter. Lisp! Interpreter! FFS!
And it’s not like you’ve shown you had spectacular command of the C++ language.
Kids these days…

0
101 Oct 26, 2006 at 02:41

@tbp

Nobody but you cares about your exact cursus. If nowadays, on your continent, you can get some CS degree without going through such classical patterns, then you should sue your university. That’s basic mandatory knowledge in my book. Do you really expect me to dig through your ground breaking code? You can’t be serious. It’s a freaking LISP interpreter. Lisp! Interpreter! FFS!
And it’s not like you’ve shown you had spectacular command of the C++ language.
Kids these days…

Well, since you admit that you haven’t checked it out, your claim that it is “neither trivial nor complex, or original” is definitely misplaced. You’ve simply shown once more that I was right. If you think LISP means simple, take a look at CMUCL, or the Common LISP spec… FFS.

And well, since you’ve avoided my question about your own achievements with yet another try at weak criticism, I will simply regard you as yet another weak programmer who thinks he can show everyone how to do things.

0
101 Oct 26, 2006 at 02:48

@Nyx

Avoiding the question. How weak ;)

Unless you’re a customer i couldn’t care less about how much you value my software. You’re obviously not in such a position.

You’ll find software i’ve liberated just by checking my profile (or tracking patches around). It’s that simple.

edit: Get a clue. Lisp isn’t particularly intricate, and writing an interpreter is basic knowledge.

0
101 Oct 26, 2006 at 02:51

@tbp

Unless you’re a customer i couldn’t care less about how much you value my software. You’re obviously not in such a position. You’ll find software i’ve liberated just by checking my profile. It’s that simple.

I checked it. The kind of things I was programming back in high school ;) Cute.

0
101 Oct 26, 2006 at 03:00

Fascinating. You mean you’ve implemented stuff years before they got published? You should claim a Nobel. Or two. At least.

0
101 Oct 26, 2006 at 03:01

@tbp

Fascinating. You mean you’ve implemented stuff years before they got published? You should claim a Nobel. Or two. At least.

I don’t think Nobel prizes are given in CS.

0
167 Oct 26, 2006 at 03:05

This has gone far enough; I’m locking the thread.