473,395 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Quick question about arrays and odd behavior

I have an odd situation I'm trying to understand..... I'm using MS VS 7.0
C++ with the MFC CBuffer class.

If I do this:

#define NUM_ELEMENTS 2
CBuffer<char, 512 x[NUM_ELEMENTS-1];
CBuffer<char, 512 y[NUM_ELEMENTS-1];

So as you can see I have two variables which will hold strings, each of
which has two elements. One at position [0] and the other at [1].

However with the above definition x[1] and y[0] point at the same exact
address and overwrite one another! Why is this??

If I define those vars as NUM_ELEMENTS, instead of NUM_ELEMENTS-1, then
things work great. It would think that [1] would do the trick since I have
[0] for the first element and [1] for the second. So why is it then
necessary to have these defined as [2]? And yes I am certain when I am
writing to these vars I'm using [0] to address the first element and [1] to
address the second.

Defining it as [NUM_ELEMENTS] seems to do the trick to resolve this issue,
but I wanted to better understand why I am seeing that behavior to make sure
that this apparent fix is not an illusion with a subtle bug waiting to bite
me!

Thanks!!
Oct 10 '06 #1
3 1701
Mike Cain wrote:
I have an odd situation I'm trying to understand..... I'm using MS VS 7.0
C++ with the MFC CBuffer class.

If I do this:

#define NUM_ELEMENTS 2
CBuffer<char, 512 x[NUM_ELEMENTS-1];
CBuffer<char, 512 y[NUM_ELEMENTS-1];

So as you can see I have two variables which will hold strings, each of
which has two elements. One at position [0] and the other at [1].
The way you have defined NUM_ELEMENTS and your arrays lead to them being only of
one cell each. Remove the -1, and you will have the behavior you described. This
is because when you initialize an array you are passing the number of elements
(i.e. the size). You are not declaring the highest index used by the array.

Regards,

alunduil

--
Student, B.S. Physics & Computer Science
Department of Physics and Astronomy, MSUM
Department of Computer Science and Information Services, MSUM
www.alunduil.com
Oct 10 '06 #2
Mike Cain schrieb:
I have an odd situation I'm trying to understand..... I'm using MS VS 7.0
C++ with the MFC CBuffer class.

If I do this:

#define NUM_ELEMENTS 2
CBuffer<char, 512 x[NUM_ELEMENTS-1];
CBuffer<char, 512 y[NUM_ELEMENTS-1];

So as you can see I have two variables which will hold strings, each of
which has two elements. One at position [0] and the other at [1].
No.

In an array, you declare the *size* (number of elements) in brackets, which
you access with an zero-based index.

Example:

int arr[5];
// arr has 5 elements, from arr[0] to arr[4]

char s[1];
// s has exactly one element: s[0]

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Oct 10 '06 #3
On Mon, 9 Oct 2006 22:56:28 -0400, "Mike Cain" <aa@aa.comwrote in
comp.lang.c++:
I have an odd situation I'm trying to understand..... I'm using MS VS 7.0
C++ with the MFC CBuffer class.

If I do this:

#define NUM_ELEMENTS 2
CBuffer<char, 512 x[NUM_ELEMENTS-1];
When the preprocessor replaces the macro, the line above becomes:

CBuffer<char, 512 x[1];
CBuffer<char, 512 y[NUM_ELEMENTS-1];
When the preprocessor replaces the macro, the line above becomes:

CBuffer<char, 512 y[1];
So as you can see I have two variables which will hold strings, each of
which has two elements. One at position [0] and the other at [1].
So as I hope you can now see, you have two variables which will hold
strings, each of which has ONE element. There is no x[1] or y[1].
However with the above definition x[1] and y[0] point at the same exact
address and overwrite one another! Why is this??
Because x[1] does not exist, it is beyond the end of the array.
Attempting to access an element beyond the end of an array produces
undefined behavior. The result of that undefined behavior in this
particular case with your particular compiler happens to be that you
actually access y[0].
If I define those vars as NUM_ELEMENTS, instead of NUM_ELEMENTS-1, then
things work great. It would think that [1] would do the trick since I have
[0] for the first element and [1] for the second. So why is it then
necessary to have these defined as [2]? And yes I am certain when I am
writing to these vars I'm using [0] to address the first element and [1] to
address the second.

Defining it as [NUM_ELEMENTS] seems to do the trick to resolve this issue,
but I wanted to better understand why I am seeing that behavior to make sure
that this apparent fix is not an illusion with a subtle bug waiting to bite
me!
Apparently you are new to C++, as the use of array subscripts can be
mildly confusing at first. The good news is that once you understand
it, you will almost certainly never forget it.

The number that goes in the brackets when you DEFINE an array is the
total number of array elements that you want. The valid values for
subscripts to access elements of the array are from 0 to (total - 1),
inclusive.

That is:

type array [TOTAL];

....for any valid "type" that you can create an array of, and any
positive value of TOTAL, has elements:

array [0];
array [1];
/* ... */
array [TOTAL - 2];
array [TOTAL - 1];
/* no more */

So when define your arrays with NUM_ELEMENTS elements, where
NUM_ELEMENTS is a macro for 2, then you are indeed creating those
arrays with two elements, each one has a [0] element and a [1]
element.

So the "apparent fix" is actually correct, and your original code is
incorrect, based on a misunderstanding about the way subscripts are
used in array definition versus array element access.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Oct 10 '06 #4

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

Similar topics

19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
111
by: JKop | last post by:
Okay here we go, I feel it's about time people conversed about the bullshit aspects of C++ (including the bullshit stuff brought forward from C). I'll begin with a few of my own grievances: 1)...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
197
by: Steve Kobes | last post by:
Is this legal? Must it print 4? int a = {{1, 2}, {3, 4}}, *b = a; printf("%d\n", *(b + 3)); --Steve
43
by: Anitha | last post by:
Hi I observed something while coding the other day: if I declare a character array as char s, and try to use it as any other character array..it works perfectly fine most of the times. It...
13
by: ralphedge | last post by:
These sorts work fine on 100000 ints but if I go much higher they will both segmentation fault **************************MERGESORT********************* mergesort(int *a, int size) //a is...
90
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
0
by: r035198x | last post by:
Inheritance We have already covered one important concept of object-oriented programming, namely encapsulation, in the previous article. These articles are not articles on object oriented...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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,...

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.