Question about arrays..

K

kiddler

I'm sorta new to the C++ syntax.

I know that arrays can be delcared like so:
int a[]={1,2,3,4};

but what if I want the array to be a global, and then set the length
and value inside a method?

like in java, you can do:

int a[];
void main()
{
a=new int{1,2,3,4};
}

How do I do that in C++?
 
J

Jerry Coffin

I'm sorta new to the C++ syntax.

I know that arrays can be delcared like so:
int a[]={1,2,3,4};

but what if I want the array to be a global, and then set the length
and value inside a method?

You can't do it (not directly anyway). If you want to do that, you're
probably better off using an std::vector instead.
 
A

AB

like in java, you can do:
int a[];
void main()
{
a=new int{1,2,3,4};
}

How do I do that in C++?

No direct method, but you can try this..

int* a ;//global declaration

void method()
{
a = new int[size] ;
for(i = 0; i < size; i++)
a = value ;
}
 
C

chriskr7

AB said:
like in java, you can do:

int a[];
void main()
{
a=new int{1,2,3,4};
}

How do I do that in C++?

No direct method, but you can try this..

int* a ;//global declaration

void method()
{
a = new int[size] ;
for(i = 0; i < size; i++)
a = value ;
}


another way -

int* a;

void method()
{
int b[] = {1, 2, 3, 4};
a = new int [4];
memcpy(a, b, 4);
//think it's better for speed of performance
}//
 
T

Thomas J. Gritzan

AB said:
like in java, you can do:

int a[];
void main()

main returns an int:

int main()
{
a=new int{1,2,3,4};
}

How do I do that in C++?

No direct method, but you can try this..

int* a ;//global declaration

void method()
{
a = new int[size] ;
for(i = 0; i < size; i++)
a = value ;
}


Don't do it the Java way when you program C++. Where would you delete[]
the pointer?

Better would be a std::vector as Jerry suggests:

#include <vector>

std::vector<int> a;

int main()
{
a.push_back(1);
a.push_back(2);
a.push_back(3);
a.push_back(4);

// or:
a.resize(4);
for (int i = 0; i < 4; i++)
a = i+1;
}
 
V

Victor Bazarov

Thomas said:
AB said:
like in java, you can do:

int a[];
void main()

main returns an int:

int main()
{
a=new int{1,2,3,4};
}

How do I do that in C++?

No direct method, but you can try this..

int* a ;//global declaration

void method()
{
a = new int[size] ;
for(i = 0; i < size; i++)
a = value ;
}


Don't do it the Java way when you program C++. Where would you
delete[] the pointer?

Better would be a std::vector as Jerry suggests:

#include <vector>

std::vector<int> a;

int main()
{
a.push_back(1);
a.push_back(2);
a.push_back(3);
a.push_back(4);

// or:
a.resize(4);
for (int i = 0; i < 4; i++)
a = i+1;
}


Why is 'a' *global*? Is there really *the need* for it to be global?

V
 
T

Thomas J. Gritzan

Victor said:
Thomas said:
Don't do it the Java way when you program C++. Where would you
delete[] the pointer?

Better would be a std::vector as Jerry suggests:

#include <vector>

std::vector<int> a;

int main()
{
a.push_back(1);
a.push_back(2);
a.push_back(3);
a.push_back(4);

// or:
a.resize(4);
for (int i = 0; i < 4; i++)
a = i+1;
}


Why is 'a' *global*? Is there really *the need* for it to be global?


Because the OP wanted it:
but what if I want the array to be a global, and then set the length
and value inside a method?

I wonder how he got the global in Java to work...
 
F

Frederick Gotham

kiddler posted:
I'm sorta new to the C++ syntax.

I know that arrays can be delcared like so:
int a[]={1,2,3,4};

but what if I want the array to be a global, and then set the length
and value inside a method?

like in java, you can do:

int a[];
void main()
{
a=new int{1,2,3,4};
}

How do I do that in C++?


std::vector would probably be the way to go.

Or if you're feeling creative. . .

template<class T>
class Array {
private:

T *pNC;
unsigned lenNC;

public:

T * const p;

unsigned const &len;


Array() : p(pNC), len(lenNC) {}

void Allocate(unsigned const quantity)
{
lenNC = quantity;

pNC = new T[len];
}

void Deallocate()
{
delete [] pNC;
}
};


Array<int> a;


int main()
{
a.Allocate(4);

a.p[0] = 1; a.p[1] = 2; a.p[2] = 3; a.p[3] = 4;


...


a.Deallocate();
}
 
V

Victor Bazarov

Frederick said:
kiddler posted:
I'm sorta new to the C++ syntax.

I know that arrays can be delcared like so:
int a[]={1,2,3,4};

but what if I want the array to be a global, and then set the length
and value inside a method?

like in java, you can do:

int a[];
void main()
{
a=new int{1,2,3,4};
}

How do I do that in C++?


std::vector would probably be the way to go.

Or if you're feeling creative. . .

template<class T>
class Array {
private:

T *pNC;
unsigned lenNC;

public:

T * const p;

unsigned const &len;


Array() : p(pNC), len(lenNC) {}

Neither 'pNC' nor 'lenNC' are initialised. Initialising other members
with their values (indeterminate, BTW), is very dangerous.
void Allocate(unsigned const quantity)
{
lenNC = quantity;

pNC = new T[len];

This doesn't seem to do anything with 'p'. What is 'p' for, then?
}

void Deallocate()
{
delete [] pNC;
}
};


Array<int> a;


int main()
{
a.Allocate(4);

a.p[0] = 1; a.p[1] = 2; a.p[2] = 3; a.p[3] = 4;

Undefined behaviour. You're dereferencing an uninitialised pointer.
...


a.Deallocate();
}

V
 
T

Thomas J. Gritzan

Victor said:
Frederick said:
Or if you're feeling creative. . .

template<class T>
class Array {
private:

T *pNC;
unsigned lenNC;

public:

T * const p;

unsigned const &len;


Array() : p(pNC), len(lenNC) {}

Neither 'pNC' nor 'lenNC' are initialised. Initialising other members
with their values (indeterminate, BTW), is very dangerous.
void Allocate(unsigned const quantity)
{
lenNC = quantity;

pNC = new T[len];

This doesn't seem to do anything with 'p'. What is 'p' for, then?

Maybe p is supposed to be a reference like len.
Doesn't change the fact that it's bad advice, and that there is no
destructor.
}

void Deallocate()
{
delete [] pNC;
}
};
 
F

Frederick Gotham

Thomas J. Gritzan posted:
Maybe p is supposed to be a reference like len.
Doesn't change the fact that it's bad advice, and that there is no
destructor.


Yes, the code was unchecked, and I posted in haste.

"p" was supposed to be a reference.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top