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

``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

``````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

``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?

## All 6 Replies

It would be better starting a new thread for this. It is also good netiquette.

I dunno. It's always good to resurect 2 year old threads!

>>Hello sorry if i hijack this thread for my own purpose

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.

``````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;
}``````

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.

``````// 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.

use a vector instead of a simple c-style array and you won't have that problem. c++ vector class has a method that returns the current size of the array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.