473,396 Members | 1,775 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.

"new" always returns the same pointer

YarrOfDoom
1,247 Expert 1GB
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?
Expand|Select|Wrap|Line Numbers
  1. //the relevant code in the constructor
  2. //v is an unsigned char*
  3. //r and c are integers
  4. //init is an unsigned char
  5. this->v = new unsigned char[r*c];
  6. memset(v,init,r*c);
  7.  
Expand|Select|Wrap|Line Numbers
  1. //the code which creates the objects
  2. //water,hills and active are all an uninitialised FieldMap
  3. //gs.rows and gs.cols are integers
  4. this->water = FieldMap(gs.rows,gs.cols,255);
  5. this->hills = FieldMap(gs.rows,gs.cols,128);
  6. this->active = FieldMap(gs.rows,gs.cols,128);
  7.  
Nov 23 '11 #1
2 2101
YarrOfDoom
1,247 Expert 1GB
Fixed it. Stuff like this is what you get when you mix some Java in your C++.

The change, for those interested:
Expand|Select|Wrap|Line Numbers
  1. //the code which creates the objects
  2. //water,hills and active are all an uninitialised FieldMap*
  3. //gs.rows and gs.cols are integers
  4. this->water = new FieldMap(gs.rows,gs.cols,255);
  5. this->hills = new FieldMap(gs.rows,gs.cols,128);
  6. this->active = new FieldMap(gs.rows,gs.cols,128);
  7.  
Nov 23 '11 #2
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);
Nov 23 '11 #3

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

Similar topics

4
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...
30
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...
18
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...
24
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...
8
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...
4
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...
36
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,...
4
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...
350
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...
30
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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...
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.