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 - C/C++ is easier then VB
  
User name:
Password:
Reply:
 

UserPost
interdpth
Posts: 431/527
It'd take me longer in VB. I couldn't ever get bitblt to work right for some reason now I can. Not to mention I had coded it in VB. It'd be a bigger file size so hah!
HyperLamer
Posts: 4038/8210
That doesn't say much about VB vs C so much as you being a fast learner.
interdpth
Posts: 430/527
Another statement that C is easier then VB. In the four days I learned the basics of API programming I wrote a basic map editor in C still have to master scroll bars though here's a demo if any of you'll want to look at it. This is written entirely in C by me. And anyone have problems with structs in the latest version of dev-C++?

Oh and here is the current build. http://www.overmyhead.net/mew3domain/kittyhacker/TILE.rar
sloat
Posts: 40/85
Originally posted by HyperHacker
Yeah, I think I get it. I shouldn't be programming when I'm sleepy.

Do API functions run as another thread/process though? It seems like they would, but you'd think that would cause stability problems.



They don't in C or asm, but I'm not sure about VB. When I still used it, some things just didn't make sense, like having to loop an api call to wait for a return value. But I didn't always have to loop, only on certain functions, IIRC.


Also why does C allow such useless staments as 'x << 2' (not assigning it to anything) and 'if(blah);' (no following statement)? It's messed me up more than a few times.


On those statements, Visual C++ gives me the following warnings:
main.cpp(35) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
main.cpp(38) : warning C4390: ';' : empty controlled statement found; is this the intent?

With gcc, I don't get a warning on either unless I use the '-Wall' switch, and even then it's only on the shift operator.

gcc will probably remove the statements from the end result, but i'm not sure about vc. anyway, it's more about you being in control and you knowing better than the compiler.
loadingNOW
Posts: 51/61
as far as I remember api calls are just... well calls of functions in windows dlls (see notes in the api reference) hence that's why breakpoints like
bpx GetWindowTextA or bpx GetDriveTypeA works so well in debuggers like softice or olly...
HyperLamer
Posts: 4030/8210
Yeah, I think I get it. I shouldn't be programming when I'm sleepy.

Do API functions run as another thread/process though? It seems like they would, but you'd think that would cause stability problems.

Also why does C allow such useless staments as 'x << 2' (not assigning it to anything) and 'if(blah);' (no following statement)? It's messed me up more than a few times.
sloat
Posts: 39/85
Originally posted by HyperHacker
See, I never really got how that can work in a multi-threaded environment, particularly with API. You call the function, it allocates a string and returns a pointer... Now that string is just kinda 'stuck' in memory, since AFAIK you can't free memory in another process. And suppose another thread called the same function at the same time? The two could be mixed up.


You might be confusing threads and processes here. It's possible to have two threads from one application in different processes, but this isn't the default behavior and it's a pretty involved process to do this.

A thread has it's own stack space, but it shares global or heap memory with other threads of the same process. In other words, passing a pointer to a local variable to another thread will probably give access errors if you try to read it. A pointer to a global variable or one allocated with new or malloc will be ok; you can delete it in any thread.

I actually had to do this in a project. I needed a MessageBox that didn't block the current thread. The function allocates memory with new, then starts the thread. The thread shows the message box, and deletes the memory when finished.

If two threads try to modify the same global memory or variable, then you will almost definitely run in to problems. There are synchronization functions to handle that, but that's an entirely different discussion.

Parasyte
Posts: 413/514
This is why I tried stressing the fact that most string manipulation routines do not allocate memory at runtime. And when they do, they either free it immediately before returning, or somehow manage a queue buffer so the program can keep track of what to free later. C++ classes have the unusual benefit of "automatically" handling all of this for you. (You just have to remember to do your freeing in the destructor, the compiler does the rest).
HyperLamer
Posts: 4028/8210
See, I never really got how that can work in a multi-threaded environment, particularly with API. You call the function, it allocates a string and returns a pointer... Now that string is just kinda 'stuck' in memory, since AFAIK you can't free memory in another process. And suppose another thread called the same function at the same time? The two could be mixed up.

And another thing about VB - no pointers. Sure that's bad, but for n00bs, it means no annoying memory access errors.
Parasyte
Posts: 411/514
Either you define your arrays with plenty of memory, or just use pointers. To satisfy your question:

char *x;
x = "this is text";
// ...
x = "this text is longer";


You do not HAVE to keep strings in writable arrays. And infact, it can be a bad idea to do so, especially when "returning strings" from a function. A far better thing to do in such a situation is returning a pointer to a string. In this way, you avoid tons of overhead in the form of pushing the entire array to the stack before returning from the subroutine, AND your code won't have to worry about any of the memory management -- your function can do that internally.
Typically, it's easier to declare a static array within the function returning the string pointer. Since the array will be statically available, (EG, not held within the stack, but in either the .data or .bss sections) the string will be accessible via a pointer, even after the function in which it is declared has returned. Say you can allocate 256 bytes for the array, and forget about it. On the other hand, you may wish to dynamically allocate memory for the string at runtime. The implimentation methods vary, but they all must keep track of pointers in some way in order to free the memory later on, when it is no longer needed.

An example of how to allocate memory for use with strcat():

char *x;
// ...
x = (char*)realloc(x, strlen(x) + strlen(str2) + 1);
strcat(x, str2);


The call to realloc() will reallocate memory for 'x', keeping the previous contents intact. The string length of 'str2' is simply added to the string length of 'x', and the 1 is added for the null terminator. Of course, the call to strlen(x) is done before the call to realloc(), so there are no conflicts there ... and even if it were called after realloc(), the previous contents of the allocated memory would be left untouched, meaning it would still return the same length, regardless.

Anyhow, all of this information is rather negligable, since memory allocation is hardly ever used in conjuntion with string manipulation. It just tends to add unwanted overhead. Afterall, this is not the 1970's; a char array with 256 elements will not kill you, even if only two or three bytes are ever used.

Smart coding is quite a bit more than keeping watch on memory management.
HyperLamer
Posts: 4022/8210
Try this, then. If you can show me a way to do this as simple as you can in VB, I'll eat some arbitrary object.

Dim x as string
x = "this is text"
[...]
x = "this is longer text"

Now this seems simple enough... But suppose the second string is the output from a function and you don't know how long it will be? You have to either declare a huge string and use up a lot of memory in hopes that it'll be enough, or figure out the returned string's length and allocate more memory, which likely involves calling the function again and again until the entire string fits if it's an API call or something in a DLL. VB, none of that, just assign x to the function's return value.

And with strcat(x,y) wouldn't x already have to be long enough for x and y together? So it's the same problem again.
Zem
Posts: 1058/1107
Okay, that makes sense. I haven't looked at the site yet, so this is going off your analogy... do the "buckets" refer to the address in memory, or just some weird space with all the variables you have in your program?

EDIT: Okay, looking at the site, I got it. Thanks a lot.
Squash Monster
Posts: 560/677
I was telling some friends about that analogy and they asked what Java would be. With Java, you're given a handle and a whole bunch of extensions, many of which, if not most of which, have additional slots to hook more attatchments on. And you can paint all the polygons you'd ever want, but if you want to paint an image, you have to run it through this crazy hairbrained attatchment with more doohickies and knobs on it than a magnet at a doohickies and knobs festival (seriously, I spent days trying and failing to understand AffineTransform) -- or take some wallpaper and cut a peice of it out and use that.

Originally posted by Zem
One thing that I had trouble with in C++ even when I was doing fine with Java is pointers. I never got pointers. I'm sure they're easy once you know how they work, but I have yet to find a tutorial or book that explains them in a way I understand. (If anyone can link me to something like that, I'd love it.)

The tutorial I always end up using is the one on cplusplus.com.

I'll try to explain, but I'm liable to mess up.

Say you have some buckets. Each bucket has a number painted on it, and some buckets have some stones. When you declare an integer, the computer will find a bucket that's not being used, remember its number, and put stones in it as you ask for them.
int foo = 4;
So say you want to know what bucket is being used for that integer. We get the bucket's number with &.
int fooBucket = &foo;
So we check fooBucket and find out that it's two. Those four stones we put in foo are in bucket two. Now, it's nice knowing what bucket something is in, but we need to be able to do something with that. We can get back to the actual bucket with *.
*fooBucket = 9;
Now, if you check foo, it'll be nine, not four, because you just said to put 9 into bucket two, which we know is the bucket we store foo at.
fooBucket++;
Now, this time we just incremented fooBucket, not the number of stones in the bucket it's pointing to, so foo is still nine, not ten. fooBucket is no longer pointing at foo, it's pointing at bucket three.
*fooBucket = 2
And now bucket three has two stones.

That's the concept. I still recomend the link I gave earlier for the details of how to work with pointers.
Parasyte
Posts: 408/514
You could always be a bad programmer and create your own string class to handle such stupid syntax as "Str1 = Str1 + Str2"... I highly advise against it, however.
Dish
Posts: 322/596

Str1 = Str1 + Str2.


strcat( Str1, Str2 );


Const x as String = "Text omfg".


const char* str = "Text omfg";


Really simple stuff. I still don't see what the fuss is about.
HyperLamer
Posts: 4007/8210
Oh god, Borkified code. XD

And sure, C's string handling isn't that bad, but compare to VB's. Str1 = Str1 + Str2. Const x as String = "Text omfg". Really simple stuff.
neotransotaku
Posts: 2843/4016
what does that have to do with anything? i'm guessing nothing...

as for fun-loving, i'm never going to get some
alte Hexe
Posts: 3468/5458
STD r eazy to get if urnt a progrmming nerd n get pussy
neotransotaku
Posts: 2836/4016
yeah, it is an april fool's thing--i was never into april fools

as for what interdepth did, his method works under strict conditions--after all fundamentally, comparision in computers is comparing bits...
Dish
Posts: 321/596
interdpth:

That's not string comparison, that's number comparison =P

I mean if you have a string that says "house" and other string that says "shoe" --- if you'd want to see if they both say the same thing, you'd use strcmp().

PS - this bork bork bork thing really sucks -- some lame april fools joke or something?
This is a long thread. Click here to view it.
Acmlm's Board - I2 Archive - Programming - C/C++ is easier then VB


ABII


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



Page rendered in 0.007 seconds.