Question:
Problem with Object Array in C++?
Aditya Joshi
2010-02-15 15:03:54 UTC
Hey you geeks out there; this is a part of larger program I'm stuck up with. Here I create a very simple class codes and make its object array c.

class codes
{
public:
char *opcode, *mcode, *clas;
int len;

void get(char *a, char *b, char *c, int d)
{
strcpy(opcode,a);
strcpy(mcode,b);
strcpy(clas, c);
len=d;
}
void put()
{
cout<<" "< }
};

// Well now I create an array of objects
codes c[12]; // I suppose there's no need of 'new' here. It wont work either.

// And now comes the next part
c[0].get("MOVR", "01", "IS", 1);
c[1].get("MOVM", "02", "IS", 1);
c[2].get("ADD", "03", "IS", 1);
c[3].get("MUL", "04", "IS", 1);
c[4].get("SUB", "05", "IS", 1);
c[5].get("DIV", "06", "IS", 1);
// And so on.....

Now when I run it, the values are not loaded properly. Check out your debugger watch window for things like c[3].opcode ... Where am I wrong?

Please reply asap. Get ready for the five points.
Four answers:
Mantis
2010-02-15 15:46:33 UTC
The problem is that you are trying to copy memory to an uninitialized pointer. Let me show a few lines of code that will show the problem more clearely.



char * mystring; //just a pointer... could be pointing anywhere

strcpy(mystring, "Hello");



The problem here is that mystring is just a pointer. You have several of these strings in your class, but they are only pointers. So you're trying to copy data to a pointer that could be pointing anywhere. Frankly, I'm surprised the program doesn't crash.



You have two ways to fix this problem:



1. Convert the variables from char* to char arrays. Probably your best option

2. Allocate the char * arrays with new (opcode = new char[10]) in a constructor and delete[] in a destructor



Good luck.
Cubbi
2010-02-15 16:28:47 UTC
Why not skip the whole pointer business and use C++ strings?



#include

#include

using namespace std;

class codes

{

string opcode, mcode, clas;

int len;

public:

explicit codes(const string& a, const string& b, const string& c, int d)

: opcode(a), mcode(b), clas(c), len(d)

{ }

void put() const

{

cout<<" "<
}

};

int main()

{

vector c;

c.push_back(codes("MOVR", "01", "IS", 1));

c.push_back(codes("MOVM", "02", "IS", 1));



c[0].put();

c[1].put();

}



$ g++ -Wall -o test test.cc

$ ./test

MOVR 01 IS 1

MOVM 02 IS 1



Not to mention that if you plan to look up data in your array, it makes a lot more sense to make it a map or even a bimap, depending on what kind of look ups are you planning.



Also, is that 2nd field always integer? Why do you want it to be a string? Is it always incrementing and unique too? If so, you might want to keep it a vector, but use mcode as index.



(El Gordo: we have a difference in opinion about what constitutes "learning". Understanding that these choices of member type, initialization method, and container type are poorly suitable for the task would teach more than learning to place unjustified calls to cstdlib's malloc in C++ code, in my opinion)
El Gordo
2010-02-15 16:58:21 UTC
Before you copy your string to the char array you need to alloc some memory

like this:



opcode = malloc(sizeof(a));

strcpy(opcode,a);



mcode = malloc(sizeof(b));

strcpy(mcode,b);



mcode = malloc(sizeof(c));

strcpy(clas, c);



I'm so disappointed that all of the other answerers chose to criticize you instead of just explaining what was wrong. I can only surmise that they didn't really know.



Yes, I know that the string class should be used here. But the point is to TEACH and to LEARN, not to FLAME to feed your EGOS.



Hope this helps.
?
2010-02-15 15:31:55 UTC
You need a constructor for Code() and new.



so it would look like



class Code

{

Code()

{

;// initialise code here

}

}



and in main you would do...



Code *code = new Code[10]();



or something like that. Lol im tired and Im not 100% sure on the initialisation.



Worst case do a



Code code[10];



for loop 0->i

code[i] = *(new Code())

end loop



anyways, hope i was at least of a little help lol,

peace


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...