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 | |
Pages: 1 2 3Add to favorites | "RSS" Feed | Next newer thread | Next older thread
User Post
Dish

Spiny
Level: 38

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

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 04-02-05 12:57 PM Link | Quote

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.
Parasyte

Bullet Bill
Level: 35

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

Since: 05-25-04

Since last post: 104 days
Last activity: 32 days
Posted on 04-02-05 04:58 PM Link | Quote
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.
Squash Monster

New Age Retro Hippie
Togateiru Fohku Kohgeki!!
GRUNGE no HAMSTER otona bite
Peace love and turnpike!

Level: 40

Posts: 560/677
EXP: 430507
For next: 10802

Since: 03-15-04
From: Maryland (of the Country Between Canada and Mexico)

Since last post: 5 hours
Last activity: 5 hours
Posted on 04-03-05 01:07 AM Link | Quote
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.
Zem
You can be civil without being flowery, dipshits.
Level: 49

Posts: 1058/1107
EXP: 829398
For next: 54485

Since: 06-13-04

Since last post: 131 days
Last activity: 131 days
Posted on 04-03-05 01:12 AM Link | Quote
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.


(edited by Zem on 04-02-05 03:15 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: 4022/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 04-03-05 02:25 AM Link | Quote
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.
Parasyte

Bullet Bill
Level: 35

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

Since: 05-25-04

Since last post: 104 days
Last activity: 32 days
Posted on 04-03-05 04:17 AM Link | Quote
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
<||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: 4028/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 04-03-05 11:43 AM Link | Quote
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

Bullet Bill
Level: 35

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

Since: 05-25-04

Since last post: 104 days
Last activity: 32 days
Posted on 04-03-05 12:12 PM Link | Quote
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).
sloat

Level: 16

Posts: 39/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 04-05-05 11:26 PM Link | Quote
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.

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: 4030/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 04-06-05 12:10 AM Link | Quote
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.


(edited by HyperHacker on 04-05-05 07:16 AM)
loadingNOW

Red Paragoomba
Level: 14

Posts: 51/61
EXP: 10382
For next: 2689

Since: 07-15-04
From: Silent Hill

Since last post: 136 days
Last activity: 3 days
Posted on 04-06-05 02:03 AM Link | Quote
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...
sloat

Level: 16

Posts: 40/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 04-06-05 02:25 AM Link | Quote
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.
interdpth

Rex
Level: 36

Posts: 430/527
EXP: 294398
For next: 13712

Since: 03-20-04

Since last post: 10 days
Last activity: 31 days
Posted on 04-06-05 03:46 AM Link | Quote
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
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: 4038/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 04-06-05 04:41 AM Link | Quote
That doesn't say much about VB vs C so much as you being a fast learner.
interdpth

Rex
Level: 36

Posts: 431/527
EXP: 294398
For next: 13712

Since: 03-20-04

Since last post: 10 days
Last activity: 31 days
Posted on 04-06-05 07:01 AM Link | Quote
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!
Pages: 1 2 3Add to favorites | "RSS" Feed | Next newer thread | Next older thread
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.013 seconds.