Please help me to find error in this program .I am trying to add data to a dynamic array .

#include<iostream.h>
#include<conio.h>
#include<string.h>

class data
{
	private:

		char(* dynamicarray)[20];
		int size1,i;

	public:

		data(char(*tmp)[20],int Size)
		{
			dynamicarray = new char(Size([20]);
			dynamicarray = tmp;
			size1 = Size;
		}
		get()
		{
			for(i=0;i<size1;i++)
			{
				cout<<dynamicarray[i];
			}
		}
	};


	main()


	{
		 char array1[5][20] ={"tom","dick"};
		 int Size1 =5;
		 data obj;
		 obj.data(array1,Size1);
		 data g;
		 obj.get();
		 return 0;


	}

Dani AI

Generated

The posted snippet has several separate problems that together produce compile errors and wrong behavior. The key faults are: invalid/new allocation syntax and then immediately overwriting that pointer (memory leak), missing function return types and a mismatched constructor call pattern, and use of obsolete headers. correctly flagged the classic allocate-then-reassign bug; was on the right track about allocation but did not show the allocation you need when you want several 20-byte rows.

Two practical fixes (pick one):

  1. Prefer modern C++ (recommended)
  • Store strings in std::vector<std::string> to avoid manual memory management and lifetime bugs. This removes the need for raw new/delete and makes copying and printing trivial.

Example:

#include <iostream>
#include <vector>
#include <string>

class Data {
    std::vector<std::string> rows;
public:
    Data(const std::vector<std::string>& src) : rows(src) {}
    void print() const {
        for (const auto &s : rows) std::cout << s << '\n';
    }
};
  1. If you must use fixed-size C strings
  • Use a pointer-to-array type and allocate the whole 2-D block at once (one new for N rows of 20 chars). Copy each C-string into the allocated rows and ensure null termination. Do not allocate and then immediately assign the pointer to another array — that leaks memory and is logically wrong.

Other practical notes

  • Give get() a proper return type (e.g., void get() const) and call the constructor correctly (create the object with parameters or provide a separate init member function). Creating an object with no matching constructor then trying to "call" the constructor like a regular function will not work.
  • Replace old headers like <iostream.h> and <conio.h> with standard <iostream>; compile with a modern standard (-std=c++11 or newer) and warnings (-Wall -Wextra) to catch issues early.
  • For reference on dynamic allocation and containers see cppreference: new expression (https://en.cppreference.com/w/cpp/language/new), std::vector (https://en.cppreference.com/w/cpp/container/vector), and std::string (https://en.cppreference.com/w/cpp/string/basic_string).

Recommended Answers

All 3 Replies

Honestly speaking it looks like an absolutely senseless text written in unknown language.
May be better you present your assignment?..

dynamicarray = new char(Size([20]);
dynamicarray = tmp;

There's a classic error. Why is it so?

your code :

dynamicarray = new char(Size([20]);

my code :

...
dynamicarray = new char(Size([20])); // <-- ???
//--or
dynamicarray = new char(Size(20));
//--or
dynamicarray = new char[20];
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.