• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

advice to expand to c/c++ with about 10-15 years of java experience

 
Rancher
Posts: 213
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, I tried to search for advices on getting started to learn c/c++ - but ended up in either a list of several books starting from 0 - or a few which require basic to advanced knowledge of c and/or c++.
As I have about 10-15 years experience with java I don't thing I will have to start at the very lowest basic - I know how computer works and what are the differences between datatypes, design paradigms and have a good understanding of object-oriented programming - although even my most recent php codes are still flat procedural although I know even php can be done in a oo-style.
What I'm looking for is somewhat of a "starting at the weird c/c++ low-level pointer stuff". Something like that doesn't waste a third of the book to re-teach me the basic fundamentals I'm familiar with but jumping right in like "This is what *, ** and & means before and after variables, types and functions.".

Don't get me wrong, I don't want to cut corners that are really required for a fundamental start in the C family - but I don't really need a re-iteration of binary, hexadecimal, big-endian vs little-endian or how to set up a dev environment with an editor and a compiler.

Possible languages are either english or german - or if what you have in mind like france only if a translation is available. International orders like via amazon or other retailers are fine.

I may still end up with possible a few basic books for both C and C++ - but as I do already have programming experience and it isn't my first language I feel confident starting at act2 of a beginners work.

Oh, and as I guess it's important my main target platform is Linux. I have no intentions for writing windows-compatible code - and the primary use case are terminal based tools and libraries.
 
Marshal
Posts: 75707
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since C is a procedural language, I would suggest you probably do want to start with the basics. A C application looks completely different from a Java® app.
 
Marshal
Posts: 5327
324
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got a lot out of The C Programming Language by Brian Kernighan and Dennis Ritchie (no relation to Campbell I believe) and would highly recommend it. In developer circles you'll often hear this book referred to as "K&R".
 
Rancher
Posts: 386
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree K&R is very good. However the OP has mentioned learning C++, and there is the argument that it's better to learn C++ without learning C first. Otherwise the danger is that you write C++ while still thinking in C, and C++ requires a different mindset to utilize its power/features. I suspect the OP has that mindset coming from Java.
 
Tim Cooke
Marshal
Posts: 5327
324
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well he did say "c/c++" which suggests a desire to learn about both. I have not used C++ in any significant capacity so can't make any suggestions.
 
John Matthews
Rancher
Posts: 386
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True. I'm primarily C, with some C++, and I think it might be better to start with C++; it's probably more useful, depending on what the OP wants to do with these languages of course (OP?), and perhaps easier to get to grips with if coming from Java.

I reckon coming down to C from C++ is easier than going up to C++ from C, although the latter might seem like the obvious route to take (and is what I did).
 
Campbell Ritchie
Marshal
Posts: 75707
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:. . . no relation to Campbell . . . .

I believe all Ritchies are related to one another somehow, but DR was such a distant relative that I never met nor knew him.
Agree it is a good book; it also teaches structured programming, missing out use of the goto keyword, another feature I like.
Not sure about Windows®, but Jens Gustedt's book doesn't seem to all happy with C compilers on Windows. Those for Unix‑like boxes (e.g. gcc are much better.
 
Saloon Keeper
Posts: 25475
180
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a little out of the mainstream here, but I'd argue that K&R still remains one of the best places to start (not that I had a choice - I didn't/couldn't learn C++ until Bell Labs released it. C++ was, after all, originally named "C with classes".

You can write perfectly legitimate C++ programs in C++. Though that would be a Bad Idea, obviously. C++ is C with classes, and more to the point, classes are C structs whose default scope for members is private, whereas structs in C++ are classes whose default scope is public.

So K&R can teach you the basics, and procedure-oriented programming. The authoritative tome on C++ is probably its twin: "The C++ Programming Language" by Bjarne Stroustrup. He should know. He invented it. Before that book came out, my information on C++ came primarily from his introductory article in the Communications of the ACM.

C or C++, welcome to the wonderful world of pointers and memory leaks! Learn how x[5] and *(x+5) are the same thing.

And speaking of memory leaks, if you're really looking to cover the C paradigm, check out Rust, too. I believe that Linus Torvalds has greenlit the use of Rust for Linux kernel code now.
 
John Matthews
Rancher
Posts: 386
13
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://lb-stuff.com/cc

I'm just the messenger
 
Campbell Ritchie
Marshal
Posts: 75707
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Matthews wrote:. . . I'm just the messenger

Hahahahahahaha!

That looks a good link.
 
Tim Holloway
Saloon Keeper
Posts: 25475
180
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something not listed there. In mid-1985, Commodore Corporation released the Amiga 1000. This was the first personal computer to run using true (pre-emptive) multi-tasking on a 32-bit memory model and flat address space (The Macintosh had the Motorola MC68000 MPU as well, but ran it as a 16-bit architecture).

Shortly after, I obtained a license from AT&T Bell Labs for their C++ translator product. At the time, there were no native compilers, so the translator converted C++ to C. The code was on a 3B-series (1600 BPI) magnetic tape, but a friend who worked at a local AT&T office helped me port its contents to Amiga-readable floppies, where I loaded it up and built in on the Amiga using the Lattice C compiler and an open-source copy of YACC. I then translated the Amiga OS header files to C++ (Amiga Exec was extremely object-oriented, even though it was implemented in assembler and C). When I went looking for beta testers, someone from Lattice contacted me about remarketing it. So I visited them in Illinois and we worked out a deal. Shortly thereafter, SAS Institute acquired Lattice, so the product was actually named SAS C++ for the Amiga.

SAS Institute was primarily interested in OS/2, and they wanted me to port to that platform, but at the time it was prohibitive. On the Amiga, a text-only app required 1MB to compile and a GUI app required 1.5MB. This was a bit of a stretch back then for IBM PCs, but the real killer was that the translator was designed to process depth-first and that was an extremely bad fit for a non-flat (segmented) memory space. To make it fit would have required breaking the translator into overlays like the old mainframes used to do, and that would have been a complete redesign. OS/2 later did go 32-bit flat, but I was long out of the game by then.

So in short, one of the first, possibly the first C++ implementations of C++ for personal computers was the Amiga version. The first time I ran C++ on an IBM PC was about 1990, and some of the early IBM C++ packages included a RAM expansion card with the product, if memory serves.

I should also mention that although C++ was designed initially as an extension to C, the process worked in both directions. The "//' comment convention was back-ported from C++ to C, for example. Regardless, both languages have seen a lot of changes since then.
 
John Matthews
Rancher
Posts: 386
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a good example of C-style C++ and 'proper' C++ - see the 3rd answer here, starts '@Fred Nurk', giving 2 different versions of a sort function:
https://softwareengineering.stackexchange.com/questions/48401/learning-c-properly-not-c-with-classes

I'd definitely write the first version; it would take me some time/googling to work out what the second is doing
 
Tim Holloway
Saloon Keeper
Posts: 25475
180
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A general observation not directly related. I learned to avoid this:


Because back when logging was more brute-force I'd end up doing this:


So instead I always code this:

That applies to C, C++, Java,, and pretty much any other language where forgetting where the boundaries are when you're under stress can bite you.

One thing about old-time C and C++ was that there weren't many pre-supplied services. Out of the box you'd get stdio and stdlib and a few others, but nothing like the wealth you're used to with Java. What few class libraries were available for C++ were generally licensed/proprietary so they were useless for developing open-source products.

Happily that's now changed, as the examples from the StackExchange show, even if some may find their usages a bit cryptic. Although I've not worked with those classes, I can pretty well tell what's going on.

These days, the closest to C/C++ I get is on the Arduino platform, where the standard development language is something very similar to C++. How similar, I've never investigated, although so far language and class definitions match up. Stuff like the set classes I'm pretty sure are not available, Arduino has its own String class (note case) and I doubt the existence of namespaces, although anything you can cram into an Arduino probably isn't going to be complex enough to need that.
 
Tim Holloway
Saloon Keeper
Posts: 25475
180
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note: The code examples above seem to be displaying with an isolated ">" on line 2 of each example. I have no idea why, since they're not there when I try to edit them out. Ignore them if you see them.
 
John Matthews
Rancher
Posts: 386
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:... the Arduino platform, where the standard development language is something very similar to C++.


It is C++: https://arduino.stackexchange.com/questions/86242/which-c-standard-does-the-arduino-language-support
 
Matthew Bendford
Rancher
Posts: 213
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First: I'd like to thank all of you for your input so far. I'll have a look into the suggestions and place some orders.

As it came up: I've seen C and C++ more as two dialects of the same family. But this doesn't seem true and C++ seem to have changed quite a lot since it first came up as C with classes. So, is it required to first learn C to understand its influence in C++ or is it ok to start straight with C++? Or could it maybe beneficial to not learn C but only C++?
It's not about cutting important corners. If there's stuff from C that's adviced to have understood before moving on with C++ and what's built on top of it - I guess that's the better way to go.
I'm mostly concerned about those things I'm not yet familiar with like pointers, structs, destructors, memory/resource management and all that. Sure, I understand the basic concept of a "function pointer" and can see its use cases in libraries and callbacks - but only in the object oriented way of Java's reflections - not as a simple int pointing to some memory location (although I guess down in the guts of a jvm both are the same?). I also know that I have to allocate and request new memory for every data I handle and that I have to release it back to the system by hand instead of rely on the language to handle that itself - but how hard is it actually to write clean non-leaking code without the risk of such as use-after-free?

Guess there'S quite a lot of basics I have to learn about it.
 
Saloon Keeper
Posts: 13886
314
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion, it's much better to treat the two as completely separate languages.

Especially because they are so similar, it's easy to get confused and you might want to use C++ features when you're writing C, or you might be using obsolete work-around from C in your C++ code that have a more idiomatic solution in C++.

If you don't intend to use C for hobby projects or your professional life, I suggest you start learning C++ without the burden of prior C knowledge. You can always come back to C later if you're interested.
 
Tim Holloway
Saloon Keeper
Posts: 25475
180
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Bendford wrote:
I'm mostly concerned about those things I'm not yet familiar with like pointers, structs, destructors, memory/resource management and all that. Sure, I understand the basic concept of a "function pointer" and can see its use cases in libraries and callbacks - but only in the object oriented way of Java's reflections - not as a simple int pointing to some memory location (although I guess down in the guts of a jvm both are the same?). I also know that I have to allocate and request new memory for every data I handle and that I have to release it back to the system by hand instead of rely on the language to handle that itself - but how hard is it actually to write clean non-leaking code without the risk of such as use-after-free?.



Pointers and structs are handled essentially the same in both C and C++, although in C++ you can use a struct as though it were a class whose member scope has been pre-set to public. I don't recommend doing that, though. if you want member protections in C++ use a class. Destructors are a C++ only thing as are Constructors. The legacy memory functions of C are still available in C++, but as a rule, you'd be better off using the new/delete operators. For one thing, you won't get constructor/destructor calls when using brute-force memory management. The biggest difference between C/C++ and Java is that you can't just de-reference things and expect the system to clean up, you absolutely positively HAVE to get rid of them yourself. Which is why Rust was invented.

I have never had a problem with memory leaks in either C  or C++, but then I am a Superior Being. Apparently many others are not so fortunate. My secret has always been to ensure that I have a direct path to the deletion of everything I create and don't just toss them to the wind.

Incidentally, Java can compact its heap objects because Java references don't directly point to things. In C/C++ pointers refer to absolute memory locations and once something has been allocated, it's going to stay there. This can sometimes lead to large holes in the heap space. One popular solution to that is "handles", which are pointers to pointers which is functionally equivalent to a Java reference. The original Macintosh used this concept.
 
Matthew Bendford
Rancher
Posts: 213
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As an quick update: After my shift I placed an order for a C++ book at a local book shop in between home and work - expected during next week. I ordered it cause I found a rather extensive online work on C by the same author I'll study over the weekend.

I also asked the lady to order the K&R or its C++ counterpart - but as I wasn't able to find any ISBN she wasn't able to find any hardcopy in her local repo. It's only limited to german language. Can anyone provide me with any ISBN for both the K&R and the C++ book by Stroustrup?

All Amazon got me was kindle e-book versions - but I don'T own one. Thanks in advance.
 
John Matthews
Rancher
Posts: 386
13
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amazon gives them;

K&R C:
ISBN-10 ‏ : ‎ 0131103628
ISBN-13 ‏ : ‎ 978-0131103627

C++:
ISBN-10 ‏ : ‎ 0321563840
ISBN-13 ‏ : ‎ 978-0321563842

That's from UK site (paperbacks - ok?), where I could buy them; not sure why you're not seeing them.
 
Tim Holloway
Saloon Keeper
Posts: 25475
180
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Bendford wrote:
All Amazon got me was kindle e-book versions - but I don'T own one. Thanks in advance.



There is a Kindle app for Windows available from Amazon. Also, if you have an Android tablet there's a Kindle reader. Of course, some of the Kindle devices are pretty cheap themselves.

In addition to the above, there's Calibre, which can manage your entire eBook collection with a very flexible search facilities. It can handle most document types and even convert between them. Kindle books are readable via a plugin.

For tablets, you may find it preferable to use one of the larger devices. For reading fiction, I find the 7-inch (18cm) size optimal, but for office-sized documents (PDFs)   and books with technical illustrations one really needs a 25cm or larger screen to be able to make sense of things.

Electronic documents have many benefits, although for learning/browsing purposes, I still like the dead tree format myself.

 
Matthew Bendford
Rancher
Posts: 213
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Matthews wrote:K&R C:
ISBN-13 ‏ : ‎ 978-0131103627

C++:
ISBN-13 ‏ : ‎ 978-0321563842

That's from UK site (paperbacks - ok?), where I could buy them; not sure why you're not seeing them.



Thanks a lot - I'll give them to the book shop lady on monday. With this she should be able to place an order.
As for why I wasn't able to find them myself: I don't know. Using the ISBNs and follow an amazon link from google then shows it - but for some reason if I search the books on my own and get presented only with the kindle versions (for whatever reason) I don't get them shown - and I don't even have prime ... weird.

As for why going the "dead wood" way AND ordering them in a book store rather than order online: Well, first I just prefer real books for technical stuff like programming or metal works and such. On the other hand: Getting something shipped by regular post service often come with some inconvience for me I can avoid by have a shop order it and I pick it up there - which is even more convenient when it's on my way to work anyway.
 
Tim Holloway
Saloon Keeper
Posts: 25475
180
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Notable hint: when you can't get the Dead Tree version (or don't want one), the same ISBN often works for the eBook edition.

In fact, often the main difference between the two may only be that one was imaged onto printing equipment from a master file whereas the other may simply be a copy of the master file (or ebook conversion thereof). This leads to eBooks that say "If the cover has been removed..."
 
Matthew Bendford
Rancher
Posts: 213
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I read thru the C stuff over the weekend I learned about & and * operators and the pass-by-reference. To me as someone only know Java well enough I'm only familiar with Javas pass-by-value where on primitives the actual primitive value is copied and for objects the base address in the reference. The only way I currently able to get my head around is comparing it to arrays and mutual objects like collections where the passed address "points" to the start of some memory which content can be changed be the callee.

So, I've given an interface which has the signature of char *ouput, int outputSize and const char *input. The datatype is said to be \0-terminated unicode utf-8, the outputSize the max length of output returned including the final \0 and the input what's given by the caller. From research this a C style signature and if I want to use C++ I have to convert the input to a C++ string and before returning back to a C char array with some simple to use functions. Is my understanding at least somewhat correct so far or am I already on a wrong track?
 
Tim Holloway
Saloon Keeper
Posts: 25475
180
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
C is much closer to the machine code than Java is, so instead of referencing objects - which don't really exist in the original C - you deal with pointers to "stuff" in memory. That "stuff" can be single primitive objects (but watch for the byte ordering!), arrays, or structs.

The "&" operator originally meant "address-of" so that if you had an int and said:
then you would setting px to te address of x. If you'd just done "px = x", then px would be set to the value of x and that's perfectly legal in C and C++ but usually not what you want. Unless x was 0, which is the null pointer value.

C does not normally copy objects onto the stack (pas-by-value), as the stack has a limited size and copying is an expensive operation so instead you'd pass a pointer. Or, in more modern times a reference (declared parameter type is "&something", which is just an inside-out pointer as far as the compiler is concerned.

Note that neither C nor C++ has an actual string data type. A String is a character array terminated by the NUL character ("\0"). These days there are both standard and third-party libraries that define string methods and/or classes, but they're not interchangeable. The only language-defined concession to strings is that if you declare a string literal (quoted sequence of characters), a section of memory is allocated by the compiler and pre-initialized to the string's value PLUS the trailing NUL.

And, incidentally, there's no guarantee that that section of memory is immutable the way Java Strings are. It's quite legal in the original C to say something like "abcde"[2] = 'X'; and end up with abXde in RAM!
 
John Matthews
Rancher
Posts: 386
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Bendford wrote:From research this a C style signature and if I want to use C++ I have to convert the input to a C++ string and before returning back to a C char array with some simple to use functions.


You don't have to - the C string library functions are available in C++ as well, and you could write virtually all of your C++ code in such a way that a C compiler would be happy with it.

Converting it to a C++ string just gives you more options in terms of library functions, some of which might be preferable depending on what your function has to do with the string. Do you actually have something in mind, or is this more of a theoretical question?
 
John Matthews
Rancher
Posts: 386
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Matthews wrote:Converting it to a C++ string just gives you more options in terms of library functions ...


And of course allows you to use the other C++ language features (classes, templates, etc. etc.) if you want/need to.
 
Matthew Bendford
Rancher
Posts: 213
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Matthews wrote:Do you actually have something in mind, or is this more of a theoretical question?


Yes, I actually have a specific goal in mind which can be done both in C as well as C++ - but as I want to extend my knowledge in both C and C++ I figured as I may extend the code in the future I chose C++ to start with.

I have a given application which provides a simple plugin interface: A plugin has to implement the function ExtCall(char *output, int maxLength, const char *input). The "strings" are specified as unicode using utf-8 encoding and are \0 terminated (both the input and output).

As I currently have very little knowledge of C and/or C++ my goal is to write a small java bridge using a tcp socket connection and a very simple protocol copied from another popular plugin. The actual string processing will be done in java along with some database backend.

Why: The existing plugins do not provide my required stuff - but writing the plugin in C ... well, as it's just back and forth of strings I prefer to implement it in java. Issue: There's no way to create a linux shared object elf from java source - at least none I'm aware of.
A friend of mine had C# in mind - but we're both not clever enough to get a working .so from c# source although following official M$ guides.
 
John Matthews
Rancher
Posts: 386
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, but I'm afraid I'm quite low level and don't understand much of that I was really curious as to what the C/C++ function would be doing with the strings, and if you stated that in your explanation above, apologies.
 
Matthew Bendford
Rancher
Posts: 213
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, basically: I have a given application that provides an extension interface. Although there're several extensions already existing none of them provide what I need - so I'd like to develope my own. Problem: The extension/plugin as to be given as a 64bit linux dynamic .so lib - which, although can be created from more languages than just C and C++, can not be achieved by using java - as java doesn't have a way to get compiled down into native code.
So the goal is to write a small wrapper .so which just implements the interface - takes the strings comming from the application - sends them over to java via a tcp socket - waits for java to process them and returns back whatever java returns within the given maxSize limit (which according to docs is currently limited to 10kb).
Sury, I may be able to develope the extension in C or C++ or any other language which allows me to compile a dynamic lib .so - but I only know java and a bit of PHP - both languages not match my requirement.
I want to take this opportunity to also start learning C and/or C++ - and hence the question. Will I stop learning C/++ after I got my small wrapper done? Hopefully not - as I'm investing about 150 bucks into a couple of hard copy books. Maybe I'll also start developing in C and/or c++ along with java or even maybe instead of java.
 
John Matthews
Rancher
Posts: 386
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So as far as the C++ is concerned, it sounds like there's not much, if any, actual string processing involved. That is, your function is just sending them over a socket, reading a response, and returning that response via the output buffer?

Does it need to create the socket, or is the socket just sitting there ready to be used?
 
Campbell Ritchie
Marshal
Posts: 75707
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Bendford wrote:. . . I learned about . . . pass-by-reference. . . .

I hope you didn't. C doesn't support pass by reference (C++ does, however).
Please start a new thread for that question about Strings.
 
John Matthews
Rancher
Posts: 386
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:C doesn't support pass by reference (C++ does, however).


Well... you can certainly write functions in C that take a reference to a structure argument; logically it does support pass by reference, via pointers. Physically, yes, the reference is the value of a memory address. But it's not like Java which doesn't support pass by reference at all (as I understand it).

C++ just makes it a bit more abstract; it's still a memory address on the stack, but now it's actually called a reference.
 
Campbell Ritchie
Marshal
Posts: 75707
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understood that is still pass by value, but it allows you to mimic pass by reference. As you said, Java® neither allows you to implement pass by reference nor to mimic it.
 
John Matthews
Rancher
Posts: 386
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I would say to someone coming from Java that C does support pass by reference, which is true, logically. Sure, mention that you need to understand a bit about pointers to do it, but they'll (presumably) be more interested in whether it's possible or not.
 
Campbell Ritchie
Marshal
Posts: 75707
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might have the same effect as pass by reference but the pointer is what is passed and that is passed by value. If you can think of a less confusing explanation . . . .
 
John Matthews
Rancher
Posts: 386
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it depends at what level you're describing it. If you're talking about differences between languages in terms of what they allow you to do, at a sort of 'API' level, then it's simple - C supports pass by reference, as does C++, as doesn't Java.

If you want to do go down into the 'implementation details', then yes, then you see the difference between C and C++.

It does depend on your audience, but in almost all cases I think it's fine to say that C supports pass by reference by providing pointers.
 
Campbell Ritchie
Marshal
Posts: 75707
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll buy that
 
Tim Holloway
Saloon Keeper
Posts: 25475
180
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In C/C++, the unary "&" is formally known as the "reference operator".

Technically, passing an object reference in Java has the same effect as passing an object reference in C++, as in both cases, subroutine/method logic that alters data based on the reference alters the object referenced and not a clone of that object, as it would, were the object passed on the stack. A practice, as I noted earlier, is generally discouraged.

I started with C (only option available), was an early adopter of C++, then moved to Java. I subscribe to Alan Kay's description of Java as "C++ without the mace and knives". I most commonly work with Arduino C++ these days. It's not the same as "standard C++" in that, for example, the Arduino libraries provide a String class that's completely different to the stdlib string. But given a choice, I'd generally use Java. Cramming a 100KB JVM into a 16k AVR device, however, ain't gonna work.
 
John Matthews
Rancher
Posts: 386
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Matthews wrote:But it's not like Java which doesn't support pass by reference at all (as I understand it).


I understood wrong, as shown here:
https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value

The top answer states what I'd believed, that Java is only pass by value. But then the example goes on to show what looks like pass by reference (in the 2nd block of code).

I'm with the 2nd answer in having a different understanding of what 'pass by value/reference' means. I agree with the 2nd answer's statement that "Java works exactly like C". No, the value of the caller's pointer/reference cannot be changed by the call, but that's not what we're interested in.

The pointer/reference is passed by value, which allows us to pass the pointed to/referenced object by reference. If your code does foo(aDog) and changes the object that aDog is referencing, then that to me is pass by reference.

 
It's a beautiful day in this neighborhood - Fred Rogers. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic