13 Replies - 517 Views - Last Post: 14 November 2011 - 12:53 PM Rate Topic: -----

#1 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2497
  • View blog
  • Posts: 15,460
  • Joined: 24-September 08

Autoinitializing values in an array [theory]

Posted 11 November 2011 - 12:55 PM

Correct me if I'm wrong, but isn't there some way that you can autoinitialize values in an array when you declare it? I thought I remembered my professor saying something like that.

Like
int a[] = 0;
or something similar?
Is This A Good Question/Topic? 0
  • +

Replies To: Autoinitializing values in an array [theory]

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Autoinitializing values in an array [theory]

Posted 11 November 2011 - 01:00 PM

If you do this:
int a[100] = {0};


then the compiler should initialize the entire array to 0. If you do this:
int a[100] = {99};


then the compiler should only set the first index a[0] to 99.

This post has been edited by CTphpnwb: 11 November 2011 - 01:00 PM

Was This Post Helpful? 1
  • +
  • -

#3 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2497
  • View blog
  • Posts: 15,460
  • Joined: 24-September 08

Re: Autoinitializing values in an array [theory]

Posted 11 November 2011 - 01:02 PM

That's what I was thinking. cplusplus didn't come out and say it, but I coulda sworn I saw it somewhere. My hat off to you, kind sir.

EDIT: I accidentally a word.

This post has been edited by NeoTifa: 11 November 2011 - 01:12 PM

Was This Post Helpful? 0
  • +
  • -

#4 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2497
  • View blog
  • Posts: 15,460
  • Joined: 24-September 08

Re: Autoinitializing values in an array [theory]

Posted 12 November 2011 - 03:17 PM

A tangentially related question: so I know that I'm going to use an array, so I'm declaring it in my .h, and I want to go back later and assign the size later. So, basically, I'm declaring an empty int array, and when I get the size in the constructor, I'm going to re-declare the array with the size and initialize them to a value. Is this valid?

int table[];
then go back and be like
table[size] = {EMPTY};
where EMPTY is already defined (-1 if you feel so inclined)

??????????
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Autoinitializing values in an array [theory]

Posted 12 November 2011 - 03:33 PM

Array sizes must be constants. If you want to be able to change an array size you need to use dynamic allocation. I prefer using vectors for that so that I don't need to worry about memory leaks and I get the benefit of the methods available to vectors.
Was This Post Helpful? 1
  • +
  • -

#6 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2497
  • View blog
  • Posts: 15,460
  • Joined: 24-September 08

Re: Autoinitializing values in an array [theory]

Posted 12 November 2011 - 04:19 PM

Hrmm... my friend who already took the class said she put it in the .cpp in the constructor, but it seems like that would go out of scope or something. I might do vectors, but then you gotta fool with iterators and shit too though, right?
Was This Post Helpful? 0
  • +
  • -

#7 theman82120  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 05-November 11

Re: Autoinitializing values in an array [theory]

Posted 12 November 2011 - 04:28 PM

you could initilize arrays like this
int example [2]={1,2};

but you cant do this
int example[2];
example[2] ={1,2};


you have to specify the size of the array while declaring it or you will have to do dynamically allocate as CTphpnwb said like this
int table[];
table= new int [size];


or use vectors
#include <vector>
std::vector <int> table ;
table.push_back(1);


Was This Post Helpful? 1
  • +
  • -

#8 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2497
  • View blog
  • Posts: 15,460
  • Joined: 24-September 08

Re: Autoinitializing values in an array [theory]

Posted 12 November 2011 - 04:50 PM

Thanks. I put it in my constructor but there's a red squiggly under it saying ClosedHash::table must be a modifiable lvalue. :( my int table[] is public.

ClosedHash::table = new int[size];


Never mind, it magically went away, but now the int table[] in my .h says "incompatible type not allowed".

Never mind again, I just made table a pointer.

int* table;
...
table = new int[size];

:D It compiles!

This post has been edited by NeoTifa: 12 November 2011 - 05:32 PM

Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: Autoinitializing values in an array [theory]

Posted 13 November 2011 - 05:22 AM

View PostNeoTifa, on 12 November 2011 - 07:19 PM, said:

Hrmm... my friend who already took the class said she put it in the .cpp in the constructor, but it seems like that would go out of scope or something. I might do vectors, but then you gotta fool with iterators and shit too though, right?

You can treat vectors like arrays. For example, this could be a vector or an array:
table[some_index] = some_value;

There's no way to tell with out seeing the declaration.

It's easier to initialize a vector with nonzero values:
using namespace std;
#include <iostream>
#include <vector>

int main () {
	vector<int> myvec;
	myvec.assign(25,110);
	cout << myvec[10] << endl;
	return 0;
}



View PostNeoTifa, on 12 November 2011 - 07:50 PM, said:

int* table;
...
table = new int[size];

:D It compiles!

That's dynamic allocation. Be sure to delete the array when you're done with it or you will have a memory leak.
Was This Post Helpful? 1
  • +
  • -

#10 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2497
  • View blog
  • Posts: 15,460
  • Joined: 24-September 08

Re: Autoinitializing values in an array [theory]

Posted 13 November 2011 - 05:42 PM

By Jove, you're right! I forgot to add a destructor. :/
Was This Post Helpful? 0
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Autoinitializing values in an array [theory]

Posted 13 November 2011 - 05:52 PM

My bare bones array looks something like:
template<class T>
struct Array {
	T *data;
	const unsigned int size;
	Array(unsigned int n) : size(n) { data = new T[size]; }
	~Array() { delete [] data; }
	T &operator[](unsigned int index) { return data[index]; }
};


Was This Post Helpful? 0
  • +
  • -

#12 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: Autoinitializing values in an array [theory]

Posted 13 November 2011 - 06:55 PM

Note there Baavgai that if you have a deconstructor you need a copy constructor and an assignment operator too! All three need to be defined of any one of them is more like:
#include <algorithm>
template<class T>
struct Array {
	T *data;
	unsigned int size;
	Array(const unsigned int n) : size(n) { data = new T[size]; }
    Array(const Array& other) {
        size = other.size;
        data = new T[size];
        std::copy(other.data, other.data+size, data);
    }
	~Array() { delete [] data; }
	T &operator[](unsigned int index) { return data[index]; }
    Array<T>& operator=(const Array& other) { 
        if (&other != this) {
            delete[] data;
            size = other.size;
            data = new T[size];
            std::copy(other.data, other.data+size, data);
        }
        return *this; 
    }
};

#include <iostream>
#include <string>
using namespace std;

int main() {
    Array<string> tempNames(10);
    tempNames[0] = "Nicolas Sarkozy";
    tempNames[1]= "Francois Fillon";
    tempNames[2] = "Bruno Le Maire";
    tempNames[3] = "Nadine Morano";
    tempNames[4] = "Valerie Pecresse";
    Array<string> frenchGovNames(tempNames);
    Array<string> namesToDisplay(10);
    namesToDisplay = frenchGovNames;
    for(int i = 0; i < 10; i++) {
        cout << "Name: \"" << namesToDisplay[i] << "\"" << endl;
    }
    return 0;
}

Was This Post Helpful? 2
  • +
  • -

#13 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Autoinitializing values in an array [theory]

Posted 14 November 2011 - 07:19 AM

Heh, you'll note the "bare bones" conditional. Simply, the user of the class should have some idea what they want to do with it.

You're right, of course, I should have added:
private:
	Array(const Array&);



So no one would be doing silly stuff like:
Array<string> foo(bar);
Array<string> baz = bar;



Note, this is also the reason for the const, to frustrate misuse. Why? Because arrays aren't that clever either. ;)

If we're going for clever, then it's time to have a little privacy:
template<typename T>
class Array {
public:
	Array(const Array &);
	Array(unsigned int);
	~Array();
	Array<T>& operator=(const Array &);
	T &operator[](unsigned int index);
	const T &operator[](unsigned int index) const;
	unsigned int length() const;
private:
	T *data;
	unsigned int size;
};




You got me to thinking though. Do I want my operator= to simply make a copy, or refer to the same instance? The issue then becomes shared memory space and how to manage it.

So, I thought this was fun:
#include <iostream>

template<typename T>
class Array {
public:
	Array(const Array &);
	Array(unsigned int);
	~Array();
	Array<T>& operator=(const Array &);
	T &operator[](unsigned int index);
	const T &operator[](unsigned int index) const;
	unsigned int length() const;
	Array<T> clone() const;
private:
	struct Instance {
		T *data;
		const unsigned int size;
		unsigned int count;
		Instance(unsigned int n) : size(n), data(new T[n]), count(1) { }
	} *instance;
};

void show(const Array<std::string> &a) {
	for(unsigned int i=0, size=a.length(); i<size; i++) {
		std::cout << "Name: \"" << a[i] << "\"" << std::endl;
	}
	std::cout << std::endl;
}


int main() {
	using namespace std;
	Array<string> tempNames(10);
	tempNames[0] = "Nicolas Sarkozy";
	tempNames[1]= "Francois Fillon";
	tempNames[2] = "Bruno Le Maire";
	tempNames[3] = "Nadine Morano";
	tempNames[4] = "Valerie Pecresse";
	Array<string> frenchGovNames(tempNames);
	Array<string> namesToDisplay(10);
	namesToDisplay = frenchGovNames;
	Array<string> ac = namesToDisplay.clone();
	show(namesToDisplay);
	tempNames[3] = "Bob";
	show(namesToDisplay);
	show(ac);
	return 0;
}


template<typename T> Array<T>::Array(unsigned int n) : instance(new Instance(n)) { }
template<typename T> Array<T>::Array(const Array &a) : instance(a.instance) { instance->count++; }
template<typename T> Array<T>& Array<T>::operator=(const Array &a) {
	if (instance!=a.instance) {
		if (--(instance->count)==0) { delete instance; }
		instance = a.instance;
		instance->count++;
	}
	return *this; 
}
template<typename T> Array<T>::~Array() { if (--(instance->count)==0) { delete instance; } }
template<typename T> T &Array<T>::operator[](unsigned int index) { return instance->data[index]; }
template<typename T> const T &Array<T>::operator[](unsigned int index) const { return instance->data[index]; }
template<typename T> unsigned int Array<T>::length() const { return instance->size; }

template<typename T> Array<T> Array<T>::clone() const {
	Array<T> a(instance->size);
	std::copy(instance->data, instance->data+instance->size, a.instance->data);
	return a;
}



Probably not the most C++y way to go and far from thread safe, but it amused me.
Was This Post Helpful? 0
  • +
  • -

#14 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2497
  • View blog
  • Posts: 15,460
  • Joined: 24-September 08

Re: Autoinitializing values in an array [theory]

Posted 14 November 2011 - 12:53 PM

Lol you nerds. <3 Over analyzing everything. :wub:


I like how people keep posting on a solved topic instead of helping me with my current problem. Lol. Oh well, too late now.

This post has been edited by NeoTifa: 14 November 2011 - 12:53 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1