Hi everyone:

I am fairly new to c++. I have created a dynamic array, but I am having trouble initializing it. My goal is to create a grid, and then assign an initial condition (for example, initial temperature = some value) on the grid points.

I know that it would be straightforward to use a vector, for example:

vector<double> u(I, 0.5);

but I need to use an array in this case.

Here is what I've done so far:

int I;
int i;                

double* u  ;                

u = new double[I*sizeof(double)];

for( i=0 ; i < I ; i++ ) 
	{
        e     = 0.5;
	u[i]  = e;  // I have also tried u[i] = {0.5};
		
		cout << "The vector u[i] contains:\n";
		for (i=0 ; i < I ; i++ )
		{
			cout << u[i] << endl;
		}
	}

The problem is that the result is this:
The vector u contains:
0
0.5
0
0
0
0
0
0
0
0
logout

I would very much appreciate it if anyone could suggest why only one of the grid points has the value I want (0.5) and what I might do to assign 0.5 on all the grid points.

Thank you for your help.

Dani AI

Generated

A short expert summary and practical tips. identified the key allocation/loop problems and confirmed the fix — useful for anyone who finds this thread later: the original symptoms come from a mixture of undefined values (size not set), incorrect allocation math, and a printing loop nested inside the initialization loop that reused the same loop index. The result is undefined behavior, so the observed single 0.5 amid zeros is not surprising.

Checklist (quick things to verify)

  • Make sure the array size variable is given a valid value before allocating.
  • Allocate by element count, not by trying to pass bytes to the array-new expression.
  • Do initialization and printing in separate loops (and avoid reusing the same index variable for nested loops).
  • Declare any temporary variable you use (e.g., the e in the original post) and avoid relying on implicit/default values.
  • Prefer RAII (std::vector or smart pointers) for exception-safety and automatic cleanup.

A safer, modern pattern (C++11+) — allocate and fill without raw loop bookkeeping:

#include <memory>
#include <algorithm>

size_t N = 100;                         // set your grid size
auto u = std::make_unique<double[]>(N); // RAII-managed array
std::fill_n(u.get(), N, 0.5);           // set every element to 0.5

If you must use raw new[], validate the size first, initialize elements explicitly (not with memset for doubles), and call delete[] when done. Compile with warnings enabled (e.g., -Wall -Wextra) and run under AddressSanitizer or Valgrind to catch uninitialized reads/out-of-bounds. These checks will quickly reveal the sorts of problems that produced the mixed-zero output in the original code.

Recommended Answers

All 3 Replies

>>u = new double[I*sizeof(double)];

No! This is not C. In C++ its used like this :
u = new double[ I ];

The value inside [ ], is the number of elements. No need for the sizeof.

Put your printing loop out side separately. Like this :

void Foo(const int Size, const double value = 0 )
{
   double *Array = new double[ Size];
  //initialize it
   for( int i = 0; i != Size; ++i){
     Array[i] = value;
   }

   //print it
   for(int i = 0; i != Size; ++i){  
     cout << Array[i] << " ";
  }
}

>>u = new double[I*sizeof(double)];

No! This is not C. In C++ its used like this :
u = new double[ I ];

The value inside [ ], is the number of elements. No need for the sizeof.

Put your printing loop out side separately. Like this :

void Foo(const int Size, const double value = 0 )
{
   double *Array = new double[ Size];
  //initialize it
   for( int i = 0; i != Size; ++i){
     Array[i] = value;
   }

   //print it
   for(int i = 0; i != Size; ++i){  
     cout << Array[i] << " ";
  }
}

Hi firstPerson:

Thanks a lot! That does the trick. Much appreciated.

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.