03.01 - Q&A

Started by
119 comments, last by Teej 20 years, 6 months ago
If there''s something about the C language that you don''t fully understand, then chances are you''re going to suffer for it somewhere down the line. Between myself and the rest of the members of this forum, no C language question will go unanswered for long. You''ll get the answer you need, and the forum will gain a nice collection of C language Q&As for the benefit of future readers. Like I''ve said before, "There''s no such thing as a stupid question". Teej
Advertisement
You can get Gimp for win32 (which is about 90-99% stable depending on your video card) from here.

It is equal to Photoshop in many ways, better in some, and it does have it''s own faults. But generally much better than PaintShop Pro and of course free.
Hi, I could use a little help with pointers, linked lists and FILE I/O.

d00dzs

I turn pixels into gold.
José Correia AKA d00dzs posting...HEEELLLOO
I just finished a course on data structures where we did linkedlists via array/cursor, dynamic, recursive... shoot me a question and I might be able to answer it , as for pointers, here is a great tutorial!

http://pweb.netcom.com/~tjensen/ptr/pointers.htm

you got me on the file I/O though... I know dittily squat about that stuff , hope that helps.

"You won''t get wise with the sleep still in your eyes." - Neil Peart
"You won't get wise with the sleep still in your eyes." - Neil Peart
Heya,

I was wondering if anyone on ths forum has tried to use the free compiler: LCC Win32. I have recompiled the Quake 3 source with this but I do not think it compiles C++ stuff. Any yays or nays about it?

L8R

-Nark
--------------------------Budha walks up to a hotdog vender and says:"Make me one with everything."L8R
Alright, I''ve got a billion questions on *exactly* how C/C++ compilers work (and I understand some of my questions may be compiler specific).

I''m currently taking a class on assembly language for the Motorola 68HC12, and many of my questions will involve understanding memory. I''ve got a ton of them (though, I can only remember so many at once!) so, please be patient =)

Firstly, I was to make a few statements...and please correct them if I''m wrong!! These are some of the fundamentals which I judge situations on, and I''d like for them to be corrected first, if they need to be.

1) C/C++ passes all parameters by value by default. This involves allocating a new & separate memory address on the hardware stack (using a instruction similar to LEAS -2,SP (for say, and int) AFTER the calling routine. Afterwards, a value equal to the passed parameter is placed within that address on the stack. The stack is deallocated BEFORE the end of the subroutine, therefore destroying or deallocating the "variable" on the stack. The described above is a part of (if not all of, I''m not sure!) what we would call "function overhead" -- any, of course takes a few CPU cycles. If the function is called over & over, it would be a waste of CPU cycles -- which introduces inline, which I''ll ask about in a sec.

2) An exception to this rule is whenever C/C++ passes parameter of any type of object. This includes any non-C strings, prewritten classes. This is because an "object" is essentially a variable (of type long int? - for 16 bit addressing) that acts as a pointer to the first position in memory in which the object holds. In this case, does C/C++ allocate stack space for the pointer (ie, 2 bytes onto stack, LEAS -2,SP) and allow the value of the pointer to change within the subroutine - but *NOT* affecting the results after the close of the subroutine. Therefore, you have access to "permanently" change all values within the object; however, any changes to the pointer (changing "where the pointer points to") will not be affected after the function call? This runs into a lot of grey area for me, as my understanding of C/C++ is limited here. Please explain any misunderstandings I have of C/C++ as well as anything you can think of that adds to this discussion.

3) Finally, I''m wondering *exactly* what goes on as far as memory goes, CPU cycles, and differences between functions, in the following code...in the comment I put what *I* believe is going on...

Class Example {
...
};

Example SubA(Example ex1)
{
... // does not create a copy of entire object ex1, merely a copy of the pointer ex1, allows all access to public variables
}

Example SubB(const Example &ex1)
{
... // passes the object "by reference" and does not allow the change of the pointer ex1, however, still allows access to public variable INSIDE ex1
}

And finally...tell me why this code is valid:
Example SubC()
{
Example ex2 = new Example();
return ex2;
}
...with my understanding, the local variable ex2 should be deallocated prior to the subroutine actually "ending." For this reason, the address ex2 points to should be an unallocated, random place in memory. The only way that I can convince myself that this works is if return actually (a) allocates space on the hardware stack for retrieval after the subroutine ends, to hold the value of the object ex2. (b) acts somewhat like a RTS instruction in the 68HC12, where it physically *replaces* the machine code that the function call (from main, or parent routine) with a copy of ex2; however, this would require special preparation of the function call, so that there is enough space in the actual call & space from any parameters passes (usually placed after the mnemoic AS WELL AS any extra space needed, so that you would not rewrite or overwrite the next instruction. In such case, the return operator much calculate how many bytes to increment the PC counter (again, I''m refering to the Motorola chip when I should be trying to learn the Intel x86 chipset) by the proper amount to "skip" over this white space. This seems like an awful long winded way to do things, so some clarification is much MUCH appreciated!!

Anyway, sorry for the terribly long post...and thanks for your time...
~khal

---
~khal
---~khal
Help.. please this is driving me nuts.

Since the first time the board was up I have followed through all the posts and have had very little in the way of problems, but this has me stumped. If I compile one of the original files from Teej''s site I have no problems with the compile or running of the game. If I type the code from scratch using the same settings for the libraries and links etc. then the game compiles ok but when run comes up with (mostly) "DI_Init failed".

I have checked for spelling mistakes and all the usual stuff, I even thought I had corrupted something so I formatted the hard drive and re-installed everything with still the same results.

If anyone has any suggestions as I am at a loss and not very experienced at this, it would be much appreciated.

I am using Windows2000 and VC++ 6.0 with DX8

Thanx in advance.
I don''t know your problem for sure but i can give you a advice.
When you are at a critical point of the program(Creation of objs or devices...) put a MessageBox(HWND, LPCSTR, LPCSTR, UINT) call (it creates a messagebox with custom message) where teej does the error checking.This way you might have an idea what is wrong.
Sorry the bad English.)

I turn pixels into gold!!
José Correia AKA d00dzs posting...HEEELLLOO
Hi,

I love this forum and I''ve followed it since the beginning.
So here I am for the second edition and I hope we are all gonna enjoy it.

Thanks a lot for your time Teej,
bios10h.
d00dzs: Pointers is a pretty popular topic, and for good reason -- it''s one of the most powerful assets of the C language. If you''re patient, I''m sure that you''ll find an article on pointers sooner rather than later in 03.03 - Selected Language Topics. I can almost guarantee the same treatment with linked-lists and file I/O; both hot topics.

khal: I happen to also do some OS development now and again, and I''ve found a nice little document that illustrates compiled code and actual ASM instructions:

http://nondot.org/sabre/os/S1Booting/CompilingBinaryFilesUsingACompiler.pdf

As for your numerical questions:

1) The PDF file I''ve mentioned will be of help here. It looks at functions, parameters and pointers, and how they''re treated by the compiler.

2) If memory serves, a function is given the pointer on the stack and is free to alter the contents of any memory accessible via the pointer, but cannot change the pointer itself. After all, the pointer is merely a variable (4-byte on Win32) value. When the caller regains control, the original pointer value is still on the stack from the original function call.

3) SubA() receives a copy of Example, and therefore can''t affect the object externally -- your typical ''pass by value''. On the other hand, SubB() receives a pointer to the object and masquerades it as an actual object (by reference), but can''t alter anything because of the const restraint. Finally, the last function, SubC() works because the return value of a function is passed back to the caller via the stack. Once again, the PDF file link I''ve provided will give you a look at function parameters and return values.

Whew...

Mano3D: You''re a little premature in worrying about the template code -- It''s part of the Ladder series that may be up by the time you read this. In the meantime, it''s hard to diagnose the problem from here, but one thing comes to mind: do you have the following #define?

#define DIRECTINPUT_VERSION 0x0700 


One thing that was blatently absent from the template code the first time around was any form of debugging facility, but I can assure you that this time it won''t be overlooked. In your particular case, I''d rewrite DI_Init() to allow the HRESULT value from all of those DI calls to be returned to the caller (Game_Initialize()) so that you can see the error code and look it up in the documentation.


Keep ''em coming...

Teej



This topic is closed to new replies.

Advertisement