LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-14-2009, 01:55 PM   #16
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454

Quote:
Originally Posted by MTK358 View Post
I still don't quite understand, it, but it is starting to look like an object.

And I misunderstood the meaning of the curly braces last time, does the part in the curly braces really exist like a separate program or what?
The part in curly braces exists as a separate name space for stack (auto) variables and and function names - the latter is true for GNU C99, not for ANSI C99.

I'll try to develop the example further, i.e. to really make it an object with data encapsulation, constructor and destructor.
 
Old 12-14-2009, 02:30 PM   #17
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Sergei Steshenko View Post
I'll try to develop the example further, i.e. to really make it an object with data encapsulation, constructor and destructor.
OK, that would be interesting.

Is polymorphism possible with this?
 
Old 12-14-2009, 03:29 PM   #18
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I made another example with my OO system, now with abstract classes and destructors. Also demonstrates passing objects to a function:

Code:
$ cat oo.c
#include <stdlib.h>
#include <stdio.h>

/* ******** Abstract Class Animal ******** */

#define Animal_TEMPLATE \
	void (*makeNoise)(void*); \
	void (*destroy)(void*);
struct Animal { Animal_TEMPLATE };
typedef struct Animal Animal;

void Animal_destroy(void* this) {
	free(this);
}

#define Animal_INIT \
	new->makeNoise = NULL; \
	new->destroy = Animal_destroy;

/* ******** Class Cat ******** */

#define Cat_TEMPLATE Animal_TEMPLATE
struct Cat { Cat_TEMPLATE };
typedef struct Cat Cat;

void Cat_makeNoise(void* this) {
	printf("Meow!\n");
}

#define Cat_INIT Animal_INIT \
	new->makeNoise = &Cat_makeNoise;

Cat* new_Cat() {
	Cat* new = malloc(sizeof(Cat));
	Cat_INIT;
	return new;
}

/* ******** Class Dog ******** */

#define Dog_TEMPLATE Animal_TEMPLATE
struct Dog { Dog_TEMPLATE };
typedef struct Dog Dog;

void Dog_makeNoise(void* this) {
	printf("Woof!\n");
}

#define Dog_INIT Animal_INIT \
new->makeNoise = &Dog_makeNoise;

Dog* new_Dog() {
	Dog* new = malloc(sizeof(Dog));
	Dog_INIT;
	return new;
}

/* ******** Main ******** */

void poke(Animal* a) {
	printf("Poking Animal:\n");
	a->makeNoise(a);
}

int main() {
	Animal* a = (Animal*) new_Cat();
	Animal* b = (Animal*) new_Dog();

	poke(a);
	poke(b);

	a->destroy(a);
	b->destroy(b);

	return 0;
}
$ gcc -o oo oo.c
$ valgrind ./oo
==13725== Memcheck, a memory error detector
==13725== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==13725== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==13725== Command: ./oo
==13725== 
Poking Animal:
Meow!
Poking Animal:
Woof!
==13725== 
==13725== HEAP SUMMARY:
==13725==     in use at exit: 0 bytes in 0 blocks
==13725==   total heap usage: 2 allocs, 2 frees, 32 bytes allocated
==13725== 
==13725== All heap blocks were freed -- no leaks are possible
==13725== 
==13725== For counts of detected and suppressed errors, rerun with: -v
==13725== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)
$
 
Old 12-16-2009, 09:19 AM   #19
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I recently discovered this:

www.ooc-lang.org

I wonder how that works?
 
Old 12-16-2009, 04:08 PM   #20
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Is it possible to count the number of pointers to a block of allocated memory?

This would allow me to have a global variable containing a structure defining things common to all instances of a class, so that that information will not be redundantly held in each instance. The variable will initially be NULL, if a constructor is called and the class is NULL, it is created, and when all instances of the class are destroyed, the class variable will bee freed and set to NULL again.
 
Old 12-16-2009, 05:23 PM   #21
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
Is it possible to count the number of pointers to a block of allocated memory?

This would allow me to have a global variable containing a structure defining things common to all instances of a class, so that that information will not be redundantly held in each instance. The variable will initially be NULL, if a constructor is called and the class is NULL, it is created, and when all instances of the class are destroyed, the class variable will bee freed and set to NULL again.
Sorry, I'm busy lately. Still, I found that, on the one hand, GCC closures are not good enough, on the other hand, Apple in its "C" language family + LLVM has what they call "blocks" - this should be sufficient.

Reference counting is already there - IIUC.

Relevant links:

http://lists.cs.uiuc.edu/pipermail/c...st/002670.html ;

http://llvm.org/releases/2.4/docs/ReleaseNotes.html :

Quote:
llvm-gcc now supports a C language extension known as "Blocks". This feature is similar to nested functions and closures, but does not require stack trampolines (with most ABIs), and supports returning closures from functions that define them. Note that actually using Blocks requires a small runtime that is not included with llvm-gcc.
 
Old 12-16-2009, 07:15 PM   #22
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by MTK358 View Post
Is it possible to count the number of pointers to a block of allocated memory?
I thought that the class can have a member that stores the amount of instances, and object constructors and destructors can increment/decrement them.

If the constructor sees that the class is NULL, it creates a new one.

If the destructor sees that the count is 0, free()'s the class.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
GCC compile problem:struct A have a member variable which is just a struct type name? leon.zcom Programming 3 04-18-2008 04:40 PM
How to initialize a static array of a class in a static member function lali.p Programming 9 02-16-2008 09:27 AM
assign int to a struct member shogun1234 Programming 1 07-30-2007 02:18 PM
struct member alignment wmoti Programming 2 10-10-2005 05:24 AM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration