473,471 Members | 2,533 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

recursive declaration of variables

Hi, I have two questions I would like to ask.

1. How can a use a pointer to an undeclared variable
in another variable?
for example:

struct recurs {
int data;
struct recurs *link;
};

static recurs a = {5, &b};
static recurs b = {4, &a};

2. I would like to have extra data on the end of a variable.
How can I do this?

struct varsize {
int data[];
}

static varsize a = {1, 2, 4, 0}; //0 means end of the array

I get an error for both.

Thanks,
Kristof Bastiaensen
Jul 22 '05 #1
16 1585
> struct recurs {
int data;
struct recurs *link;
};
static recurs a;
static recurs b;
void Init()
{
a.line = &b;
b.link=&a;
}

When you create a, b does not exist yet, so you can't point to it.

2. I would like to have extra data on the end of a variable.
How can I do this?

struct varsize {
int* data;
}

static varsize a = {1, 2, 4, 0}; //0 means end of the array

class varsize
{
public:
varsize(int* pData)
{
long n;
for (n=0; pData[n]!=0; n++);
data=new int[n];
memcpy(data, pData, n*sizeof(int));
}
~varsize() {delete [] data;}
int* data;
};

static int pData[] = {1,2,3,0};
varsize nnn(pData);

HTH,

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
Jul 22 '05 #2

"Kristof Bastiaensen" <kr*****@vleeuwen.org> wrote in message
news:pa***************************@vleeuwen.org...
Hi, I have two questions I would like to ask.

1. How can a use a pointer to an undeclared variable
in another variable?
for example:

struct recurs {
int data;
struct recurs *link;
};

static recurs a = {5, &b};
static recurs b = {4, &a};

2. I would like to have extra data on the end of a variable.
How can I do this?

struct varsize {
int data[];
}

static varsize a = {1, 2, 4, 0}; //0 means end of the array

I get an error for both.

Thanks,
Kristof Bastiaensen


Neither of these is possible in C++, but ...

You can do 2 like this

struct varsize {
int* data;
}

int varsize_data[] = {1, 2, 4, 0};
static varsize a = {varsize_data};

You can do 1 like this

struct recurs {
int data;
recurs *link;
};

static recurs a = {5, 0};
static recurs b = {4, &a};
struct fixup_recurs_a
{
fixup_recurs_a() { a->link = &b; }
} fixup;

I'm not sure I'd recommend either method though. There are probably better
C++ ways of doing whatever it is that you really want.

john
Jul 22 '05 #3
>
static recurs a = {5, 0};
static recurs b = {4, &a};
struct fixup_recurs_a
{
fixup_recurs_a() { a->link = &b; }
} fixup;


That should be

fixup_recurs_a() { a.link = &b; }

john
Jul 22 '05 #4
On Wed, 30 Jun 2004 14:19:04 +0200, Gernot Frisch wrote:

Hi,
struct recurs {
int data;
struct recurs *link;
};
static recurs a;
static recurs b;
void Init()
{
a.line = &b;
b.link=&a;
}

When you create a, b does not exist yet, so you can't point to it.


That's in fact what I want to avoid. Because a and b are
global variables (inside the file), they exist at the same time,
only the compiler doesn't know about it. The thing is that I
have a lot of static data, and I don't want extra code to
initialize them, when they are already in memory.
2. I would like to have extra data on the end of a variable.
How can I do this?

struct varsize {
int* data;
}
Here I wrote "int[] data; ", which is not the same. I want
an array here, not a pointer. I could get away with an extra
pointer, but I want to avoid another indirection.
perhaps it should be
static varsize a = {{1, 2, 4, 0}};

static varsize a = {1, 2, 4, 0}; //0 means end of the array

class varsize
{
public:
varsize(int* pData)
{
long n;
for (n=0; pData[n]!=0; n++);
data=new int[n];
memcpy(data, pData, n*sizeof(int));
}
~varsize() {delete [] data;}
int* data;
};

static int pData[] = {1,2,3,0};
varsize nnn(pData);

HTH,


Also here I don't want the compiler to produce any code.
I don't see any reason to call new or malloc. The compiler
should be able to just put my data in the object file,
without any extra code. It works for arrays:

int a[] = {1, 2, 5, 4, 0};

but not when the array is part of a struct (see my code).

Thanks,
KB
Jul 22 '05 #5

"Kristof Bastiaensen" <kr*****@vleeuwen.org> wrote in message
news:pa****************************@vleeuwen.org.. .
[SNIP]

That's in fact what I want to avoid. Because a and b are
global variables (inside the file), they exist at the same time,
only the compiler doesn't know about it. The thing is that I
have a lot of static data, and I don't want extra code to
initialize them, when they are already in memory.


Hmm, there is no such a thing as a free meal. John already gave you a
solution to your problem, however this awful lot of static and global data
seems very much like a screwed design. Hence, you might rethink your design
instead of spending time on conjuring up such fixes.

Regards
Chris
Jul 22 '05 #6

"Kristof Bastiaensen" <kr*****@vleeuwen.org> schrieb im Newsbeitrag
news:pa****************************@vleeuwen.org.. .
On Wed, 30 Jun 2004 14:19:04 +0200, Gernot Frisch wrote:

Hi,
struct recurs {
int data;
struct recurs *link;
};

static recurs a;
static recurs b;
void Init()
{
a.line = &b;
b.link=&a;
}

When you create a, b does not exist yet, so you can't point to it.


That's in fact what I want to avoid. Because a and b are
global variables (inside the file), they exist at the same time,
only the compiler doesn't know about it. The thing is that I
have a lot of static data, and I don't want extra code to
initialize them, when they are already in memory.


// For an (alomost) linked list this can be used:
struct st
{
int n;
st* previous;
};

st a={0,0};
st b={1,&a};
st c={2,&b};
st* Begin = &a;
st* End = &c;

But this code is total crap, as is the idea you try to sell us. Use
classes and initialize them using the constructor. That's what they
are made for.

-Gernot


Jul 22 '05 #7
On Wed, 30 Jun 2004 15:22:25 +0200, Chris Theis wrote:

"Kristof Bastiaensen" <kr*****@vleeuwen.org> wrote in message
news:pa****************************@vleeuwen.org.. .
[SNIP]

That's in fact what I want to avoid. Because a and b are
global variables (inside the file), they exist at the same time,
only the compiler doesn't know about it. The thing is that I
have a lot of static data, and I don't want extra code to
initialize them, when they are already in memory.


Hmm, there is no such a thing as a free meal. John already gave you a
solution to your problem, however this awful lot of static and global data
seems very much like a screwed design. Hence, you might rethink your design
instead of spending time on conjuring up such fixes.

Regards
Chris


Hi,

I didn't see any message from 'John', maybe there is something
wrong with my newsserver (or program).
I cannot see why using static data is a screwed design.
If you inline images in your program, is that a screwed design?
The reason I put it inline in my code is that all the data is needed
by the program anyway, and I don't want to write a parser to
load it from a file.

regards,
Kristof

Jul 22 '05 #8

"Kristof Bastiaensen" <kr*****@vleeuwen.org> wrote in message
news:pa***************************@vleeuwen.org...
[SNIP]

Hi,

I didn't see any message from 'John', maybe there is something
wrong with my newsserver (or program).
Probably your newsserver has not updated yet.
I cannot see why using static data is a screwed design.
Well one cannot generally say that using static data is screwed design but
the large amount that you indicated hints at screwed design.
If you inline images in your program, is that a screwed design?
IMHO yes it is. Hardcoding stuff is generally something which takes away
your flexibility and you donīt really gain something in return.
The reason I put it inline in my code is that all the data is needed
by the program anyway, and I don't want to write a parser to
load it from a file.


Thatīs a way ("Iīll just put it in here, so I save a little time and
effort") of software development Iīve seen widely spread and it usually
doesnīt pay off in the long run. In my experience itīs much better to invest
some time to get a fully flexible (and more generic and thus reusable)
solution. Otherwise you might (and most probably will) find yourself doing
the same thing at least twice.

Donīt get me wrong, this is not meant as criticism but just as a hint ;-)

Best regards
Chris


Jul 22 '05 #9
On Wed, 30 Jun 2004 15:56:48 +0200, Gernot Frisch wrote:
// For an (alomost) linked list this can be used:
struct st
{
int n;
st* previous;
};

st a={0,0};
st b={1,&a};
st c={2,&b};
st* Begin = &a;
st* End = &c;

But this code is total crap, as is the idea you try to sell us. Use
classes and initialize them using the constructor. That's what they
are made for.

-Gernot


I am not trying to sell anything. If you don't need such a thing,
that's fine for me, but I need it for my program.
It is really just data, no class or dynamic object or whatsoever.
Using constructors and classes is just overkill.
I found that my point two (flexible array members)
works as a gcc extension, and is also defined in the C99 standard
(I am not sure about c++).

As for point one, it works for global (non static) data like this:

struct mystruct {
int i;
struct mystruct *p;
};

extern mystruct a, b;

mystruct a = {1, &b}, b = {2, &a};

The problem is that I want the data to be only visible in
the file, to avoid name clashes,
and because the actual names of the variables
are not important for the rest of the program.
What I would like is a way to predeclare a static variable.
Declaring 'extern static' is not possible.
Maybe the best solution is to make them non-static, but
give them an unlikely name, like 'file1_a'.

It seems that my newsserver doesn't show all posts, so
if I didn't answer to a brilliant solution, it is possible
that I didn't see it.

Regards,
Kristof Bastiaensen
Jul 22 '05 #10

"Kristof Bastiaensen" <kr*****@vleeuwen.org> wrote in message
news:pa****************************@vleeuwen.org.. .
[SNIP]
The problem is that I want the data to be only visible in
the file, to avoid name clashes,
and because the actual names of the variables
are not important for the rest of the program.
What I would like is a way to predeclare a static variable.
Declaring 'extern static' is not possible.
Maybe the best solution is to make them non-static, but
give them an unlikely name, like 'file1_a'.

[SNIP]

If your only concern is a possible name clash then why donīt you just put
them in a unique namespace?

Cheers
Chris
Jul 22 '05 #11
Hi, everybody.

Thanks for responding.
I finally found the solution myself:

On Wed, 30 Jun 2004 14:00:58 +0200, Kristof Bastiaensen wrote:
Hi, I have two questions I would like to ask.

1. How can a use a pointer to an undeclared variable
in another variable?
for example:

struct recurs {
int data;
struct recurs *link;
};

static recurs a = {5, &b};
static recurs b = {4, &a};

Anonymous namespaces will do the job:

namespace
{
struct recurs {
int data;
struct recurs *link;
};

extern recurs b;
recurs a = {5, &b};
recurs b = {4, &a};
}
2. I would like to have extra data on the end of a variable.
How can I do this?

struct varsize {
int data[];
}

static varsize a = {1, 2, 4, 0}; //0 means end of the array

I get an error for both.


Apparently this is possible for C99, but not for C++.
I guess the only way is to declare the array outside, and
use a pointer.
This way is more typing and less efficient, but at least
it is portable.

Kristof
Jul 22 '05 #12
On Wed, 30 Jun 2004 14:19:04 +0200 in comp.lang.c++, "Gernot Frisch"
<Me@Privacy.net> wrote,
static recurs a;
static recurs b;
void Init()
{
a.line = &b;
b.link=&a;
}

When you create a, b does not exist yet, so you can't point to it.


Very bad answer. b must be forward declared, but it certainly does
exist at initialization time and you can easily refer to it then.

extern recurs b;
static recurs a = {5, &b};
static recurs b = {4, &a};

As someone else noted, file scope static is deprecated and namespace is
preferred.

Jul 22 '05 #13
Kristof Bastiaensen <kr*****@vleeuwen.org> wrote in message news:<pa***************************@vleeuwen.org>. ..
[snip]
static recurs a = {5, &b};
static recurs b = {4, &a};


Others have hinted at re-doing your design to avoid this sort
of thing. Let me make a hint about direction of redesign.

These two things are probably suggestive of some larger issue
in your code. I doubt you'd bother with this just to store the
numbers 4 and 5. So you seem to have reduced your actual issue
from "how do I do this larger problem?" to "I've got a way to
do it that involves this smaller problem, how do I do this smaller
problem?"

Step back from struggling with the small issue and ask, why does
the thing "a" need to know about the thing "b"? And why does
the thing "b" *ALSO* need to know about the thing "a"?
What functionality is associated with that link?

What is their actual relationship? Is there a linked list
of some kind lurking here? If so, then what you really want
to do is build a linked list and shove the data in there.
Are these things really in some kind of heirarchy? Then
maybe you want to organise things based on a tree of some
kind. Or maybe, it's just a way of keeping them together
in a particular order, in which case you really want them
in a vector.

In other words, maybe you are trying to shoe-horn
a relationship that is easy in run-time initialization into
one that is quite hard in compile-time initialization.

In other words, it looks like you've settled on one way of
doing things. You seem to want to do it that way
and if you could only get a little help from the stubborn old
gurus here, you'd be able to finish your code in record time.
But what they are telling you is, there are almost certainly
easier ways to do this than by trying to seduce the compiler.
Hey, it may mean that your code takes a 1/4 second longer to
load and start doing actual work. Compare that to struggling
with your app for several days. Or possibly finding this scheme
busts when you add some new level of complexity, and spending
several weeks ripping out the old code and doing it the way
the gurus suggested anyway.

Or, to put it another way, what you think is a smaller problem
on your way to the larger issue may not be so small after all.

What turns out to be easy, and what turns out to be a smegger of
a pain in the ass, is something you should really listen to the
gurus about. Many of them got to be gurus by barking their shins,
often repeatedly, over just the same kind of issue. Others were
amazingly lucky and listened to the previous guys down the trail,
and didn't trip over the same logs.
Socks
Jul 22 '05 #14
On Wed, 30 Jun 2004 14:51:07 -0700, puppet_sock wrote:

Hi,
Kristof Bastiaensen <kr*****@vleeuwen.org> wrote in message
news:<pa***************************@vleeuwen.org>. .. [snip]
static recurs a = {5, &b};
static recurs b = {4, &a};


Others have hinted at re-doing your design to avoid this sort of thing.
Let me make a hint about direction of redesign.
[snip]
In other words, it looks like you've settled on one way of doing things.
You seem to want to do it that way and if you could only get a little
help from the stubborn old gurus here, you'd be able to finish your code
in record time. What turns out to be easy, and what turns out to be a
smegger of a pain in the ass, is something you should really listen to
the gurus about. Many of them got to be gurus by barking their shins,
often repeatedly, over just the same kind of issue. Others were
amazingly lucky and listened to the previous guys down the trail, and
didn't trip over the same logs.
Socks


Thanks for your kind explanation. Of course I know that there is a lot to
learn from the guru's and other people here. That's a reason for posting
here. But remarks like "you are trying to sell a crappy idea" just don't
contribute to any discussion.

Perhaps I wasn't clear enough about my problem. I am creating code for
some japanese chess variants. The data that I am describing are the
pieces and their movements. There is a piece on the front and on the
backside of each piece. They have different moves and properties. Basicly
what I wanted was a way to point to the promoted form and the unpromoted
form. I found that the easiest way is to use a pointer from the
unpromoted form to the promoted, and vice-versa. This is perhaps not the
most elegant solution, but it is the simplest, and adequate for my
problem.

I don't see a good reason to code in C/C++ if it isn't for efficiency.
There are many languages out there that perform less, but offer a lot more
flexibility and convenience, for example ruby, smalltalk, scheme or ocaml.
I find the best reason to write in C/C++ is to have every bit of
performance.

Regards,
Kristof Bastiaensen
Jul 22 '05 #15
Kristof Bastiaensen <kr*****@vleeuwen.org> wrote in message news:<pa***************************@vleeuwen.org>. ..
1. How can a use a pointer to an undeclared variable
in another variable?
for example:

struct recurs {
int data;
struct recurs *link;
};

static recurs a = {5, &b};
static recurs b = {4, &a};


I'll be the first to admit it's a horrible idea, but if you are willing to not
make the variables static, you can do what you want with a separate declaration
before the definition:

extern recurs b;

recurs a = {5, &b};
recurs b = {4, &a};

samuel
Jul 22 '05 #16
> Thanks for your kind explanation. Of course I know that there is a lot to
learn from the guru's and other people here. That's a reason for posting
here. But remarks like "you are trying to sell a crappy idea" just don't
contribute to any discussion.

Perhaps I wasn't clear enough about my problem. I am creating code for
some japanese chess variants. The data that I am describing are the
pieces and their movements. There is a piece on the front and on the
backside of each piece. They have different moves and properties. Basicly
what I wanted was a way to point to the promoted form and the unpromoted
form. I found that the easiest way is to use a pointer from the
unpromoted form to the promoted, and vice-versa. This is perhaps not the
most elegant solution, but it is the simplest, and adequate for my
problem.


I think your focus is a bit wrong. For a chess playing program its fair
enough to want the most efficient data structures possible, but how you get
there is less of an issue.

Presumably you only pay the cost of setting up your data structures once at
the beginning of the program. So I would store the information in a file in
an easy human readable manner and then write some code to load that
information and initialise you data structures.

The big advantage is modularity. If you decide to use a new variant you
modify the data file, if you decide to ue a new data structure you modify
the loading routine.

john
Jul 22 '05 #17

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

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
7
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
21
by: Roshan Naik | last post by:
typedef int foo ( foo ); // foo is a pointer-to-function type that takes another foo as argument and returns an int I need to achieve the above effect somehow. This is not accepted by any...
4
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. ...
3
by: IR | last post by:
Hi, I've been trying to do the following (which doesn't compile) : template<class T, class F = Example<T struct Example { F foo(); };
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
6
by: Juha Nieminen | last post by:
Multiple inclusion of the same header file can cause the compilation to fail because of multiple definitions of the same type. That's why it's standard practice to write all headers like this: ...
7
by: pereges | last post by:
In a recursive program, do you think it is necessary to add the static keyword for every variable or do you think it is unnecessary ? Is every call associated with seperate copy of variables ?
6
KevinADC
by: KevinADC | last post by:
This snippet of code provides several examples of programming techniques that can be applied to most programs. using hashes to create unique results static variable recursive function...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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...
0
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...
0
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.