Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Cherez

Strange dynamic array returns

This topic is 5892 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m working on a class that allows me to store different types and lengths of array (by a char* and bitwise shifts). But everytime I set the variables they change. Here are the funxctons I use: void setint(unsigned int location, bool which, int set){ location *= 4; if (which) for (int place = 4; place > -1; --place) data[location + place - 4] = (char)(set>>8*place);} int getint(unsigned int location, bool which){ location *= 4; int ear = 0; if (which) for (int place = 4; place > -1; --place) ear |= (char)(data[location + 4 - place] << (place * 8));} void sset(unsigned int size, unsigned int nlength, char ntype){ clear(1); data = new char[length * size]; length = nlength; type = ntype;} and data is a char* (these are all in an object called chobject). But the output is rather strange. int main(){ Chobject Joe(1); Joe.sset(3, 4, 1); Joe.setint(0, 1, 1); Joe.setint(1, 1, 1); Joe.setint(2, 1, 2); cout << Joe.getint(0, true) << "\n" << Joe.getint(1, true) << "\n"; reutrn 0; prints: 0 2237497 instead of 1 1 and int main(){ Chobject Joe(1); Joe.sset(3, 4, 1); Joe.setint(0, 1, 1); Joe.setint(1, 1, 1); Joe.setint(2, 1, 2); cout << Joe.getint(0, true) << "\n"; reutrn 0; prints 2237497. instead of 1. Any ideas of what I''m doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
Any of the following may either be a bug, a consequence of me misunderstanding the code or you not using excactly the same code as you posted

int getint(unsigned int location, bool which)
you don''t return anything, small wonder Joe.getint returns funny things


void sset(unsigned int size, unsigned int nlength, char ntype)
line 2 you use length, which is either uninitialized or even undefined. But since you assign a value to it afterwards I assume

data = new char[length * size];

should be

data = new char[nlength * size];



none of your funtions belong to a class. They should be e.g

void Chobject::getint(unsigned int location, bool which)



and couldn''t you replaced your storage loops

for (int place = 4; place > -1; --place)
data[location + place - 4] = (char)(set>>8*place);

by

int* casted = (int*)(data[location]);
*casted = set;



---------------------------
I may be getting older, but I refuse to grow up

Share this post


Link to post
Share on other sites
Thanks a lot to dreamfroger. Now it prints
3
2
I did msis the return on get int. I omitted most of the object to save on complexity. But I guess it will probably be helpful.

#include <deque>
#include <iostream>
using namespace std;
class Chobject{
bool suse;
bool duse;
unsigned int name;
char type;
unsigned int length;
char* data;
deque* ddata;
public:
Chobject(unsigned int nname){
name = nname;
suse = false;
duse = false;}

int clear(){
int deleted = 0;
if (suse){
delete data;
deleted += 1;}
if (duse){
delete ddata;
deleted += 2;}
return deleted;}

bool checksuse(){
return suse;}

bool checkduse(){
return duse;}

int clear(bool which){
if (which & suse){
delete data;
return 1;}
if ((!which) & duse){
delete ddata;
return 1;}
return 0;}

void sset(unsigned int size, unsigned int nlength, char ntype){
clear(1);
data = new char[nlength * size];
length = nlength;
type = ntype;}

void dset(unsigned int reserve){
ddata = new deque(reserve, 0);}

char checktype(){
return type;}

unsigned int checkname(){
return name;}

void setchar(unsigned int location, bool which, char set){
if (which)
data[location] = set;
else
(*ddata)[location] = set;}


char getchar(unsigned int location, bool which){
if (which)
return data[location];
else
return (*ddata)[location];}

void setint(unsigned int location, bool which, int set){
int* casted;
location *= 4;
if (which)
for (int place = 4; place > -1; --place)
data[location + place - 4] = (char)(set>>8*place);
else
for (int place = 4; place > -1; --place)
(*ddata)[location + place - 4] = (char)(set>>8*place);}

int getint(unsigned int location, bool which){
location *= 4;
int ear = 0;
if (which)
for (int place = 4; place > -1; --place)
ear |= (char)(data[location + 4 - place] << (place * 8));
else
for (int place = 4; place > -1; --place)
ear |= (char)((*ddata)[location + 4 - place] << (place * 8));
return ear;}

void setlong(unsigned int location, bool which, long set){
location *= 8;
if (which)
for (int place = 8; place > -1 ; --place)
data[location + place - 8] = (char)(set >> place * 8);
else
for (int place = 8; place > -1 ; --place)
(*ddata)[location + place - 8] = (char)(set >> place * 8);}

long getlong(unsigned int location, bool which){
location *= 8;
long ear = 0;
if(which)
for (int place = 8; place > -1; --place)
ear |= (char)(data[location + 8 - place] << (place * 8));
else
for (int place = 8; place > -1; --place)
ear |= (char)((*ddata)[location + 8 - place] << (place * 8));
return ear;}

~Chobject(){
clear();}};

int main(){
Chobject Joe(1);
Joe.sset(3, 4, 1);
Joe.setint(0, 1, 1);
Joe.setint(1, 1, 1);
Joe.setint(2, 1, 2);
cout << Joe.getint(0, true) << "\n" << Joe.getint(1, true) << "\n";
return 0;}

When I tried the method of storage you suggested I got a crash. Thanks for finding the return error too.

Thanks for the help so far.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!