| | |
how to expand arrays
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2007
Posts: 13
Reputation:
Solved Threads: 0
Hello sorry if i hijack this thread for my own purpose, first English isent my first language, my apologies for the misspelling and all.
I also have problem with dynamic arrays, our stupied teacher made the assingment harder then he hade to. I'll try to explain the assingment.
First we are supposed to use dynamic arrays, like
And thats fine its nothing special about that, but he whants us to write a funktion that expands(s*) the dynamic array one integer at the time
I think this code will work.
the problem is that i have to controll the size ( n ) of the array to make room for the next integer. I have tried
and that wont work it returns 1 att the time.
Does any one have a simpel ansver to my problem?
I also have problem with dynamic arrays, our stupied teacher made the assingment harder then he hade to. I'll try to explain the assingment.
First we are supposed to use dynamic arrays, like
C++ Syntax (Toggle Plain Text)
int *list = new int;
And thats fine its nothing special about that, but he whants us to write a funktion that expands(s*) the dynamic array one integer at the time
C++ Syntax (Toggle Plain Text)
void expandera_lista( int *&list , int n) { int *templist = new int[n]; for(int i=0; i<=n;i++) { templist[i] = list[i]; } delete[] list; list = new int[n+1]; for(int i=0; i<=n;i++) { list[i] = templist[i]; } delete[] templist; }
I think this code will work.
the problem is that i have to controll the size ( n ) of the array to make room for the next integer. I have tried
C++ Syntax (Toggle Plain Text)
int n = (sizeof(*list)/sizeof(list[0]))
and that wont work it returns 1 att the time.
Does any one have a simpel ansver to my problem?
Last edited by Ancient Dragon; Sep 30th, 2007 at 6:54 pm. Reason: add line numbers to code tags
>>Hello sorry if i hijack this thread for my own purpose
Please don't do it again. I split your thread this time for you for free
Line 5 is incorrect. Arrays are numbered from 0 to, but not including, the value of N. You should use the < operator, not <= operator.
To solve your problem I think just pass the second parameter by reference and let expandera_lista() increment it. That way the calling function will always have an updated copy of the size of the array.
Your function is also doing too much copying. You only have to copy once.
Please don't do it again. I split your thread this time for you for free

Line 5 is incorrect. Arrays are numbered from 0 to, but not including, the value of N. You should use the < operator, not <= operator.
To solve your problem I think just pass the second parameter by reference and let expandera_lista() increment it. That way the calling function will always have an updated copy of the size of the array.
Your function is also doing too much copying. You only have to copy once.
C++ Syntax (Toggle Plain Text)
void expandera_lista( int *&list , int& n) { int *templist = new int[n+1]; for(int i=0; i<n;i++) { templist[i] = list[i]; } delete[] list; list = templist; ++n; }
Last edited by Ancient Dragon; Sep 30th, 2007 at 7:02 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Sep 2007
Posts: 13
Reputation:
Solved Threads: 0
Thank you ancient dragon for your answer, and your code exampel =). My problem is a bit diffrent that what u describe, i'll cut and paste the code so u can have a resonable chans to understand my problem. l read trough my last post and i dident give u a fair chans to understand my problem.
well the code dosent work as it is, but u get the general idea how its suppose to work. My problem is how to get the size of my array when its a dynamic array, the way i have tried dont work, it returns 1 allt the time.
ps our teacher still is an idiot=) ds or mybe its me.
well the code dosent work as it is, but u get the general idea how its suppose to work. My problem is how to get the size of my array when its a dynamic array, the way i have tried dont work, it returns 1 allt the time.
ps our teacher still is an idiot=) ds or mybe its me.
C++ Syntax (Toggle Plain Text)
// Laboration1.3.cpp : Defines the entry point for the console application. //DYNAMISKA FÄLT #include "stdafx.h" void fylla_lista(int *&list, int n, int tal) { list[n+1]=tal; } void expandera_lista( int *&list , int n) { int *templist = new int[n]; for(int i=0; i<=n;i++) { templist[i] = list[i]; } delete[] list; list = new int[n+1]; for(int i=0; i<=n;i++) { list[i] = templist[i]; } delete[] templist; } void printa_lista ( int *list , int n) { for(int i=0; i<=n;i++) std::cout<<list[i]<<std::endl; } int _tmain(int argc, _TCHAR* argv[]) { char vanta; int *list = new int[1]; int tal=0, n=0; std::cout<<"Skriv in ett tal at gangen och avsluta med CTRL-Z\n\n"; while(!std::cin.eof()) { std::cin>>tal; list[i++]=tal; **the problem** *n = (sizeof(*list)/sizeof(list[0])) ;* expandera_lista(list,n); fylla_lista(list, n, tal); } printa_lista(list,n); delete[] list; std::cin>>vanta; // the DOSpromt vill stay activ return 0; }
>My problem is how to get the size of my array when its a dynamic array
You allocated the array, you should know the size. In other words, there's no portable way to find the size of allocated memory given nothing but a pointer to it. You need to remember the size yourself and pass it around along with the pointer.
You allocated the array, you should know the size. In other words, there's no portable way to find the size of allocated memory given nothing but a pointer to it. You need to remember the size yourself and pass it around along with the pointer.
I'm here to prove you wrong.
![]() |
Similar Threads
- (reformatted) How to return Multi-Dimensional Arrays (C++)
- A couple Qs (arrays, c++, and reading in text files) (C++)
- Sprintf and buffer overflow (C)
- Arrays (C++)
- How to Return Multidimensional Arrays (C++)
- C file input/output 2D arrays. (C)
- passing arrays in visual basic (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: Assistance needed with craps game
- Next Thread: Determining max and min
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






