Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
0 user currently in Programming. | 3 guests
Acmlm's Board - I2 Archive - Programming - Thinking of getting serious with C. | |
Pages: 1 2 3Add to favorites | "RSS" Feed | Next newer thread | Next older thread
User Post
HyperLamer
<||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people

Sesshomaru
Tamaranian

Level: 118

Posts: 2885/8210
EXP: 18171887
For next: 211027

Since: 03-15-04
From: Canada, w00t!
LOL FAD

Since last post: 2 hours
Last activity: 2 hours
Posted on 01-11-05 03:25 AM Link | Quote
Originally posted by Disch
Umm... you always have to use the resource ID... that's what it's there for. I must be misunderstand what you're saying

Well by default the button is named something like IDC_BUTTON1, and that worked fine. But if I rename it to something like Button1 and try to use that, it says the variable isn't defined. I have to use the number 1000 (which Button1 is defined as) instead.
Parasyte

Bullet Bill
Level: 35

Posts: 179/514
EXP: 267348
For next: 12588

Since: 05-25-04

Since last post: 104 days
Last activity: 32 days
Posted on 01-11-05 03:34 AM Link | Quote
Eh, you have to use "IDC_BUTTON1" if the button is defined as "IDC_BUTTON1" If you don't like that name, you can give it another one (rename it within the dialog editor properties window!) like "IDC_CLOSE".

It's common to have defines (preprocessor defines, remember #define ?) written in all uppercase letters. Remember that C is case-sensitive. In no way does "Button1" equal "IDC_BUTTON1"
HyperLamer
<||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people

Sesshomaru
Tamaranian

Level: 118

Posts: 2898/8210
EXP: 18171887
For next: 211027

Since: 03-15-04
From: Canada, w00t!
LOL FAD

Since last post: 2 hours
Last activity: 2 hours
Posted on 01-11-05 04:54 PM Link | Quote
I did rename it in the dialog editor, and it updated resource.h but it doesn't seem to want to re-read the file.

[edit] Also, regarding memory management; how can I define a variable-sized array? For example, say I wanted to make a program to read files that could be anywhere up to 4MB. If the file is only 32K, it wouldn't make much sense to have 4MB allocated for it. Is there some way to dynamically allocate memory blocks of a variable size?


(edited by HyperHacker on 01-11-05 12:46 PM)
Dish

Spiny
Level: 38

Posts: 230/596
EXP: 355646
For next: 14801

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 01-11-05 10:16 PM Link | Quote
As for your first problem... only thing I can think of, is resource.h isn't being saved before you compile.

As for your memory allocation question:

The C way is usually done with malloc() and free() functions. You #include <malloc.h>, and create your array with malloc like so:

int* myarray;
myarray = (int*)malloc( howmany * sizeof(int) );

'howmany' would be however many ints you want to make memory for. If there isn't enough memory, malloc will return NULL, so you might want to check for that.

Be sure to clean up whe you're done... or you'll have memory leaks in the program. You un-allocate the blocks with free():

free( myarray );


The C++ way is pretty much the same... but instead you use new[] and delete[]:

int* myarray;
myarray = new int[ howmany ];

delete[] myarray;

(afaik, doesn't require any #includes, which I find to be strange, actually)


Never mix malloc with delete.... or new with free. If you allocate something with malloc, use free. If you allocate something with new... use delete. The only real difference between the two is that new[] will call an object's Constructor (if it has one), and delete[] will call the object's destructor (if it has one) -- making it more useful for C++ classes and stuff like that. Whereas malloc/free will not call any constructors/destructors.
sloat

Level: 16

Posts: 31/85
EXP: 18044
For next: 2212

Since: 05-21-04
From: South Central Delaware

Since last post: 19 days
Last activity: 5 hours
Posted on 01-12-05 12:37 AM Link | Quote
Originally posted by HyperHacker

[edit] Also, regarding memory management; how can I define a variable-sized array? For example, say I wanted to make a program to read files that could be anywhere up to 4MB. If the file is only 32K, it wouldn't make much sense to have 4MB allocated for it. Is there some way to dynamically allocate memory blocks of a variable size?


You could check the size of the file before allocating the memory for reading.

There's basically no such thing as a variable sized array in C; nothing that you could compare to VB anyway. You can get around this by allocating, copying, and deleting the old mem, or if you're using the Windows memory functions, there is a HeapResize function which will do that for you. I'm willing to bet that the ReDim command in VB just copies old memory, which would explain why it's so slow.

In this case, a better approach may be to use VirtualAlloc and the related functions. This function lets you reserve pages in memory and use them (or not) later.

Another option would be to use memory mapped files. You probably won't want to use them for this, but memory mapped files are really good if you need to read portions of a huge file.


(edited by sloat on 01-11-05 03:39 PM)
Parasyte

Bullet Bill
Level: 35

Posts: 180/514
EXP: 267348
For next: 12588

Since: 05-25-04

Since last post: 104 days
Last activity: 32 days
Posted on 01-12-05 05:21 AM Link | Quote
Have you tried the "Rebuild All" option after making changes to the resource names? Also try "Clean" and then "Build".
HyperLamer
<||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people

Sesshomaru
Tamaranian

Level: 118

Posts: 2934/8210
EXP: 18171887
For next: 211027

Since: 03-15-04
From: Canada, w00t!
LOL FAD

Since last post: 2 hours
Last activity: 2 hours
Posted on 01-16-05 03:01 AM Link | Quote
Cool, hitting Clean did it.

Now what does "fatal error C1010: unexpected end of file while looking for precompiled header directive" mean? This comes up in a source code file which I've included from another source code file (also tried including it in the header file). The only solution I've yet heard is to copy all the #includes into that file, but that just raised a whole lot of errors (and for good reason too, that doesn't even make sense ).
Parasyte

Bullet Bill
Level: 35

Posts: 193/514
EXP: 267348
For next: 12588

Since: 05-25-04

Since last post: 104 days
Last activity: 32 days
Posted on 01-16-05 10:02 AM Link | Quote
Precompiled header directives... Those are the *.pch files that the compiler makes. I have no idea why the silly compiler requires these things. Personally, I would suggest using a better compiler. MinGW + make files + batch files for compiling and cleaning always did the trick for me. As for IDEs. I've grown to love TextPad.

If you hate such setups, I can't say I blame you. It does take a while to ween yourself off of Microsoft's nipple.
HyperLamer
<||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people

Sesshomaru
Tamaranian

Level: 118

Posts: 2954/8210
EXP: 18171887
For next: 211027

Since: 03-15-04
From: Canada, w00t!
LOL FAD

Since last post: 2 hours
Last activity: 2 hours
Posted on 01-18-05 02:08 AM Link | Quote
I'll look into that. Does it come with a resource builder? If not, what should I use for that? Also does it optimize big switch statements into jump tables? I was hoping to write an emulator (big project I know, but I already wrote the essentials in VB, it's just too slow) and I'd like it to be as fast as possible. (Even though it's only emulating a 4Mhz Z80. )
Parasyte

Bullet Bill
Level: 35

Posts: 205/514
EXP: 267348
For next: 12588

Since: 05-25-04

Since last post: 104 days
Last activity: 32 days
Posted on 01-18-05 02:51 AM Link | Quote
There are many, many, many resource editors available that can be used with MinGW. MinGW does not come bundled with a resource editor, but it does include a resource compiler. (windres.exe) A free resource editor can be downloaded with the LCC-Win32 package - http://www.cs.virginia.edu/~lcc-win32/

There are also at LEAST 11 levels of optimization you can use with MinGW. Perhaps even more. The optimatation levels range from speed-oriented (-O0 to -O9 for slower to faster executables) to size-oriented (-Os to create the smallest file the compiler can manage). Each optimization level will optimize certain portions of code (such as switch statements) in many different ways. You may notice that speed-optimized executables tend to be a bit larger than non optimized builds. That's usually due to unrolling loops and inlining functions to reduce overhead.
Anything you write in C will be fast. VERY fast, compared to Visual Basic. But just how fast it will be depends on how it's written. There are a lot of interesting tricks you can use to write fast code. So you should keep that in mind, and not rely on the compiler to do all the work for you.


(edited by Parasyte on 01-17-05 05:53 PM)
HyperLamer
<||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people

Sesshomaru
Tamaranian

Level: 118

Posts: 2957/8210
EXP: 18171887
For next: 211027

Since: 03-15-04
From: Canada, w00t!
LOL FAD

Since last post: 2 hours
Last activity: 2 hours
Posted on 01-19-05 12:09 AM Link | Quote
How exaclty do I work this? I compiled it with 'gcc -c' like the doc says, and it didn't output anything except a .o file in \bin, even though the source file was in a completely different folder.
Dish

Spiny
Level: 38

Posts: 233/596
EXP: 355646
For next: 14801

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 01-19-05 12:22 AM Link | Quote
.o files are object files. You still need to run the linker to make an executable.

.cpp --> [compiler] --> .o --> [linker] --> .exe
HyperLamer
<||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people

Sesshomaru
Tamaranian

Level: 118

Posts: 2958/8210
EXP: 18171887
For next: 211027

Since: 03-15-04
From: Canada, w00t!
LOL FAD

Since last post: 2 hours
Last activity: 2 hours
Posted on 01-19-05 12:26 AM Link | Quote
Oh yeah, I see how that works now. Read the wrong thing.
Parasyte

Bullet Bill
Level: 35

Posts: 207/514
EXP: 267348
For next: 12588

Since: 05-25-04

Since last post: 104 days
Last activity: 32 days
Posted on 01-19-05 03:42 AM Link | Quote
gcc -o file.exe -c file.c
HyperLamer
<||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people

Sesshomaru
Tamaranian

Level: 118

Posts: 2971/8210
EXP: 18171887
For next: 211027

Since: 03-15-04
From: Canada, w00t!
LOL FAD

Since last post: 2 hours
Last activity: 2 hours
Posted on 01-19-05 04:22 AM Link | Quote
I've been using gcc -o file.exe file.c, seems to be doing the job. The only problem I've encountered so far is with the statement 'BOOL StopEmulation = false;' - it raises an error that false is undefined outside a function. If I don't give it a default assignment, it works, but then I don't know what the value will be.

And yay for being able to have a console window with a Win32 program. Sure beats MessageBox() for debug output.

[edit] Pooh, I just have to capitalize it. Where's that resource editor anyway? Do I have to download that entire program? Also what's this 'C99 mode'? I get the error "'for' loop initial declaration used outside C99 mode" when I try to do a for loop with the variable defined in it, like "for(int i=0;i<48;i++)". (This works if i is defined outside the loop, though.)


(edited by HyperHacker on 01-18-05 07:25 PM)
(edited by HyperHacker on 01-18-05 07:43 PM)
Parasyte

Bullet Bill
Level: 35

Posts: 209/514
EXP: 267348
For next: 12588

Since: 05-25-04

Since last post: 104 days
Last activity: 32 days
Posted on 01-19-05 06:35 AM Link | Quote
C99 is a C 'standard' that warns you about doing stupid crap that C++ supports. Just like declaring variables within functions. It's bad code. When you think about it, declaring variables within a loop seems like it would be re-declaring on each iteration, right? (That's not what actually happens, but it should be viewed in that manner.) Same goes for declaring within a for loop. The for loop is set up so it goes (assign; test; action) If you try to break the rules with something even as simple as (declare, assign; test; action) the compiler will obviously complain.
If you really want to write such nonsense code, though, you can switch to the C++ compiler. Which is generally g++.


(edited by Parasyte on 01-18-05 09:37 PM)
(edited by Parasyte on 01-18-05 09:38 PM)
neotransotaku

Baby Mario
戻れたら、
誰も気が付く
Level: 87

Posts: 1946/4016
EXP: 6220548
For next: 172226

Since: 03-15-04
From: Outside of Time/Space

Since last post: 11 hours
Last activity: 1 hour
Posted on 01-19-05 09:08 AM Link | Quote
well if you didn't know, your false outside the function error is because false is a C++ keyword, not a C keyword. However, one of the C headers #define BOOL, TRUE, and FALSE so you can use it.
Dish

Spiny
Level: 38

Posts: 234/596
EXP: 355646
For next: 14801

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 01-19-05 10:33 AM Link | Quote
'true', 'false', 'TRUE', and 'FALSE' are like the stupidest keywords ever.

Why the hell are 0 and 1 so inconvienient to type? =P I mean really... they're easier to type.. .and it's not like they're confusing.
HyperLamer
<||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people

Sesshomaru
Tamaranian

Level: 118

Posts: 2972/8210
EXP: 18171887
For next: 211027

Since: 03-15-04
From: Canada, w00t!
LOL FAD

Since last post: 2 hours
Last activity: 2 hours
Posted on 01-19-05 08:24 PM Link | Quote
Well, you write how you want then. The only part I don't like about it is that they have to be capitalized, but a quick '#define true TRUE' and '#define false FALSE' fixes that.

I'm having a bit of a problem with pointers, though. This function in particular:
void Inc16(BYTE reg1, BYTE reg2)
{
printf("Inc16(%X, %X) -> ",reg1,reg2);
reg2++;
if(reg2 == 0)
{
reg1++;
}
printf("%X, %X\n",reg1,reg2);
}


What I'm trying to do is pass it two variables, and have it modify the variables themselves. If I call it like "Inc16(regB, regC);" where regB = 0x13 and regC = 0x37, the function does indeed show that it's incremented reg2, but regC is still 0x37. I also tried using pointers, the best I could manage was to increment the pointer itself.

[edit] Bleh, no tabs. Also I know that if doesn't need braces but I'm gonna have to add to it later.


(edited by HyperHacker on 01-19-05 11:25 AM)
Parasyte

Bullet Bill
Level: 35

Posts: 210/514
EXP: 267348
For next: 12588

Since: 05-25-04

Since last post: 104 days
Last activity: 32 days
Posted on 01-19-05 09:28 PM Link | Quote

void Inc16(BYTE *reg1, BYTE *reg2)
{
  (*reg2)++;
  if((*reg2) == 0)
  {
     (*reg1)++;
  }
}

//...
BYTE val1 = 8, val2 = 120;

printf("Inc16(0x%02X, 0x%02X) -> ",val1,val2);
Inc16(&val1,&val2);
printf("0x%02X, 0x%02X\n",val1,val2);
//...

Learning how to use pointers properly is rather important. Read what I said once again, about dereferencing pointers in C.
Pages: 1 2 3Add to favorites | "RSS" Feed | Next newer thread | Next older thread
Acmlm's Board - I2 Archive - Programming - Thinking of getting serious with C. | |


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.012 seconds.