C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 5th, 2005, 10:46 AM
disruptive disruptive is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 2 disruptive User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 m 4 sec
Reputation Power: 0
Pointers and arrays

I am trying to create a dynamic array - one that increases in element size when I encounter a new thing to be added. It copies over the old elements in the new array, but I find that the returning function does not seem to actually return the correct array. Can you help? The code is listed below:

#include <stdio.h>



void arraycopy(int oldarray[], int newarray[], int index);
void NNListBuild(int lipidIndex, int *NNInput, int *NNList, int arrayindex);

void arraycopy(int oldarray[], int newarray[], int index)
{

int i;
for (i=0;i<index;i++)
{
newarray[i] = oldarray[i];
//printf("copying: %d\n", newarray[i]);
}


}


void NNListBuild(int lipidIndex,int *NNInput, int *NNList, int arrayindex)
{

int *newarray = new int[arrayindex+1];
arraycopy(NNInput, newarray, arrayindex);

NNInput = newarray;
NNInput[arrayindex] = 110;

int i;
for (i=0;i<arrayindex+1;i++)
{

//printf("copying: %d\n", NNInput[i]);
}


NNList = &NNInput[0];

for (i=0;i<arrayindex+1;i++)
{

printf("after copying: %d\n", NNList[i]);
}

//delete [] newarray;

}



int main(void)
{
int arrayindex;
arrayindex = 4;
int *NNList = new int[arrayindex];
int *NNOutput;
NNList[0] = 1;
NNList[1] = 2;
NNList[2] = 3;
NNList[3] = 4;
NNListBuild(7,NNList,NNOutput,arrayindex);





//arraycopy(oldarray,newarray,arrayindex);

int i;
for (i=0;i<arrayindex+1;i++)
{

printf("final output: %d\n", NNOutput[i]);
}


delete [] NNList;
//delete [] newarray;
printf("hello");
return 0;
}

Reply With Quote
  #2  
Old May 5th, 2005, 02:26 PM
ShadowCoder ShadowCoder is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Location: Ottawa
Posts: 20 ShadowCoder User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 54 m 12 sec
Reputation Power: 0
void NNListBuild(int lipidIndex, int *NNInput, int *&NNList, int arrayindex)
{
NNList = new int[arrayindex+1];
arraycopy(NNInput, NNList, arrayindex);
NNList[arrayindex] = 110;
}

int main(void)
{
int arrayindex=4;
int *NNList = new int[arrayindex];
int *NNOutput = NULL;
NNList[0] = 1;
NNList[1] = 2;
NNList[2] = 3;
NNList[3] = 4;
NNListBuild(7,NNList,NNOutput,arrayindex);
delete [] NNList;
delete[] NNOutput;
}

Reply With Quote
  #3  
Old May 6th, 2005, 05:52 AM
disruptive disruptive is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 2 disruptive User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 m 4 sec
Reputation Power: 0
Thanks for that - however it does not seem to work - the returning elemnts of the Output array cannot be displayed leading to a segmentation fault. The code is as you sugggsted.

Reply With Quote
  #4  
Old May 6th, 2005, 10:19 AM
ShadowCoder ShadowCoder is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Location: Ottawa
Posts: 20 ShadowCoder User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 54 m 12 sec
Reputation Power: 0
Quote:
Originally Posted by disruptive
Thanks for that - however it does not seem to work - the returning elemnts of the Output array cannot be displayed leading to a segmentation fault. The code is as you sugggsted.


Code:
void arraycopy(int oldarray[], int newarray[], int index);
void NNListBuild(int lipidIndex, int *NNInput, int *&NNList, int arrayindex);
 
int main(int argc, char* argv[])
{
int arrayindex=4;
int *NNList = new int[arrayindex];
int *NNOutput = NULL;
NNList[0] = 1;
NNList[1] = 2;
NNList[2] = 3;
NNList[3] = 4;
NNListBuild(7,NNList,NNOutput,arrayindex);
for (int i=0; i<5; i++)
{
printf("\n%d.) %d", i, NNOutput[i]);
}
delete[] NNList;
delete[] NNOutput;
getchar();
return 0;
}
 
void arraycopy(int oldarray[], int newarray[], int index)
{
for (int i=0; i<index; i++)
{
newarray[i] = oldarray[i];
}
}
 
void NNListBuild(int lipidIndex, int *NNInput, int *&NNList, int arrayindex)
{
NNList = new int[arrayindex+1];
arraycopy(NNInput, NNList, arrayindex); 
NNList[arrayindex] = 110;
}
 

I do not receive no errors with this code.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Pointers and arrays


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT