Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,195,598 members, 7,958,840 topics. Date: Thursday, 26 September 2024 at 04:58 AM |
Nairaland Forum / Science/Technology / Programming / Must My Array Or Pointer Allocation Have A Limit In C Or C++? (1326 Views)
Can An Array In Java Hold Multiple Types? / Creating UWP Application In C# Or C++ (xaml) / Is There A Limit To How Far One Can Go In Programming Without Mathematics? (2) (3) (4)
Must My Array Or Pointer Allocation Have A Limit In C Or C++? by Nobody: 11:37pm On Jan 03, 2015 |
I want to create a c/c++ program that accepts an unknown set of integers at runtime but I dont want to declare the size of the array or pointer in my program. I want the program to be dynamic enough to resize itself to the user inputs. I don't want a declaration like a[100] or a = new int[100] or a*int=a*malloc[100], etc because of the lenght 100 (or any other number). My question is if it is possible for a dataype like integer to read values at runtime with unknown lenght? |
Re: Must My Array Or Pointer Allocation Have A Limit In C Or C++? by tjones007: 9:13am On Jan 04, 2015 |
It is possible with dynamic array allocation. The array is stored in the heap. So remember to free or delete it. In c++ array is dynamically assigned using new which return a pointer. For example. Int* myarray = new int[num]; In c u using malloc to dynamically assigned memory at run time. You can google for more clarity. |
Re: Must My Array Or Pointer Allocation Have A Limit In C Or C++? by Javanian: 9:24am On Jan 04, 2015 |
Or Just use a Linked List. 1 Like |
Re: Must My Array Or Pointer Allocation Have A Limit In C Or C++? by 2mNaira: 8:21pm On Jan 04, 2015 |
Marsatto: @tjones007 has given u the right answer. |
Re: Must My Array Or Pointer Allocation Have A Limit In C Or C++? by Nobody: 1:50pm On Jan 05, 2015 |
tjones007: You don't seem to understand the question. Based on your answer I will still need to enter a "num" at runtime. I said I want the program to only stop when there is a delimiter (maybe -1). Based on your answer if my entry exceeds the num the program will not work. Your reply is almost same as my illustration only that I used "n". |
Re: Must My Array Or Pointer Allocation Have A Limit In C Or C++? by tjones007: 8:19pm On Jan 05, 2015 |
Marsatto: Ooh I get you. Why not use linkedlist. Or you can create your customize list with a that assign a static array then later change the size of the array when the value have exceed it. |
Re: Must My Array Or Pointer Allocation Have A Limit In C Or C++? by ijeezlux(m): 11:35am On Jan 06, 2015 |
Marsatto:am stil having the same problem. I also find it dificut to alocate memory to array of arbitrary set of elements. May b u shud gv ur number so we can discus it more in whatsap. Any way my num is 08036407597. Snd me a whatsap msg. Tnks |
Re: Must My Array Or Pointer Allocation Have A Limit In C Or C++? by HouseOfBalloons(f): 12:06pm On Jan 06, 2015 |
Marsatto:No. Just leave it vacant. Ie. A[ ] |
Re: Must My Array Or Pointer Allocation Have A Limit In C Or C++? by 2mNaira: 5:57pm On Jan 06, 2015 |
Marsatto: my friend why make life difficult for urself. u have two simple options; 1. use a vector 2. Write ur own dynamic array.( view my old post u will see an example u can adapt. note:a vector is a dynamic array. 1 Like |
Re: Must My Array Or Pointer Allocation Have A Limit In C Or C++? by neuralnets: 10:40pm On Jan 06, 2015 |
tjones007: you can do as quoted above. but instead of allocating the entire array, you can use realloc to dynamically resize the array after every n data input. say 10 or more. you have another variable tracking how much data has been entered. when you get your delimiter, you can now use to realloc to fit the array perfectly. //////////////////////////////////////////////////////////////// int counter, arraysize; unsigned char *dataArray, temp; counter = arraysize = 0; dataArray = malloc(10); while((temp = get_data_routine()) != -1) //continue loop until delimiter found { if(counter == arraysize) //if counter catches up with arraysize, increase array by 10 bytes { arraysize += 10; //new array size dataArray = realloc(arraysize); } *(dataArray + counter) = temp; counter += 1; } *dataArray = realloc(counter); //finally resize array to fit ................................ 1 Like |
Re: Must My Array Or Pointer Allocation Have A Limit In C Or C++? by codemarshal08(m): 10:25am On Jan 07, 2015 |
Op Check mnairaland answer. Use a Vector of ints. You can Push_back till mummy calls ! 1 Like |
(1) (Reply)
PHP Programmers:Name Your Best HMVC Framework / Javascript Developers / Bluestacks App (is It Free Or Paid)
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 26 |