Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ self-referencing array?

Tags:

c++

I accidentally created a bug in a program by self-referencing in an array. Here's a very simplified demo program similar in concept:

#include <iostream>
using namespace std;

int kTest[] = {
    kTest[0]
};

int main() {
    cout << kTest[0] << endl;
}

I was surprised that I received neither a compiler error or even a warning on this code! In my case it ended up producing unpredictable output. Is it accessing garbage memory?

I was curious about under what circumstances this would have well-defined output (if ever!).

Edit: Does it make a difference if kTest is static? What about const? Both?

like image 617
aardvarkk Avatar asked Oct 19 '16 17:10

aardvarkk


People also ask

What is self reference structure in C?

The self-referential structure is a structure that points to the same type of structure. It contains one or more pointers that ultimately point to the same structure. Structures are a user-defined data structure type in C and C++.

Are self-referential structures possible in C?

A self-referential structure is a structure that can have members which point to a structure variable of the same type. They can have one or more pointers pointing to the same type of structure as their member.

What is a self-referential structure?

A self referential data structure is essentially a structure definition which includes at least one member that is a pointer to the structure of its own kind. • Such self referential structures are very useful in applications that involve linked data structures, such as lists and trees.

What is self-referential structure give suitable example?

A self-referential structure is one of the data structures which refer to the pointer to (points) to another structure of the same type. For example, a linked list is supposed to be a self-referential data structure. The next node of a node is being pointed, which is of the same struct type.

What are self referential structures in C++?

Self Referential structures are those structures that have one or more pointers which point to the same type of structure, as their member. In other words, structures pointing to the same type of structures are self-referential in nature. In the above example ‘link’ is a pointer to a structure of type ‘node’.

What is a reference array in C++?

As reference array retains information about underlying array and its type would be int [4], not int*. Now let us discuss a reference to an array. First most the common way that comes into our mind is described below syntactically.

What is a self-referential data structure?

Where ‘next’ is a pointer to a struct node variable, it should be remembered that the pointer to the structure is similar to the pointer to any other variable. Hence, a self-referential data structure is generally a structure definition that includes at least one member that is a pointer to the structure of its kind.

What is a self-referential object?

Self-referential objects can be linked together to form useful data structures, such as lists, queues, stacks and trees. Figure 25.2 illustrates two self-referential objects linked together to form a linked list.


2 Answers

int kTest[] = {
    kTest[0]
};

is similar to, if not exactly same as

int x = x;

It will be undefined behavior, if declared locally in a function.

It seems to be well defined when kTest is a global variable. See the other answer for additional details.

like image 170
R Sahu Avatar answered Oct 17 '22 09:10

R Sahu


I'm not so sure this is undefined. Quote from the current draft:

[basic.start.static]/3

If constant initialization is not performed, a variable with static storage duration ([basic.stc.static]) or thread storage duration ([basic.stc.thread]) is zero-initialized ([dcl.init]). Together, zero-initialization and constant initialization are called static initialization; all other initialization is dynamic initialization. Static initialization shall be performed before any dynamic initialization takes place.

To me it looks like kTest is already zero-initialized when the dynamic initialization starts, so it may be defined to initialize to 0.

like image 8
krzaq Avatar answered Oct 17 '22 11:10

krzaq