PDA

View Full Version : Another C++ question.


Bmxpunk86pl
02-22-02, 11:50 AM
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/showthread.php?s=&threadid=68574

Penguin mentioned something like this:


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

XWRed1
02-22-02, 12:21 PM
the length of the array?

Bmxpunk86pl
02-22-02, 02:26 PM
so then the length of the array would be one?

XWRed1
02-22-02, 03:51 PM
No, its would be 65,000, because that is the value of x.

Bmxpunk86pl
02-22-02, 03:53 PM
so u mean that any character for a to z will consume 65,000 bytes?

XWRed1
02-22-02, 03:57 PM
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.

Bmxpunk86pl
02-22-02, 04:04 PM
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

Visidex
02-22-02, 09:24 PM
x is a variable that equals 65000. Each array is declared with space for 65000 chars

VotTak
02-23-02, 01:10 AM
Hey, why don't you just take that piece of code and compile. And keep on reading about arrays.

David
02-23-02, 09:06 AM
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 :D

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.

Bmxpunk86pl
02-23-02, 11:07 AM
alright thanks for ur help.

XprincoX
02-24-02, 03:13 AM
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 :D

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. ;)

vandersl
02-24-02, 04:52 PM
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.

Bmxpunk86pl
02-24-02, 06:36 PM
#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.

XprincoX
02-24-02, 09:07 PM
well your using the wrong data type to declare ur arrays. The index has to be an integer and can not be an character. :D

vandersl
02-24-02, 10:30 PM
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'.

VotTak
02-25-02, 01:34 AM
that's right

Guid0
02-26-02, 06:02 PM
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.

XWRed1
02-27-02, 12:22 AM
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.

vandersl
02-27-02, 08:08 AM
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.