473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

dynamic allocation

8
Hi

I am trying to create matrix of a struct I defined. It seems i cannot just sub in my struct name for float:

Expand|Select|Wrap|Line Numbers
  1. float **matrix;
  2. int rows = 10, cols = 10;
  3. matrix = new float* [rows];
  4. for(int i = 0; i<rows; i++)
  5. {
  6.   matrix [i] = new float [cols];
  7. }
What can I do instead?
May 20 '07 #1
5 1855
weaknessforcats
9,208 Expert Mod 8TB
What are you trying to do?? I defined a struct and plugged it in for float:
Expand|Select|Wrap|Line Numbers
  1. struct hello {};
  2. hello **matrix;
  3. int rows = 10, cols = 10;
  4. matrix = new hello* [rows];
  5. for(int i = 0; i<rows; i++)
  6. {
  7.     matrix [i] = new hello [cols];
  8. }
  9.  
and it compiles fine.

If you are trying to allocate a 2D array of your struct, you do it this way:

Expand|Select|Wrap|Line Numbers
  1.  
  2. hello      (*matrix)[10] = new hello[10][10];
  3.  
In this second case matrix is a pointer to an array of 10 hello (your struct).

Remember, C++ really doesn't have 2D arrays. It has one-dimensional arrays. The value between the first pair of brackets is the number of elements in the array. Values between other brackets are just defining the element type.

Expand|Select|Wrap|Line Numbers
  1. char arr[10];           //arr has 10 elements. Each element is a char
  2. char arr1[10][10];  //arr1 has 10 elements. Each element is an array of 10 char.
  3.  
So, in C++ the name of the array is the address of element 0. That means the name "arr" is the address of element 0 making "arr" a char*. You can allocate this array by:

Expand|Select|Wrap|Line Numbers
  1. char* arr = new char[10];
  2.  
Continuing, "arr1" is the address of element 0, which is an array of 10 char.
That makes "arr1" the address of an array of 10 char. You can allocate this array but you have to declare a pointer to an array of 10 char for the address returned by new:

Expand|Select|Wrap|Line Numbers
  1. char      (*arr1)[10] = new char[10][10];
  2.  
May 20 '07 #2
BEEMZA
8
What are you trying to do?? I defined a struct and plugged it in for float:
Expand|Select|Wrap|Line Numbers
  1. struct hello {};
  2. hello **matrix;
  3. int rows = 10, cols = 10;
  4. matrix = new hello* [rows];
  5. for(int i = 0; i<rows; i++)
  6. {
  7.     matrix [i] = new hello [cols];
  8. }
  9.  
[/code]
This worked fine until I added variables to the struct. The erro is as follows:

main.cpp:46: error: no matching function for call to ‘VEdge::VEdge()’
VEdge.h:6: note: candidates are: VEdge::VEdge(int, int, bool)
VEdge.h:1: note: VEdge::VEdge(const VEdge&)

I think it wants a constructor call of some sort
May 20 '07 #3
AdrianH
1,251 Expert 1GB
Continuing, "arr1" is the address of element 0, which is an array of 10 char.
That makes "arr1" the address of an array of 10 char. You can allocate this array but you have to declare a pointer to an array of 10 char for the address returned by new:

Expand|Select|Wrap|Line Numbers
  1. char      (*arr1)[10] = new char[10][10];
  2.  
This is new to me. I tried to do something like that a while ago and it didn't work. Cool.


Adrian
May 20 '07 #4
Ganon11
3,652 Expert 2GB
This worked fine until I added variables to the struct. The erro is as follows:

main.cpp:46: error: no matching function for call to ‘VEdge::VEdge()’
VEdge.h:6: note: candidates are: VEdge::VEdge(int, int, bool)
VEdge.h:1: note: VEdge::VEdge(const VEdge&)

I think it wants a constructor call of some sort
I think it wants a constructor, also. When you are creating each array of X VEdges, the compiler tries to call the default constructor so you have X copies of the same basic VEdge. If you have provided other constructors (but not a default constructor), this will create a problem. Include a default constructor and see if the problem persists.
May 20 '07 #5
BEEMZA
8
Thanks, it works with the default constructor :)
May 21 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Tom | last post by:
What I mean is why can I only allocate const size stuff on the stack in C++? If I want to allocate a variable amount I need to use the OS API (Win32 in my case). Thanks, Tom.
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
7
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
3
by: ranjeetasharma81 | last post by:
Hi all, I have a big C-cod, in which there are lots of dynamic memory allocation used. I want to replace dynamic memroy allocation by static arrays. The following are the problems that i am...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
10
by: swornavidhya.mahadevan | last post by:
Which allocation (Static / Dynamic) is suitable for the situation when we are trying to allocate for a overloaded memory when the memory is full and no space to allocate. What will happen if both...
0
by: Kelly Bert Manning | last post by:
I didn't get any hits when I searched deja for DSN1COPY dynamic allocation so either everyone else had no trouble realizing that this happens or nobody else has ever come across it. I've used...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.