I have written a class which allocates an array of unsigned chars using new, but when I create multiple instances of this class, they all point to the same block of memory. Any idea what I'm doing wrong here? - //the relevant code in the constructor
-
//v is an unsigned char*
-
//r and c are integers
-
//init is an unsigned char
-
this->v = new unsigned char[r*c];
-
memset(v,init,r*c);
-
- //the code which creates the objects
-
//water,hills and active are all an uninitialised FieldMap
-
//gs.rows and gs.cols are integers
-
this->water = FieldMap(gs.rows,gs.cols,255);
-
this->hills = FieldMap(gs.rows,gs.cols,128);
-
this->active = FieldMap(gs.rows,gs.cols,128);
-
2 2101
Fixed it. Stuff like this is what you get when you mix some Java in your C++.
The change, for those interested: - //the code which creates the objects
-
//water,hills and active are all an uninitialised FieldMap*
-
//gs.rows and gs.cols are integers
-
this->water = new FieldMap(gs.rows,gs.cols,255);
-
this->hills = new FieldMap(gs.rows,gs.cols,128);
-
this->active = new FieldMap(gs.rows,gs.cols,128);
-
Just something I would note:
why aren't you typing the first new -
//the relevant code in the constructor
//v is an unsigned char*
//r and c are integers
//init is an unsigned char
this->v = new unsigned char[r*c];
memset(v,init,r*c);
instead:
typedef unsigned char* myOddType;
main()
.
.
.
this->v = (myOddType) new myOddType[r*c];
memset(v,init,r*c);
Sign in to post your reply or Sign up for a free account.
Similar topics
by: seesaw |
last post by:
class A
{
public:
static A* newA() { return new A; }
....
};
In the code, two things not very clear and natural to me:
1. the method newA() is defined as static.
2. newA as a member method...
|
by: seesaw |
last post by:
Is it right thing to always avoid using "new" to create objects? What if
after starting the application, then decide which and how many objects to
create? (Seems like under such situation is there...
|
by: Leslaw Bieniasz |
last post by:
Cracow, 28.10.2004
Hello,
I have a program that intensively allocates and deletes lots
of relatively small objects, using "new" operator. The objects
themselves are composed of smaller...
|
by: Rv5 |
last post by:
Rookie c++ question, but Ive spent the last 5 years doing Java, where
everytime I created an object I used new. In c++ I can create my objects
without and its confusing me just a little.
I have...
|
by: Dot net work |
last post by:
I need VB.NET's "shadows" functionality inside a C# project.
I tried the "new" keyword, but it didn't seem to work, because my
particular function does in fact differ in signature to the function...
|
by: Ben R. |
last post by:
I'm curious about the differeng behavior of the "new" keyword when dealing
with value versus object types. If I'm correct, when I do:
dim x as integer
There's no need for "new" to be called...
|
by: Pat |
last post by:
Hi,
I've run into a strange problem, but one that seems like it might be
fairly common.
I have a single base class, from which several other classes are derived.
To keep the example simple,...
|
by: Asm23 |
last post by:
Hi
i'm using intel P4.
when I write the statement like
char *p=new char
what's the p value, is it aligned by 4 bytes? that's is p%4==0?
and, now I'm using the Intel SSE2 instruction to do...
|
by: Lloyd Bonafide |
last post by:
I followed a link to James Kanze's web site in another thread and was
surprised to read this comment by a link to a GC:
"I can't imagine writing C++ without it"
How many of you c.l.c++'ers use...
|
by: Medvedev |
last post by:
i see serveral source codes , and i found they almost only use "new"
and "delete" keywords to make they object.
Why should i do that , and as i know the object is going to be destroy
by itself at...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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,...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
|
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,...
| |