hi there, i've got a problem:
can i create a dynamic array like this?
pMap = new char[mapX][mapY];
gcc (my compiler) sais, it's wrong...
i know that dynamic memory is more often used in this way:
pMap = new char[mapX];
but i need a second element...
can somebody help? 7 1570
"Andreas Lassmann" <bu***********@ hotmail.com> wrote in message
news:pa******** *************** *****@hotmail.c om... hi there, i've got a problem: can i create a dynamic array like this?
pMap = new char[mapX][mapY];
gcc (my compiler) sais, it's wrong... i know that dynamic memory is more often used in this way:
pMap = new char[mapX];
but i need a second element... can somebody help?
One way is to do this:
char** pMap = new char*[mapY]; // does char* need () around it?
for (int i = 0; i < mapY; ++i)
pMap[i] = new char[mapX];
and later,
for (int i = 0; i < mapY; ++i)
delete [] pMap[i];
delete [] pMap;
-Howard
Howard wrote: "Andreas Lassmann" <bu***********@ hotmail.com> wrote in message news:pa******** *************** *****@hotmail.c om...
hi there, i've got a problem: can i create a dynamic array like this?
pMap = new char[mapX][mapY];
gcc (my compiler) sais, it's wrong... i know that dynamic memory is more often used in this way:
pMap = new char[mapX];
but i need a second element... can somebody help?
One way is to do this: [...]
Two FAQs are related to this thread, I believe: #5.5 and #16.15.
V
Howard wrote: "Andreas Lassmann" <bu***********@ hotmail.com> wrote in message news:pa******** *************** *****@hotmail.c om...
hi there, i've got a problem: can i create a dynamic array like this?
pMap = new char[mapX][mapY];
gcc (my compiler) sais, it's wrong... i know that dynamic memory is more often used in this way:
pMap = new char[mapX];
but i need a second element... can somebody help?
One way is to do this:
char** pMap = new char*[mapY]; // does char* need () around it? for (int i = 0; i < mapY; ++i) pMap[i] = new char[mapX];
and later,
for (int i = 0; i < mapY; ++i) delete [] pMap[i]; delete [] pMap;
-Howard
But your memory isn't contiguous.
I'd allocate th rows as a big block:
char *pMapData = new char[mapX * mapY]
char **pMap = new char[mapY];
for (int = 0 ; i < mapY; ++i)
pMap[i] = pMapData + (i * mapX);
Then you only have 2 delete[] calls, and the memory for the 2D array is
contiguous.
Or possibly, you could use a pointer to an array.
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:OV******** **********@news read1.mlpsca01. us.to.verio.net ... Howard wrote:
One way is to do this: [...]
Two FAQs are related to this thread, I believe: #5.5 and #16.15.
Okay, okay, I know... I answered a question that's already in the FAQ (and
didn't answer it as well or as completely either, I might add). But just to
get my own stab back at you, Victor, this is from #5.5:
Note #1: Please don't give them the location of the appropriate FAQ. E.g.,
don't say, "Look at FAQ [10.3]" or "Look in section [10]". It's the old
give-them-a-fish vs. teach-them-to-fish problem.
Neener neener! :-) (Sorry, I just couldn't resist)
-Howard
"red floyd" <no*****@here.d ude> wrote in message
news:h0******** **********@news svr13.news.prod igy.com... can i create a dynamic array like this?
pMap = new char[mapX][mapY]; One way is to do this:
char** pMap = new char*[mapY]; // does char* need () around it? for (int i = 0; i < mapY; ++i) pMap[i] = new char[mapX];
and later,
for (int i = 0; i < mapY; ++i) delete [] pMap[i]; delete [] pMap;
-Howard
But your memory isn't contiguous.
Nope. But that wasn't a specified requirement. I was just giving one way
to do it, not neccessarily the best. I'd allocate th rows as a big block:
char *pMapData = new char[mapX * mapY] char **pMap = new char[mapY]; for (int = 0 ; i < mapY; ++i) pMap[i] = pMapData + (i * mapX);
Then you only have 2 delete[] calls, and the memory for the 2D array is contiguous.
Yep, something like that's probably better. Or possibly, you could use a pointer to an array.
Or a vector!
Anyway, the FAQ explains the whole thing quite well. And, for the OP,
that's available at: http://www.parashift.com/c++-faq-lite/
-Howard
"red floyd" <no*****@here.d ude> wrote in message
news:h0******** **********@news svr13.news.prod igy.com... Howard wrote: "Andreas Lassmann" <bu***********@ hotmail.com> wrote in message news:pa******** *************** *****@hotmail.c om...
hi there, i've got a problem: can i create a dynamic array like this?
pMap = new char[mapX][mapY];
[snip]
I'd allocate th rows as a big block:
char *pMapData = new char[mapX * mapY] char **pMap = new char[mapY]; for (int = 0 ; i < mapY; ++i) pMap[i] = pMapData + (i * mapX);
Then you only have 2 delete[] calls, and the memory for the 2D array is contiguous.
Or possibly, you could use a pointer to an array.
I think better than what everyone else advised so far,
would be to use vector<vector<c har> > or perhaps
vector<string>
Don't muck around with raw pointers unless you must.
$.02,
-Mike This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: soni29 |
last post by:
hi,
i'm reading a c++ book and noticed that the author seems to allocate
memory differently when using classes, he writes:
(assuming a class called CBox exists, with member function size()):
// first way
CBox x;
x.size();
// second way
|
by: Jakob Bieling |
last post by:
Hi,
Whenever allocating memory using operator new or operator new I feel
like I should only use it very sparingly, because otherwise memory is wasted
(by additional overhead needed to manage all those allocations) which also
loses some performance. I am specifically talking about many little
allocations (approx. 16-512 bytes). Is my feeling about this justified?
thanks!
--
|
by: fivelitermustang |
last post by:
Actually, how would I go about allocating a four-dimensional dynamic
array?
I only know how to make two dimensional dynamic arrays:
double **v;
v = new double*;
for (int i=0; i<C; i++)
{
v = new double;
}
|
by: Sameer |
last post by:
Hello Group,
This is one problem in programming that is troubling me.
there is a segmentation fault just before creating memory to
a structure ..i.e, just after the "allocating memory " statement.
This happens for some inputs and not all.
What can be the reason for such fault ?
|
by: fix |
last post by:
Hi all,
I am writing a program using some structs, it is not running and I
believe it is because there's some memory leak - the debugger tells me
that the code causes the problem is in the malloc function.
Is there any general rules that tell me when to allocate memory?
I thought I don't have to if it is a variable that's not a pointer, and
I have to if it is.
I am a bit confused about the arrays particularly.
In normal situations,...
| |
by: smnoff |
last post by:
I have searched the internet for malloc and dynamic malloc; however, I still
don't know or readily see what is general way to allocate memory to char *
variable that I want to assign the substring that I found inside of a
string.
Any ideas?
|
by: Amit_Basnak |
last post by:
Dear Friends
I have the follwoing function "tss_fe_get_own_info" which has the
arguments as shows below
tss_fe_get_own_info(char *user_id, tss_user_profile_t
**p_buf_UserSecInfo, error_status_t *retStatus)
// Structure tss_user_profile_t from the function
typedef struct {
|
by: filia&sofia |
last post by:
For example I would like to dynamically allocate memory for linked
list (or complex struct etc.) that has not maximum number of elements.
All that I can remember is that I have to have allocated variable to
point to the linked list in main program and that I have to have some
kind of a method that initializes the list (plus other methods to
manipulate list, of course). "Correctly" means that there should be
pointers involved, and possibly...
|
by: arashmath |
last post by:
Hi everyone,
I have a problem with large dynamic memory allocating. this is my code (in briefness):
#include <stdio.h>
#include <mem.h>
#include <assert.h>
#define SML_BOUNDS_CHECK
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |