LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
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
 
LinkBack Search this Thread
Old 12-29-2004, 02:39 PM   #1
george_mercury
LQ Newbie
 
Registered: Jun 2004
Location: Slovenia
Distribution: Gentoo
Posts: 27

Rep: Reputation: 15
Exclamation Arrays overwriting variables in c


I've spent quite some time on this issue, but I just can't see what I'm doing wrong. Take a look at this:

struct test {
int array[5];
int value;
};

struct test abc;

Well, guess what: abc.value and abc.array[5] are the same thing - they always have the same address. So if you write to abc.array[5] you automatically overwrite abc.value, and vice versa. But if the array and value aren't inside a structure, this doesn't happen. It's like the array inside the structure has 5 elements, while outside it has 6 elements (which is what I want ). So what is wrong here?

George

Last edited by george_mercury; 12-29-2004 at 02:41 PM.
 
Old 12-29-2004, 02:51 PM   #2
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Erm, an array of length 5 has entries 0 to 4, no? The memory address after [4] is the next variable's.
 
Old 12-29-2004, 03:16 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,524

Rep: Reputation: 93
Quote:
So if you write to abc.array[5] you automatically overwrite abc.value, and vice versa. But if the array and value aren't inside a structure, this doesn't happen.
You're writing past the end of the array, like Proud said.
So, when you're array are "not inside a structure", and you write past the array, some other variable (or arguably worse, part of the stack) may, or may not get overwritten. If tou're "lucky" you get a "segmentation fault" runtime error...
 
Old 12-30-2004, 04:37 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,048

Rep: Reputation: 95
yes, in C array numbering starts from zero
so
array[4] is the fifth element

(or is it bruce willis?)
 
Old 12-31-2004, 06:20 AM   #5
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
Yup the above answers are true, if the memory allocated to your
struct test abc will be something like

memory of abc.array[0]
memory of abc.array[1]
memory of abc.array[2]
memory of abc.array[3]
memory of abc.array[4]
memory of abc.value

And as there are no bound check for array in C/C++ and the address of array element is calculated like.

Address of array + (Size of data type * index number)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script - variables & arrays question rblampain Linux - Software 4 09-25-2004 09:57 AM
Redhat 9 overwriting /etc/fstab? dmancine Linux - General 2 08-24-2004 05:24 PM
Avoid overwriting bootloader jwijesundra Linux - General 5 07-30-2004 05:16 AM
Overwriting Redhat9 With Suse9 happyman Linux - Distributions 2 04-08-2004 11:37 AM
Shel scripting: variables pointing to variables and case Dark_Helmet Programming 5 06-08-2003 11:07 AM


All times are GMT -5. The time now is 02:55 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration