954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Dynamic Memory Allocation of Array of Structures.

Quick Question. For some reason I can't figure out how to get an array of structures if I am defining the size dynamically.

I can do it for a pointer i.e

struct DB
{
   string name;
   int age;
   string ice;
};

//some lines

DB* data1p;
    data1p= new DB[no1];


but i can't figure out how to do the equivalent of pointer = &variable.

Thanks for any help.

Incidently when you get stuck on little problems like this and can't figure them out where do you look for help?

coveredinflies
Light Poster
32 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 
Incidently when you get stuck on little problems like this and can't figure them out where do you look for help?

DaniWeb :)

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

The address of a variable returned by the reference operator (&) is not an l-value, meaning you can't put it on the left side of an assignment operator. Besides, why would you want to?

CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 77
Solved Threads: 40
 

do you mean equivalent reference variable?

Prabakar
Posting Whiz
342 posts since May 2008
Reputation Points: 94
Solved Threads: 33
 

Hmm, perhaps you could do:

int x;
	x = *(new int[5]);
	(&x)[0] = 1;
	(&x)[1] = 1337;

but I believe this is a very bad idea. new is creating an array of five ints, and this array is being copied into x, leaving the original array somewhere in memory that is unknown to us, so we can't delete it later. Also, x only has room for one int, not 5, so copying 5 ints into x may intrude on memory belonging to someone else.

CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 77
Solved Threads: 40
 

Perhaps this

#include <iostream>

int main ( )
{
	int &x = *(new int[5]);
	(&x)[0] = 1;
	(&x)[1] = 1337;
	std::cout<<(&x)[0]<<(&x)[1] ;
	std::cin.get() ;
}

again not advisable, for the above reasons

Prabakar
Posting Whiz
342 posts since May 2008
Reputation Points: 94
Solved Threads: 33
 

Thanks for all the replies people.

Besides, why would you want to?

Hmm I guess I was just trying to continue as I had been before i.e int* a; a=&b; Then using b unless I want to send it into a function or something but I guess this doesn't carry over in this case.
Just to check I have got the right end of the stick, you are saying it is best to just define a pointer and continually dereference it every time? There is no "normal" variable defined at the memory address of the pointer.

coveredinflies
Light Poster
32 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 
Hmm I guess I was just trying to continue as I had been before i.e int* a; a=&b; Then using b unless I want to send it into a function or something but I guess this doesn't carry over in this case.


I'm not following exactly...

a contains the address of b. modifying b is the same as modifying *a, until a is resigned to another address, or b goes out of scope (or a goes out of scope)Just to check I have got the right end of the stick, you are saying it is best to just define a pointer and continually dereference it every time? There is no "normal" variable defined at the memory address of the pointer.
I'm not sure what you mean by "normal variable". Do you mean a primitive data type (i.e., int, float, double, char, short, long, etc.)? If so, there can definitely be a normal variable at the address of a pointer:

int* x = new int;//there is an int at the memory address of int
*x = 5;
//work with x, even past ends of scope
delete x;//you need to manually free the int, C++ won't do it for you when you use new

the value that x itself contains is a a memory address. you should only use x without the dereference operator if you're dealing with another pointer

int y = new int;
*y = 6;
delete x;//assuming x wasn't already deleted
x = y;//no dereference
CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 77
Solved Threads: 40
 

I know the basics of working with int, float and structures but I am not as good with pointers I know how to do an array of structures for examples if the structure was called records i would normally do something like records[i].title for example. Since I dynamically allocated the memory I am struggling since I had to define a pointer to a structure and I can't figure out how to do the equivalent i've tried things like (*records[i]).title and records[i]->title.

Hope I've explained better this time.

coveredinflies
Light Poster
32 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 
records* myarray;
myarray = new records[numRecords];
myarray[i].title = "w/e";


or, you you mean:

records** myarray;
myarray = new records*[numRecords];
(*myarray[i]).title = "w/e";//same as...
myarray[i]->title = "w/e";


x[y] is equivalent to *(x + y) . If that's confusing, read "Pointers and Arrays" in this article

Basically, in the first example, [] is already dereferencing the pointer, so you can't dereference it again with * or ->.

CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 77
Solved Threads: 40
 
records** myarray; myarray = new records*[numRecords]; (*myarray[i]).title = "w/e";//same as... myarray[i]->title = "w/e";

Storing values before allocating memory for 'em? I will be surprised if it works.

Prabakar
Posting Whiz
342 posts since May 2008
Reputation Points: 94
Solved Threads: 33
 
Storing values before allocating memory for 'em? I will be surprised if it works.


Oh, right. Sorry.

records** myarray;
myarray = new records*[numRecords];
for(int i = 0;i < numRecords;i++)
    myarray[i] = new records;
(*myarray[2]).title = "w/e";//same as...
myarray[2]->title = "w/e";
CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 77
Solved Threads: 40
 

Thanks had got a bit confused so didn't spot the obvious :)

coveredinflies
Light Poster
32 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You