473,883 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I Keep Switching Back...

I'm looking at other languages. Some of them fool me into thinking
they're useful, and I change my home page to them... 'comp.lang.c++'
for instance. But I always end up switching it back to comp.lang.c.
:-D

When I redid my code into C++ recently, I saw some amazing bogosities.
Such as member functions being called before the constructors ran, and
member functions being called for NULL objects. Enough to make me
switch back to C, where function calling order is controlled by the
programmer, and proceeds in a safe and sane order. :-O

So - I'm baaaack. :-D I haven't encountered a good enough language yet
that has made me switch my home page permanently.

--Kamilche
Nov 13 '05 #1
32 2604
kl*******@home. com (Kamilche) writes:
When I redid my code into C++ recently, I saw some amazing bogosities.
Such as member functions being called before the constructors ran, and
member functions being called for NULL objects. Enough to make me
switch back to C, where function calling order is controlled by the
programmer, and proceeds in a safe and sane order. :-O


C++ has a really, really scary amount of complexity. Personally,
a few months ago I embarked on trying to learn it well enough to
use it for real projects, by way of buying all the best books on
it and summarizing their advice, with citations, into a "best
practices guide" for myself. So far, I have over 10,000 words
citing 18 books and articles, and I still have quite a way to go.
--
"I am worth much more brick-bat!!"
--Uday Joshi
Nov 13 '05 #2
Kamilche wrote:

I'm looking at other languages. Some of them fool me into thinking
they're useful, and I change my home page to them... 'comp.lang.c++'
for instance. But I always end up switching it back to comp.lang.c.
:-D

When I redid my code into C++ recently, I saw some amazing bogosities.
Such as member functions being called before the constructors ran, and
member functions being called for NULL objects. Enough to make me
switch back to C, where function calling order is controlled by the
programmer, and proceeds in a safe and sane order. :-O

So - I'm baaaack. :-D I haven't encountered a good enough language yet
that has made me switch my home page permanently.

Welcome back. We've missed you.
--
Joe Wright mailto:jo****** **@earthlink.ne t
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #3
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote in message news:<be******* ***@hercules.bt internet.com>.. .
Welcome back to the comp.lang.c newsgroup.

We've moved the coffee machine to make room for another server. You'll now
find it in the cafeteria, next to the swing doors.

Your cloakroom peg has been re-assigned, so please apply for a new one when
you pick up your swipe card.

Don't forget to unsubscribe from comp.lang.c++ :-)


:-D
Kamilche plops a plate of cookies next to the water cooler, and
surveys her old digs happily.
Nov 13 '05 #4
Ben Pfaff <bl*@cs.stanfor d.edu> wrote in message news:<87******* *****@pfaff.Sta nford.EDU>...
kl*******@home. com (Kamilche) writes:

C++ has a really, really scary amount of complexity. Personally,
a few months ago I embarked on trying to learn it well enough to
use it for real projects, by way of buying all the best books on
it and summarizing their advice, with citations, into a "best
practices guide" for myself. So far, I have over 10,000 words
citing 18 books and articles, and I still have quite a way to go.


Yeah, I believe it. But when push comes to shove, what I saw looked
and smelt more like bugs than complexity. I saw member functions being
called before the constructor was called, and learned you can't rely
on constructor initialization order for global objects. So I turned
them into pointers instead, and discovered that C++ will call member
functions for objects that DON'T EVEN EXIST. They can be NULL, and you
can still call their functions. The function gets a NULL 'this'
pointer!

The following code compiles and runs, and A() executes. Apparently the
object orientation of C++ is a thin scum over procedural programming,
UNRELIABLE procedural programming at that, to allow code like this to
execute. That's when I put it down and walked away from it.

class My
{
public:
void A()
{
if (this==NULL) printf("NULL Pointer ");
}
};

void main()
{
My *ptr=NULL;
ptr->A();
}

I feel that by going to C++, I'd be giving up the complexities
inherent in large C programs, for the undiscovered bugs in C++... ya
know? I know object oriented programming from other languages, such as
VB... and this is just not the way it should work.

I've been doing a huge amount of experimenting with hierarchical
objected oriented C systems. Right now, I'm looking at code
generation, or possibly heavy use of configuration files, to reduce
complexity... something where I can keep the high level 'big picture'
pseudocode in a text file, and either load it at startup, or auto
generate code from it.
Nov 13 '05 #5
kl*******@home. com (Kamilche) wrote in message news:<88******* *************** ****@posting.go ogle.com>...
I'm looking at other languages. Some of them fool me into thinking
they're useful, and I change my home page to them... 'comp.lang.c++'
Currently I mostly use C for my work, but I have some 10 years of C++
experience too. I also like other languages like to study scheme,
haskell et cetera, and my conclusion is that there is no "one
language" that is good for everything. C is a great language,
and so is C++.
When I redid my code into C++ recently, I saw some amazing bogosities.
Such as member functions being called before the constructors ran,
What did you do?

Perhaps something like the following?
Foo* x;
x->member_func( );
and
member functions being called for NULL objects.
The caller is responsible for initializing the objects, calling member
functions for NULL objects is just as much undefined behavior as
the "C" constructs:

int i;
i = i++ + ++i;

and

int* i;
*i = 42;
Enough to make me
switch back to C, where function calling order is controlled by the
programmer, and proceeds in a safe and sane order. :-O
You switch to C because you can't handle the responsibility to initialize
the objects before you use them? Perhaps pascal is a better language
for you...
The following code compiles and runs, and A() executes.
It doesn't for me. I get the following compile errors:

~% g++ -pedantic -ansi -Wall asdf.cc
asdf.cc: In method `void My::A()':
asdf.cc:6: `NULL' undeclared (first use this function)
asdf.cc:6: (Each undeclared identifier is reported only once
asdf.cc:6: for each function it appears in.)
asdf.cc:6: `printf' undeclared (first use this function)
asdf.cc: At top level:
asdf.cc:11: warning: return type for `main' changed to `int'

To fix it I have to include <cstdio>, change printf to std::printf, and
change "void main" to "int main".
Apparently the
object orientation of C++ is a thin scum over procedural programming,
UNRELIABLE procedural programming at that, to allow code like this to
execute. That's when I put it down and walked away from it.

class My
{
public:
void A()
{
if (this==NULL) printf("NULL Pointer "); // This can never be NULL, this test is not necessary. }
};

void main()
{
My *ptr=NULL;
ptr->A();
}


Dereferencing a NULL pointer. I don't understand why you are upset.
This is just as undefined behavior in C++ as the following C program
would be:

#include <stdio.h>

typedef void (*Func)(void);

struct Foo {
Func A;
};

int main ()
{
Foo* ptr = NULL;
ptr->Func();
return 0;
}

Anything can happen in the C-program above, including the output
of some text regarding null-pointers.

I can understand arguments about C++ being a complex language
with lots of things happening "under the hood", but supporting such
arguments with code that invokes undefined behavior, and would
do so even in C, if the constructs had been legal in C is silly.

/Niklas Norrthon
Nov 13 '05 #6
On 10 Jul 2003 00:26:13 -0700, kl*******@home. com (Kamilche) wrote:
....

The following code compiles and runs, and A() executes. Apparently the
object orientation of C++ is a thin scum over procedural programming,
UNRELIABLE procedural programming at that, to allow code like this to
execute. That's when I put it down and walked away from it.

class My
{
public:
void A()
{
if (this==NULL) printf("NULL Pointer ");
}
};

void main()
C++ requires int main, just as C does.
{
My *ptr=NULL;
ptr->A();
}


This is undefined behavior in C++. You are dereferencing a NULL pointer,
which is undefined in C++, just as it is in C. As a clc reader, you know
that anything can happen when undefined behavior is invoked, as in the
following analagous C fragment, also dereferencing a NULL pointer.

char* x = NULL,
strcpy(x, "Hello");

Both code fragments could run "normally", format your hard disk, or
cause a power outage in Birmingham, Alabama.

C++ has problems with undefined behavior similar to C. As Ben Pfaff
commented, C++ is also vastly more complicated than C, but if one writes
good, legal code (avoiding undefined behavior, among other things), C++
is a very useful language.

I would suggest that you learn to write legal C++ before you condemn it.

Regards,
Bruce Wheeler

Nov 13 '05 #7
In article <88************ *************@p osting.google.c om>
Kamilche <kl*******@home .com> writes:
Yeah, I believe it. But when push comes to shove, what I saw looked
and smelt more like bugs than complexity. I saw member functions being
called before the constructor was called, and learned you can't rely
on constructor initialization order for global objects.
(This is probably the wrong newsgroup to gripe about such things,
but as far as I am concerned, this ordering problem is just the
tip of the iceberg.)
So I turned them into pointers instead, and discovered that C++ will
call member functions for objects that DON'T EVEN EXIST. They can be
NULL, and you can still call their functions. The function gets a NULL
'this' pointer!
Yes.
The following code compiles and runs, and A() executes. Apparently the
object orientation of C++ is a thin scum over procedural programming ...
Actually, it is a fairly thick layer. But for simple cases like
this one, there is a direct conversion from C++ to C:
class My
{
public:
void A()
{
if (this==NULL) printf("NULL Pointer ");
}
};
(You need to #include at least one header, and the printf here
should really be "std::cout <<", etc. But never mind that.) Here
the "class function" A() that is a member of the data structure
called "My" is obtained simply by gluing together the type ("My")
and the function name ("A"), which you can do manually in C by
spelling it My_A(). Of course, you give up the automatic "if the
type changes, the function name changes too" part that C++ gives
you.
void main()
{
My *ptr=NULL;
ptr->A();
}


This needs to be "int main()" just as in C.

In any case, the C++ compiler here just glues together (in a
more error-resistant way) the name "My" and the name "A", so
this is like writing the C code:

struct My { char dummy; };
/* dummy element required only because C forbids empty struct */

void My_A(struct My *this) {
if (this == NULL)
printf("NULL pointer\n");
}
int main(void) {
struct My *ptr = NULL;
My_A(ptr);
}

If you change member function A to "virtual", it stops working.
This is because a "virtual" member function is not built by name
-- i.e., we no longer just say "well gosh, `ptr' has type `My' so
we just call My_A() and supply `ptr' as a secret argument for the
`this' parameter". Instead, there is now a "virtual function table"
attached to the data type.

This is where C++ actually gives up something you can do manually
in C. For various (good and bad) reasons, the virtual function
table is essentially an operations vector, and the data structures
-- objects of type "My" -- point to the table. So in C we might
now write:

struct ops_for_My {
void (*A)(struct My *this);
};
static struct ops_for_My ops_for_My = { A };
struct My {
struct ops_for_My *vtable;
/* other data elements go here as needed */
};

When you create an actual object of type "My", the compiler will
fill in the "vtable pointer":

/* C code loose-equivalent of the C++ stuff */
ptr = ...;
ptr->vtable = &ops_for_My;

A later call of the form:

ptr->A(); // in C++

translates to the C call:

ptr->vtable->A(ptr);

and if "ptr->vtable" points to the default table, that calls the
default "My_A()" function. (A derived class that overrides the
default My_A() function just requires another ops table, with a
different "A" pointer.)

Although the need is somewhat specialized and occasional, note that
if you have written this in C, you now have a name for the vtable(s)
that implement the "virtual functions" for anything that is, or is
derived from, a "My". Clearly, if ptr==NULL, this:

ptr->vtable->A(ptr);

is not going to work -- but in C, we can do this instead, in
those rare cases where we want to:

ops_for_My.A(NU LL);

In C++ one has to resort to any number of workarounds -- not that
they are particularly horrible or anything; but it is annoying to
know that there is a virtual function table all set up, yet you
are not allowed to access it. You *must* have an instance of the
class in order to access that class's virtual function table.
(The easiest workaround is thus to have a static "dummy" instance.)

In any case, both regular "class functions" and "virtual functions"
are easy to do in C, using these techniques. C++ merely automates
some of the drudge-work, avoiding the opportunity to get it wrong,
but also requiring you to give up some control over it.
--
In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://67.40.109.61/torek/index.html (for the moment)
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 13 '05 #8
On 10 Jul 2003 01:30:46 -0700, ni***@passagen. se (Niklas Norrthon)
wrote:

A very nice post.

I posted something similar in a different subthread.

However,
class My
{
public:
void A()
{
if (this==NULL) printf("NULL Pointer ");// This can never be NULL, this test is not necessary.


Actually, 'this' can be NULL, as the method invocation is undefined
behavior if 'this' is NULL. Anything can happen. Some C++
implementations (VC++ in particular) actually have such code in tests in
library code.
}
};

void main()
{
My *ptr=NULL;
ptr->A();
}


Dereferencin g a NULL pointer. I don't understand why you are upset.
This is just as undefined behavior in C++ as the following C program
would be:

#include <stdio.h>

typedef void (*Func)(void);

struct Foo {
Func A;
};

int main ()
{
Foo* ptr = NULL;
ptr->Func();
return 0;
}

Anything can happen in the C-program above, including the output
of some text regarding null-pointers.


Correspondingly , anything (including invocation of the method) can
happen in the C++ example above, because it is also undefined behavior.

I can understand arguments about C++ being a complex language
with lots of things happening "under the hood", but supporting such
arguments with code that invokes undefined behavior, and would
do so even in C, if the constructs had been legal in C is silly.

/Niklas Norrthon


Regards,
Bruce Wheeler

Nov 13 '05 #9
Kamilche <kl*******@home .com> wrote:
Ben Pfaff <bl*@cs.stanfor d.edu> wrote in message news:<87******* *****@pfaff.Sta nford.EDU>...
kl*******@home. com (Kamilche) writes:

C++ has a really, really scary amount of complexity. Personally,
a few months ago I embarked on trying to learn it well enough to
use it for real projects, by way of buying all the best books on
it and summarizing their advice, with citations, into a "best
practices guide" for myself. So far, I have over 10,000 words
citing 18 books and articles, and I still have quite a way to go.
Yeah, I believe it. But when push comes to shove, what I saw looked
and smelt more like bugs than complexity. I saw member functions being
called before the constructor was called, and learned you can't rely
on constructor initialization order for global objects. So I turned
them into pointers instead, and discovered that C++ will call member
functions for objects that DON'T EVEN EXIST. They can be NULL, and you
can still call their functions. The function gets a NULL 'this'
pointer!


You somehow drew the conclusion that C++ doesn't allow for programmers
to write buggy software, where would this come from? In C++ as in C
you're responsible for writing legal code, not the language.

The following code compiles and runs, and A() executes. Apparently the
object orientation of C++ is a thin scum over procedural programming,
UNRELIABLE procedural programming at that, to allow code like this to
execute. That's when I put it down and walked away from it.

class My
{
public:
void A()
{
if (this==NULL) printf("NULL Pointer ");
}
};

void main()
{
My *ptr=NULL;
ptr->A();
}

I feel that by going to C++, I'd be giving up the complexities
inherent in large C programs, for the undiscovered bugs in C++... ya
know? I know object oriented programming from other languages, such as
VB... and this is just not the way it should work.


I ask myself, why you want to give up C at all. If C is inappropriate
for a problem, use something different, but if it does the job (or if
you think you can do the job in C) why not just stick with it?

A mechanic wouldn't use a hammer when a screwdriver is needed, allthough
I bet that he/she could solve the job with a hammer too.

--
Z (Zo**********@d aimlerchrysler. com)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2411
by: Johann Blake | last post by:
The following is a bug I have discovered using tab pages and threads and I am looking for a workaround. Create a new Windows Forms application and add a tab control with two tab pages. Add a label to each tab page. On the first tab page add a button. When the button is pressed the code behind the button creates a thread and starts that thread. The only thing that the thread is to do is to switch from the first tab page to the 2nd tab...
5
2040
by: JRB | last post by:
Hi, I'm creating a small C#program that communicates through the serial port. I have a separate thread that continuously takes in data from an external device in a while loop. The problem is that when I try and run another program, open a menu, or anything, I miss getting bytes. If I set the threads priority to above normal it works fine, but the rest of the program slows down dramatically. Is there any command or way to ensure that a block...
10
7254
by: EOZyo | last post by:
Hi, i'm trying to set pagination for a search i run on my website, i'll try to explain the easiest i can: When i click the search button on search.php, data is received and stored in variables within results.php, MySQL structure seems to work as expected, although i get some problems: As soon as i click on the "Page 2" Link, i get nothing, neither results nor page numbers.
3
4392
by: Simon Verona | last post by:
I have a parent form which contains a toolbar. The toolbar controls the loading and switching to of MDI child forms. The code for the toolbar click event and one of the subroutines that loads and shows a child form is: Private Sub tbMenu_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles tbMenu.ButtonClick Dim btn As System.Windows.Forms.ToolBarButton For Each btn In...
1
5748
by: garry.oxnard | last post by:
Can anyone help me to solve a problem which involves switching from Access to Excel (then back to Access) programatically please? I have an Excel template which, on open, also opens an Access database - containing a list of addresses. Sequence of events is = (1) Excel template opens in its default XXX.xls filename. (2) Code runs to save the spreadsheet as XXX.xls. (3) User clicks a button to open an Access database containing an
15
6728
by: l3vi | last post by:
I have a new system Im building that stores entries of what people are searching for on my sites. I want to be able to keep records of how many times a keyword was searched for daily, and from that I can calculate weekly and monthly. At this point I have one entry per search phrase with the number of hits the search phrase has gotten, and the last time it was updated. As I start to take the program out of testing and move in more...
1
1860
by: brendan.wong | last post by:
hello. i've just installed the most version of xampp, but i still have the previous version just in case. i've simply renamed the previous version's root directory to "lampp_orig." so, does anyone know how to switch back and forth? thanks
4
1465
by: james00 | last post by:
Switching Page Layouts!!! Does anyone have any idea how to create a script for Switching Page Layouts. I know how to create one for Style Sheet Switcher (http://www.dynamicdrive.com/dynamicindex9/stylesheetswitcher.htm) but not for PAGE layouts. I mean I can make a menu to switch between a Wide Page Layout and Narrow Page Layout but I want that to be stored into a cookiee so that will show up when the visitor check back again. THANK...
4
2149
by: adlloyd | last post by:
Hi all, I've got an application that's written in C++ making use of MFC (VS6). Its purpose is to process SMS messages received from a GSM modem connected via a serial port (USB connection). The serial communications are handled entirely in their own thread. For the most part this works perfectly.However, there appears to be a problem where multiple instances of the application are running in different Windows XP environments. It seems...
0
10743
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10847
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10416
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9574
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7971
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7129
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5991
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3233
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.