Archived

This topic is now archived and is closed to further replies.

JoHnOnIzEr

Arrays

Recommended Posts

I am pretty sure you can''t in C++. You can use the Vector class. This works just like an array and can also tell you the size.



First make it work,
then make it fast.

--Brian Kernighan

"The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were." - John Fitzgerald Kennedy(35th US President)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Well if your array is statically sized such as
array[20]; then you can use the size of operator to determine it''s size. If it''s a dynamically sized array you need to keep track of that yourself, or use std::vector.

Share this post


Link to post
Share on other sites
C and C++ don't keep track of length at all. You must do this yourself.

If the number of elements in an array is constant, you can just code around that number:


#define NUM_INTS 256
int array_of_death[NUM_INTS];
:
:

for (int i = 0; i < NUM_INTS; i++)
{
printf("%d is the NUMBER OF DEATH!\n", array_of_death);
}
:
:


For CZ-strings (char* strings), you do not keep track of the number directly. Instead all CZ-strings end with a '\0' or NULL byte. All STD string functions expect the last byte of a string to be a 0 or you will get LOTS OF segmentation faults.


char buffer[] = "My really cool buffer"; // initialized string....
char name[256]; // uninit'ed string

strcpy(name, buffer); // copies buffer to name

printf("Name is %d bytes long\n", len(name) ); // len counts the number of bytes from the start of the string to the NULL terminator


You can create a class or struct to keep track of the length yourself. That's how Vectors work.


class Vector
{
private:
int* array; // this pointer will hold the array
int length; // this will hold the length of the array
public:
Vector();
Vector(int init_count);
~Vector();
void add(int a);
void length(); // this simply returns length
int get(int index); // this checks to make sure index is greater than 0 and less than length, and if it is, returns array[index]
}


Java / javascript sacrafice a little bit of speed for a lot of safety:


// javascript
var array[5];
for (var i = 0; i <= 5; i++)
{
array[i] = 0; // will flag an error when i = 5...
}

// C++
int array[5];
for (int i = 0; i <= 5; i++)
{
array[i] = 0; // will silently write past the array.... a potentially serious and hard to find error
}



C++ allows more freedom than javascript. Whether you use that freedom to make code that runs 10x faster than javascript or use it to "shoot yourself in the foot".... that's up to you.


/¯\_/¯\_/¯\_/¯\_/¯\_/¯"You TK'ed my chicken!"
\_/¯\_/¯\_/¯\_/¯\_/¯\_/

[edited by - Tac-Tics on September 2, 2003 2:42:09 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
like whats his face said before a loop can do it to


main()
{
int the_array[100]; //whatever number even user made

int num = 0;
for(int blah = 0; blah != NULL; blah++)
num++;

}

this will loop till the end of the array because the end is the null terminator

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
like whats his face said before a loop can do it to


main()
{
int the_array[100]; //whatever number even user made

int num = 0;
for(int blah = 0; blah != NULL; blah++)
num++;

}

this will loop till the end of the array because the end is the null terminator


no it won''t

Share this post


Link to post
Share on other sites