dynamic-arrays

How can I change the length of an array? [duplicate]

限于喜欢 提交于 2019-12-02 13:07:38
This question already has an answer here: Java dynamic array sizes? 18 answers So I am assigned with a project where I have an array and as the user puts elements into this array it will have to double in length once it gets full. We are not permitted to use array lists or anything in the collections interface. What I was trying to do was to make a new array once the old one was full, and then I would copy the values over to the new array. The problem is I don't know how many times I will have to make a new array, so I was wondering how to go about solving this. Arrays are fixed length. If you

Images in Uiscroll cell populated by mysql database

吃可爱长大的小学妹 提交于 2019-12-02 12:49:04
问题 I am stuck somewhere, I am dynamically pulling images from MYSQL database using PHP, that is all ok until I reach the point of retrieving the names of the photo from the URL. I want to dynamically insert thumbnails on UIScroll View so that users can scroll horizontally to view all the images, description of the photos, summary of the photos, I would also like them to click and open more details Here is my code so far -(void)getItems { NSString *root = URL NSURL *url = [NSURL URLWithString:

Returning Vectors standard in C++

北城余情 提交于 2019-12-02 12:07:33
Now, I know this is a common question, but I haven't been able to really find a straight answer on this. This is really a question about standards. I am working on a project involving the genetic algorithm. But I'm running into a bottleneck when it comes to returning a vector. Is there a "proper" way to do this. Normally I use dynamically allocated arrays, and return a pointer to a newly created array. obj* func(obj* foo); That way, everything is efficient and there is no copying of data. Is there an equivalent to doing this with a vector? This vector has objects in it, so returning it by

finding specific indices with pointer array

帅比萌擦擦* 提交于 2019-12-02 11:56:39
问题 I am relatively new to Fortran and break my head about one thing for hours now: I want to write a subroutine for finding the indexes for specific elements in a real 1D array (given to the routine as input). I have generated an array with 100 random reals, called arr , and now want to determine the indexes of those elements which are greater than a real value min , which is also passed to subroutine. Plus, in the end I would like to have a pointer I'd allocate in the end, which I was said

Fortran: How do I allocate arrays when reading a file of unknown size?

空扰寡人 提交于 2019-12-02 11:55:10
My typical use of Fortran begins with reading in a file of unknown size (usually 5-100MB). My current approach to array allocation involves reading the file twice. First to determine the size of the problem (to allocate arrays) and a second time to read the data into those arrays. Are there better approaches to size determination/array allocation? I just read about automatic array allocation (example below) in another post that seemed much easier. array = [array,new_data] What are all the options and their pros and cons? I'll bite, though the question is teetering close to off-topicality. Your

Need to know how to parse words by space in c. Also need to know if I am allocating memory correctly?

三世轮回 提交于 2019-12-02 09:03:17
问题 I am writing a program in c that reads in text from a text file then randomly selects words from the file and if the words are greater than or equal to six it appends the words together, removes the spaces, and finally prints the new word. (I am using the redirect on linux "<" to read in the file) Example input: "cheese and crackers" New word should be: cheesecrackers Here is the code: int main (void) { int ch; char *ptrChFromFile; int strSize = 1; int i; int numberOfWords = 1; ptrChFromFile

Declaring and allocating a 2d array in C++

旧城冷巷雨未停 提交于 2019-12-02 04:56:23
问题 I am a Fortran user and do not know C++ well enough. I need to make some additions into an existing C++ code. I need to create a 2d matrix (say A) of type double whose size (say m x n) is known only during the run. With Fortran this can be done as follows real*8, allocatable :: A(:,:) integer :: m, n read(*,*) m read(*,*) n allocate(a(m,n)) A(:,:) = 0.0d0 How do I create a matrix A(m,n), in C++, when m and n are not known at the time of compilation? I believe the operator new in C++ can be

What is the purpose of ANYSIZE_ARRAY in <winnt.h>?

£可爱£侵袭症+ 提交于 2019-12-01 20:16:17
问题 What's the purpose of ANYSIZE_ARRAY , located in WinNT.h? I see an MSDN blog post about it from 2004 but it doesn't make sense to me. 回答1: I assume you are talking about this blog post. It is often used when a variable-sized (unknown at compile time) array is part of a struct: typedef struct { int CommonFlags int CountOfThings; THING Things[ANYSIZE_ARRAY]; //Things[1]; } THINGSANDFLAGS; To work with those structures you often first call the desired API to get the size of the data, then

“Pointer being freed was not allocated.” error after malloc, realloc

岁酱吖の 提交于 2019-12-01 19:46:59
问题 I have this error with the following code: int main(){ point *points = malloc(sizeof(point)); if (points == NULL){ printf("Memory allocation failed.\n"); return 1; } other_stuff(points); free(points); return 0; } void other_stuff(point points[]){ //stuff realloc(points, number*sizeof(point)) } I have searched, but found only examples where it was clear there was no allocation. Here, I used malloc to initialise points , and later changed its size with realloc ; so how is the pointer "not

“Pointer being freed was not allocated.” error after malloc, realloc

别等时光非礼了梦想. 提交于 2019-12-01 19:09:10
I have this error with the following code: int main(){ point *points = malloc(sizeof(point)); if (points == NULL){ printf("Memory allocation failed.\n"); return 1; } other_stuff(points); free(points); return 0; } void other_stuff(point points[]){ //stuff realloc(points, number*sizeof(point)) } I have searched, but found only examples where it was clear there was no allocation. Here, I used malloc to initialise points , and later changed its size with realloc ; so how is the pointer "not allocated" when I come to free it? realloc may move the memory to a new location (if there is not enough