• Advertisement
Sign in to follow this  

Pointer to a Dynamic Array

This topic is 3035 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

typedef struct 
{ 
   long *array; 
   Childe smallChild; 
}Parent; 

typedef struct 
{ 
   long *arrayPtr; 
}Childe; 

void foo(Parent &p) 
{ 
   p->array = new long[40]; 
   //fill with data into p->array 
   p->smallChild.arrayPtr = p->array; //I shouldn't need to use it like &p->array 
}


This doesn't exactly yield the results I want. I want to be able to use the elements of the Parent structure's array from within the child. However, to save memory I'm trying to just create a pointer to that memory rather than copying the data into another memory allocated array. I'll probably hit myself when I figure out the solution is something stupid, but I just can't think straight about this problem at the moment. AFAIK
int foo[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};

//ISN'T THE SAME AS

int *foo;

foo = new int [10];

//FILL the Dynamic foo with 1-0


Those two IIRC aren't the same right? The first is a literal array whereas the 2nd is just an int pointer to the first memory location right? So that would make this more to a Pointer to a Pointer problem?
//In a nutshell I'm trying to do something like this.
int foo[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};

int *fooPtr = &foo;


Just with two dynamic arrays. Any assistance that can be provided with this would be greatly appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Jinroh
*** Source Snippet Removed ***


This doesn't exactly yield the results I want. I want to be able to use the elements of the Parent structure's array from within the child. However, to save memory I'm trying to just create a pointer to that memory rather than copying the data into another memory allocated array.


Yup, that's what you're doing here.

Quote:

*** Source Snippet Removed ***

Those two IIRC aren't the same right? The first is a literal array whereas the 2nd is just an int pointer to the first memory location right?


Correct. (However, the first version will decay into a pointer to its contents automatically when needed.)

Quote:

So that would make this more to a Pointer to a Pointer problem?

*** Source Snippet Removed ***


You'd just do..
int *fooPtr = foo;

..because of the aforementioned decay-to-pointer feature.

Quote:

Just with two dynamic arrays.


Then what you're doing now should be working fine, i.e,

int *a = new int[10];
int *b = a;
// Now a and b point to the same dynamically-allocated region of memory



(As an aside, I am required by law to tell you that typedef'ing structs is not needed in C++, and you should probably be using std::vector for all of this anyway :])

Share this post


Link to post
Share on other sites
Quote:

Then what you're doing now should be working fine, i.e,

int *a = new int[10];
int *b = a;
// Now a and b point to the same dynamically-allocated region of memory



(As an aside, I am required by law to tell you that typedef'ing structs is not needed in C++, and you should probably be using std::vector for all of this anyway :])


Ok, I'll give it a shot, they're both in reference mode within a function so I may need to dereference them, I'll continue to play around with it, let me know if there's any flaw to this though.


currentChild->arrPtr = Parent->array;




Thanks Matt, I was on kinda the right wavelength at least. ^_^

Share this post


Link to post
Share on other sites
Quote:
Original post by Jinroh
they're both in reference mode within a function so I may need to dereference them


Not sure what you mean by that.. if you're referring to the way p is passed in by reference in your very first example (void foo(Parent &p)), then you'd need to be doing this:

void foo(Parent &p)
{
p.array = new long[40];
// fill with data into p.array
p.smallChild.arrayPtr = p.array;
}






I didn't bring this up earlier because I thought you'd written this code on-the-spot without test-compiling it first, and that it wasn't what you were questioning about.

Share this post


Link to post
Share on other sites
EDIT: Fixed

I wasn't doing anything wrong, but the other functions were screwing something up. So I guess it was wrong by association. :P

Thanks. ^_^

[Edited by - Jinroh on November 1, 2009 10:49:21 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement