Notices

Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

Another C++ question.

Post Reply New Thread Subscribe Search this Thread
 
 
Thread Tools
Old 02-22-02, 11:50 AM Thread Starter   #1
Bmxpunk86pl
Member

 
Bmxpunk86pl's Avatar 

Join Date: Sep 2001
Location: CT/Poland

 
Another C++ question.


Alright i didnt wanna change the subject or something to another post that someone posted but i had a question from it so go here before proceding.http://forums.overclockers.ws/vb/sho...threadid=68574

Penguin mentioned something like this:

Quote:
Assuming mem=256MB = 263000kb = 3million bytes?
long int = 8bytes?

[code]
#include <iostream>

int main()
{
long unsigned int x=65000;

char array_1[x];
char array_2[x];
char array_3[x];
...
char array_100[x];

return(0);
}
what does the x in the array mean?

thanks,
adam
Bmxpunk86pl is offline   QUOTE Thanks
Old 02-22-02, 12:21 PM   #2
XWRed1
Senior Member

 
XWRed1's Avatar 

Join Date: Oct 2001

 
the length of the array?
XWRed1 is offline   QUOTE Thanks
Old 02-22-02, 02:26 PM Thread Starter   #3
Bmxpunk86pl
Member

 
Bmxpunk86pl's Avatar 

Join Date: Sep 2001
Location: CT/Poland

 
so then the length of the array would be one?
Bmxpunk86pl is offline   QUOTE Thanks
Old 02-22-02, 03:51 PM   #4
XWRed1
Senior Member

 
XWRed1's Avatar 

Join Date: Oct 2001

 
No, its would be 65,000, because that is the value of x.
XWRed1 is offline   QUOTE Thanks
Old 02-22-02, 03:53 PM Thread Starter   #5
Bmxpunk86pl
Member

 
Bmxpunk86pl's Avatar 

Join Date: Sep 2001
Location: CT/Poland

 
so u mean that any character for a to z will consume 65,000 bytes?
Bmxpunk86pl is offline   QUOTE Thanks
Old 02-22-02, 03:57 PM   #6
XWRed1
Senior Member

 
XWRed1's Avatar 

Join Date: Oct 2001

 
No. It means that the array has 65,000 "slots" in it. Each slot should take the space of a char, which depending on the compiler, should be like 1-2 bytes. So that is 65-130k getting eaten. Go read on how arrays work and come back.
XWRed1 is offline   QUOTE Thanks
Old 02-22-02, 04:04 PM Thread Starter   #7
Bmxpunk86pl
Member

 
Bmxpunk86pl's Avatar 

Join Date: Sep 2001
Location: CT/Poland

 
no i know pretty much how arrays work but my book that i read never explained what happenes when u put a character in the [] brackets. Plus i think im better of talkin to live people then reading a book, i mean everyone could just go and read insted of ask and this forum would never exist.

P.S the book i read is "C++ for dummies" lol
Bmxpunk86pl is offline   QUOTE Thanks
Old 02-22-02, 09:24 PM   #8
Visidex
Member

 
Visidex's Avatar 

Join Date: Jan 2002

 
x is a variable that equals 65000. Each array is declared with space for 65000 chars
Visidex is offline   QUOTE Thanks
Old 02-23-02, 01:10 AM   #9
VotTak
Member

 
VotTak's Avatar 

Join Date: Feb 2002
Location: Brooklyn/NY

 
Hey, why don't you just take that piece of code and compile. And keep on reading about arrays.
VotTak is offline   QUOTE Thanks
Old 02-23-02, 09:06 AM   #10
David
Forums Super Moderator
Overclockers.com Lead Editor



 
David's Avatar 

Join Date: Feb 2001
Location: Hiding from Americans and making pretty colours in a drybox in St Andrews

10 Year Badge
 
Re: Another C++ question.


Quote:
Originally posted by Bmxpunk86pl

what does the x in the array mean?

thanks,
adam
x is a variable. I have initialised it and given it the value 65000 (which is the near the maximum value for an unsigned integer. Unsigned meaning +ve numbers only).

This is then used in the place of 65000 when initialising the arrays.

For example, if I want an array of size 100, then I could do this:

int array[200]

However, if I initialise a variable 'x' and give it the value 200:

int x=200;
int array[x];

Then the array has x elements = 200 elements. If x were 100 then the array would have 100 elements.

Hope this helps

PS dont compile and run the program - it is desgined to use as much memory as possible ie fill up RAM with empty, large, arrays.

__________________
David J. Nelson MChem(Edin) PhD(Strath) AMRSC [Academia Profile]
OC Forums Super Moderator // Overclockers.com Editor


The Workhorse: [Lenovo W510] Intel Core i7-720QM / 8 GB DDR3 / nVidia Quadro FX880M / Crucial M4 256 GB SSD / Windows 7
The HTPC/Server: AMD Phenom X4 9650 / 6 GB DDR2 / nVidia 8300 / 5 TB of HDDs / Antec Solo II Case / Windows 7
The Gaming Rig: AMD Llano A6-3650 / 4 GB DDR3 / nVidia GTX260 / 2x 500 GB HDD in RAID0 / Antec Dark Fleet DF-85 Case / Windows 7
The Benchmarking Setup: Various LGA775 chips / Asus Rampage Formula / 2 GB OCZ 1066 MHz DDR2 / nVidia Quadro NVS 285 / 320 GB HDD / Windows 7

My research fellowship is eating all my time, so I may be slow to reply to emails and private messages.
David is offline Author Profile Benching Profile Folding Profile   QUOTE Thanks
Old 02-23-02, 11:07 AM Thread Starter   #11
Bmxpunk86pl
Member

 
Bmxpunk86pl's Avatar 

Join Date: Sep 2001
Location: CT/Poland

 
alright thanks for ur help.
Bmxpunk86pl is offline   QUOTE Thanks
Old 02-24-02, 03:13 AM   #12
XprincoX
bargain hunter

 
XprincoX's Avatar 

Join Date: Aug 2001
Location: Irvine, CA

 
yes and also remember that arrays are n-1 of there size. so an to access an array at index 5 it would be somearray[4]. Just a lil tip so u dont try access something outta bounds

also another tip, if u have an array that of size 65000 it would really be a bad choice of data stucture as the program may not run on some machines. Therefore should look into more dynamic data structures like a linked list.

__________________
"If money is the root of all evil, give me all your evil!!" :D
T-bird 1.4 AYHJA @ 1622 <--- RIP DIED IN UNFORTUNATE ACCIDENT 8/01-1/02 :(
XP2000+ -- Epox 8k7a+ -- vantec Aeroflow -- 512 mb Crucial pc2100 -- SB Audigy mp3+ -- GeForce 2 GTS
120 GB WD S.E. -- 80 gig 7200 Maxtor -- Blk Lite-On 24x10x40x -- Blk 16x Lite-On DVD -- DigitalDoc 5 -- NewQ Equalizer -- Directron D30B/Enermax 431w --WinXP Pro/Win2k

-Member of The Avatar Maker's Guild™ :burn:
E.T. Phone Home?

My HeatWare
Member of the Anti-Unethical Overclockers Movement Are you?
XprincoX is offline   QUOTE Thanks
Old 02-24-02, 04:52 PM   #13
vandersl
Member



Join Date: Apr 2001

 
Assuming this is C/C++, the code won't compile anyway.

Array declarations must use a constant for the dimension, not a variable.

This would work:

#define X 65000
...
unsigned long my_array[X]

Using a variable confuses the compiler - essentially, it can't know the size of the array until run-time.
vandersl is offline   QUOTE Thanks
Old 02-24-02, 06:36 PM Thread Starter   #14
Bmxpunk86pl
Member

 
Bmxpunk86pl's Avatar 

Join Date: Sep 2001
Location: CT/Poland

 
#include <stdio.h>
#include <iostream.h>

int main(int argc, char *argv[])
{
char x=65000;
char arrayone[x];
char arraytwo[x];
char arraythree[x];
char arrayfour[x];
char arrayfive[x];
char arraysix[x];
char arrayseven[x];
char arrayeight[x];
char arraynine[x];
char arrayten[x];
char arrayeleven[x];
char arraytwelve[x];
char arraythirten[x];
char arrayfourteen[x];
char arrayfifteen[x];

cout <<"Your memory is tasty";
return 0;
}

ok i wrote thta program and it sucks up about 130 megs of RAM on my computer but i get a windows error saying
"The exception unknown software exception (0x00000fd) in the applicatoin at location 0x0040ba2f."

What was the problem? Did it take too much memory? I have 512 megs of ram.
Bmxpunk86pl is offline   QUOTE Thanks
Old 02-24-02, 09:07 PM   #15
XprincoX
bargain hunter

 
XprincoX's Avatar 

Join Date: Aug 2001
Location: Irvine, CA

 
well your using the wrong data type to declare ur arrays. The index has to be an integer and can not be an character.

__________________
"If money is the root of all evil, give me all your evil!!" :D
T-bird 1.4 AYHJA @ 1622 <--- RIP DIED IN UNFORTUNATE ACCIDENT 8/01-1/02 :(
XP2000+ -- Epox 8k7a+ -- vantec Aeroflow -- 512 mb Crucial pc2100 -- SB Audigy mp3+ -- GeForce 2 GTS
120 GB WD S.E. -- 80 gig 7200 Maxtor -- Blk Lite-On 24x10x40x -- Blk 16x Lite-On DVD -- DigitalDoc 5 -- NewQ Equalizer -- Directron D30B/Enermax 431w --WinXP Pro/Win2k

-Member of The Avatar Maker's Guild™ :burn:
E.T. Phone Home?

My HeatWare
Member of the Anti-Unethical Overclockers Movement Are you?
XprincoX is offline   QUOTE Thanks
Old 02-24-02, 10:30 PM   #16
vandersl
Member



Join Date: Apr 2001

 
Ok, you can't allocate arrays with a variable for the size.

And a 'char' (at least in my compiler) is one byte, which can store values in the range -128 to +127. So the assignment of 65000 won't work anyway.

And even if it did, assuming you only allocate the 15 arrays shown, that would amount to 15 arrays of 65000 'char's each, for a grand total of 975000 bytes, or less than 1MB.

Just allocate a big array: unsigned long my_array[1000000] will allocate ~4MB in one array. This is still meaningless to evaluate the memory management capabilites of the OS, however.

Ask questions like 'which OS offers better garbage collection', or 'which OS has the best virtual memory implementation' or 'which OS offers the best memory space protection'. Not 'which OS will let me allocate more char arrays'.
vandersl is offline   QUOTE Thanks
Old 02-25-02, 01:34 AM   #17
VotTak
Member

 
VotTak's Avatar 

Join Date: Feb 2002
Location: Brooklyn/NY

 
that's right
VotTak is offline   QUOTE Thanks
Old 02-26-02, 06:02 PM   #18
Guid0
Member



Join Date: Dec 2001
Location: Kansas, USA

 
How come noboady has writen a one liner inside of a for loop using "new".....?

also i would like to apoligize to vandersl
for asking the wrong questions.

Last edited by Guid0; 02-26-02 at 07:40 PM.
Guid0 is offline   QUOTE Thanks
Old 02-27-02, 12:22 AM   #19
XWRed1
Senior Member

 
XWRed1's Avatar 

Join Date: Oct 2001

 
Quote:
How come noboady has writen a one liner inside of a for loop using "new".....?
If you read the old thread, I essentially did that, but I used malloc because everyone else seemed to be doing C.
XWRed1 is offline   QUOTE Thanks
Old 02-27-02, 08:08 AM   #20
vandersl
Member



Join Date: Apr 2001

 
No, I'm sorry Guid0

By your response, I think I came across a little condescending. It wasn't my intent. Was just trying to point the thought in a few more constructive directions.

Since I gave such a bum answer the first time:

Under Windows (32 bit), you can get memory system information using 'GlobalMemoryStatus()', and allocate/free memory on the global heap using 'GlobalAlloc()' and 'GlobalFree()'. The following snippet will give you some info, and you can take it from there.

HGLOBAL memhandle;
MEMORYSTATUS status1, status2, status3;

GlobalMemoryStatus( &status1 );
memhandle = GlobalAlloc( GMEM_FIXED, 10000000 );
GlobalMemoryStatus( &status2 );
GlobalFree( memhandle );
GlobalMemoryStatus( &status3 );

This code obtains the memory system status (look up the structure in an API book or help), allocates 10 million bytes, gets the status again, frees the memory, and gets a final status.

You should be able to see the memory numbers change due to your allocation, but remember - there are other things running on your comp that may affect the numbers also.

Hope this helps.
vandersl is offline   QUOTE Thanks

Post Reply New Thread Subscribe


Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Mobile Skin
All times are GMT -5. The time now is 11:07 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
You can add these icons by updating your profile information to include your Heatware ID, Benching Profile ID or your Folding/SETI profile ID. Edit your profile!
X

Welcome to Overclockers.com

Create your username to jump into the discussion!

New members like you have made this the best community on the Internet since 1998!


(4 digit year)

Why Join Us?

  • Share experience
  • Max out your hardware
  • Best forum members anywhere
  • Customized forum experience

Already a member?