new operator allocates too little memory

Started by
18 comments, last by FritzMar 17 years, 2 months ago
Quote:Original post by AndiDog
Sounds quite logical :) but why should I use C++ std::list if I am too stupid to code my own *simple* list template class?

Why should you use new or malloc if you are too stupid to implement your own memory manager?

BTW, I firmly feel that all programmers should be able to implement data structures. I just don't know whether that's what you want to learn now. Just keep in mind that it's not a prerequisite to using the standard library.
Advertisement
Quote:Original post by AndiDog
Sounds quite logical :) but why should I use C++ std::list if I am too stupid to code my own *simple* list template class?


Well, if you can't get it right yourself, and you don't use the standard version, what are you going to use instead? You need *something*... :)

And if you *can* get it right yourself... why spend the time on it when there's something provided for you that (a) is as efficient as it realistically can be for what it does and (b) is - well - *provided for you*? :) (Obviously, the standards committee can't write your program for you; but certain concepts - like storing a sequence of objects of the same type in a "container" - are so common that they put together robust yet low-overhead solutions for those problems.)

Of course, you do still get to use your brain here - to choose between std::list, std::vector and std::deque. They implement different strategies for holding a sequence of items, because none is best for all situations (and there isn't really a good way to take the best of each, either - you pretty much have to choose each time).

Note that if you got the idea to make your own list on account of some formal CS education, that - well, CS education generally sucks in that regard :) Profs love to teach linked lists, but people could graduate from these programs not even knowing there are other ways (i.e. the strategies implemented by std::vector and std::deque), because they aren't encouraged to *think* about such things. Certainly they don't teach them directly - no, lists are just too "elegant" to bother with anything else :S
Checking that using the structure properly isn't causing the read access violation is easy.

Entry *e = new Entry;
e->ptr = 0;
e->prev = 0;
e->next = 0;

If that doesn't cause an access violation then why would writing to it somewhere else cause one?

And like everyone's been saying, since this is C++ why are you using void*?
Quote:Original post by Zahlman
Note that if you got the idea to make your own list on account of some formal CS education, that - well, CS education generally sucks in that regard :) Profs love to teach linked lists, but people could graduate from these programs not even knowing there are other ways

Yikes, really?
I obviously only have experience with CS where I'm studying, but that's not the case here, at least. Sounds.... scary [grin]
Quote:Original post by Spoonbender
Quote:Original post by Zahlman
Note that if you got the idea to make your own list on account of some formal CS education, that - well, CS education generally sucks in that regard :) Profs love to teach linked lists, but people could graduate from these programs not even knowing there are other ways

Yikes, really?
I obviously only have experience with CS where I'm studying, but that's not the case here, at least. Sounds.... scary [grin]


I've begun to think that American CS courses must be bad, because they keep bagging them. Ours aren't like that.

Anyway, AndiDog seems to be a C programmer who refuses to let the features of C++ take over the tedious parts of programming for him. Misconceptions about control and speed, probably. If he was using C, that'd be no problem, but he's using C++, and his huge project would be a million times more manageable. Eh.
[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]
It's ok. It's likely the OS, language or environment he's using causing the problems. He's the best programmer EVER to live. It will be decades before anyone like him comes along again. Embrace his flawless C/C++ but ignore the useful parts of C++ skills!

Remember void * is your bestest friend and VERY safe. Template are evil and the spawn of Satan. All hail Satan.
Quote:Original post by AndiDog
I don't use a constructor because I zero the struct before usage.

My code is far too complex to post it here.

The program does not crash if I allocate more memory than "needed" - and that's what I don't understand. Anyway I'm sure it's my fault (as always).


You're almost certainly accidentally writing beyond the bounds of your struct at some point - so allocating an extra 32bits of storage stops your program crashing.
@Anonymous Poster: Real funny... of course I ain't the one and only programmer (that's why I created this thread!) [wink]

Got my solution now: I removed the (prev) member in the struct because I don't use it in the code, and now it works...

O.K. I gotta admit there was a mistake in my code :)
It really doesn't sound like you solved the problem. You've merely extended your luck.
--Michael Fawcett
you should either program in c++ or c pick one or the other or at least keep one separate from the other.

When you are working with pointers error checking can save you tons of time. testing to see if your pointers are valid and reporting when they are not. always zero your pointers your self and before you use them test to make sure they are not zeroed. this has gone a long way in reducing my access violations with pointer

good luck

0))))))>|FritzMar>

This topic is closed to new replies.

Advertisement