The unexplainable crash

This topic is 3884 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I'm writing a little game, and I need to load a file with a list of levels in it, and put the list members (names of levels) into a structure. The structure has two data types in it. char *ime (which is the name of the level) and int br (which is the number of the level in the list). I uset typedef to call it 'Levels', and then I declared 'Levels *LevelList'. So, we have a pointer to an array of structures...
int LoadList()
{
int cnt = 0;
char temp[50];
LevelNum = 0;
FILE *lst;
lst = fopen("levels.lst", "r");

while ( (fgets(temp,50, lst) != NULL))
{
strcpy(LevelList->ime,temp);  //If I comment these two commands the program
LevelList->br = cnt;          //doesn't crash, so the problem is there.

printf("%s\n", temp);         //I used printf to see (in Stdout.txt) wether
//the list was read correctly. It was!
LevelList++;
cnt++;
}

LevelList - cnt;
fclose(lst);
return cnt;
}

So do you have any idea why I can't acess my structure and put data into it?

Share on other sites
It would appear that you never allocated any memory for your "ime" member of your Level structure. Its hard to tell without seeing additional code.

Your code appears to be C. Any particular reason you don't wish to use C++? It would make your life easier.

Share on other sites
I'm actually interested in writing drivers and 'system programming' (don't laugh at me), so I need C. I'm just a beginner, so I'm writing the game as an exercise.
I chose to make the game, as this kind of project would show me exactly what I don't know (amongst many, many things), and what to learn next. In this case, it's memory allocation, pointers to structures, and a thing I really don't understand pointers to functions... Man I have so much to cover :|
(Of course, I will shift to C++ once I feel I know enough of C to do so...)

Share on other sites
Look, I don't know what you heard, but C isn't necessarily the way to do "systems programming" these days. It isn't a particularly good stepping stone to C++, either. Linus Torvalds may endorse C and despise C++, but it's the general and reasoned opinion of pretty much all of us "in the know" GDNetters that he's an asshole and/or doesn't know what he's talking about. There are, after all, lots of optimizations the C++ compiler can do that the C compiler can't, simply because the extra expressivity of the language allows the programmer to give the compiler more useful information. For example, the standard library std::sort() outperforms the old-fashioned C qsort().

But regardless, I feel compelled to point out a few things:

- Even in C, you can and should initialize variables - consistently. Thus, 'FILE * lst = fopen...'.

- Y dn't rlly sv nythng by mttng vwls frm yr vrbl nms. Nor by making them short and/or cryptic in other ways. The names don't exist in the compiled code. Don't arbitrarily make things harder for yourself to read. I can't even begin to guess what an "ime" or a "br" is.

- I assume "LevelList" is a pointer into some kind of array? "List" is a bad name, then; it implies some kind of linked list. If that's what you wanted, then the arithmetic you're doing on it is totally bogus.

- "LevelList - cnt;" is a valid statement, but it doesn't do anything. I think you want '-=' for that operator.

- But actually, it's bad design for elements of a container to know where they are in the container. In order to get at them, you have to already know, so you can tell them (or rather, the code that will operate on them) when the time comes. It's also needlessly awkward to modify a global pointer, keep track of the change, and then reset it. Just work with a copy instead.

- There is a forum for beginners here. It is called the obvious thing - For Beginners - and located in the obvious place - at the top of the list.

[Edited by - Zahlman on February 1, 2008 12:39:47 PM]

Share on other sites
Quote:
 LevelList->br = cnt; //doesn't crash, so the problem is there.

Really?

There's nothing stating that writing unallocated memory causes a crash. LevelList could very well be unallocated, but accessing br member happens to not cause a crash, it just performs an undefined operation.

Writing to unallocated memory is non-deterministic. This isn't memory managed language - there you get an exception if you try to do that. Here, it may or may not work, or it may work incorrectly.

Share on other sites
Those words 'ime' and 'br' stand for 'name' and 'number' in my language :D
So, you suggest dumping C, and working on C++?
Amd yeah, thanks for the tip on LevelList - cnt, I don't know what to hell I was thinking. I still haven't learned the syntax well...
Anyway, thanks for the info.

Share on other sites
Quote:
 Original post by DrWatsonThose words 'ime' and 'br' stand for 'name' and 'number' in my language :DSo, you suggest dumping C, and working on C++?Amd yeah, thanks for the tip on LevelList - cnt, I don't know what to hell I was thinking. I still haven't learned the syntax well...Anyway, thanks for the info.

It all depends. Both languages have their uses, but it's more and more commonly that C++ is used in 'Systems Programming'. However, don't stick to just 1 language. Learning C is a good choice, because there might be a chance you need to use a structured programming language. But learning C++/C#/Java/etc. is also a good deal, because OOP is becoming more and more spread in the world of systems programming.

I have had to put up with Thorvald's fanboys in the past, who depised C++ because Thorvalds didn't use(The exact same reason they loved git, because Thorvalds wrote/used it). A good programmer knows when to apply which language. Learning a new language is mostly the easy part of the job(Learning to programming is language independent).

You could go and learn both. But if you go for C++, don't just program C with classes. Use actual design patterns.

Toolmaker

Share on other sites
PS : I don't really give a s*it about Linus, and I surely didn't start learning C because of him.

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
17
5. 5

• 9
• 12
• 9
• 33
• 13
• Forum Statistics

• Total Topics
632593
• Total Posts
3007269

×