Solved

Class pointers help please

Posted on 2003-11-30
7
273 Views
Last Modified: 2010-05-18
Hi all,
Sorry if this is a newb question! I am converting C source into an easier, more secure and reliable Class object.

I am getting an error where my pointers, although I am setting them, are being set to 0 inside the class. Heres the declaration:

class myclass
{
public:
  CHAR other_stuff;
  char* data;
 
  // now for some functions...
  // <insert standard co/deconstructors here>

 // void AddData(const char* add_data,int len);
};

// OVerload the function
void myclass::AddData(const char* add_data,int len)
{
      int pos = 0;

      if(data == NULL){
            data = new char[len];
            length = htons(len);
            memcpy((char*)data,(const char*)add_data,len);
      }
      else{
            delete data;
            pos = ntohs(length);
            length = htons(pos+len);
            data = new char[pos+len];
            memcpy((char*)data+pos,(const char*)add_data,len);}

}

Now when I call the thing later on like this (or when i check the value of char* data in the debugger) it displays 0x00000000!

myclass mine;
mine.AddData("test",4); // dont need null-char
cout << mine.data[0] << "\n";

Any thoughts?
0
Comment
Question by:nullmind
  • 4
  • 2
7 Comments
 
LVL 1

Author Comment

by:nullmind
ID: 9846984
PS, that is being called withing another Class function (the part about accessing data).

If I add the access part outside of a class function, it works. but inside it doesnt work.
0
 
LVL 6

Assisted Solution

by:PlanetCpp
PlanetCpp earned 55 total points
ID: 9847269
are you actually setting data to NULL before you do anything else with it?
you would need to do that in the constructor and also after you use delete on any allocated memory for it.
when you say
char *data;
the variable data will have some value in it, just some junk value. whatever was in that memory block from before. so when you put if(data==NULL) it wont work, unless you got really lucky and that junk value was zero'd out data.
in your constructor set data to NULL
and also after delete is used cause it wont clear the address from it.
0
 
LVL 1

Author Comment

by:nullmind
ID: 9847302
I am setting the value to NULL inside my constructor (overloaded). I also delete and set it to NULL afterwards.

The access violation happens when I do this inside a class function:

cout << data[0];

for example, thought I've added more data than that into data. If I just do this:

cout << data;

it prints out the string I've jammed into that buffer! Why? I need to access the data byte by byte.. somethings WRONG here on my part!

CHeers
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Assisted Solution

by:efn
efn earned 20 total points
ID: 9847306
After I fixed up your code so it compiled, it seemed to work.  So I have mainly questions.

PlanetCpp's question of whether data is initialized to NULL is a good one.

Is length a member?

Are you saying the pointer is zero or the data to which it points is zero?

Do you realize that when AddData is called and data is not NULL, it trashes whatever data the object used to have?  It looks like it is trying to append the data, but the implementation doesn't work.

I'd suggest you try to post some code that is minimal, demonstrates the problem, and compiles.

--efn
0
 
LVL 1

Author Comment

by:nullmind
ID: 9847353
OMG im so sorry everyone. Yes, I'm an idiot. Here is my stupid mistake for anyone else who cares:

      if(data == NULL){
            data = new char[len];
            length = htons(len);
            memcpy((char*)data,(const char*)add_data,len);

            return;
      }
<---- COMMENTED OUT NOW --->
      /*
      else{
            delete data;
            pos = ntohs(length);
            length = htons(pos+len);
            data = new char[pos+len];
            memcpy((char*)data+pos,(const char*)add_data,len);}
            */

see? It was creating the new buffer, then going to the NEXT part because it exists now. I added a return at the end of that now.
0
 
LVL 6

Accepted Solution

by:
PlanetCpp earned 55 total points
ID: 9847357
does this help? i just wrote a quick class that kinda does what youre doing and it works fine. excuse the coding style its a rush job i put the functions in the class declaration
#include <iostream>
using std::cout;
using std::endl;

class tst
{
public:
      tst(void){data=NULL;};
      ~tst(void){delete[] data;data=NULL;};
      void setdata(char *newdata)
      {
            if(data==NULL)
            {
                  data=new char[strlen(newdata)+1];
                  strcpy(data,newdata);
            }
            else
            {
                  delete[] data;
                  data=NULL;
                  setdata(newdata);
            }
      }
      void displaydata(void){cout<<data[0]<<endl;};
      char *data;
};
int main()
{

      tst t;
      t.setdata("test");
      t.displaydata();      //just the first character
      t.setdata("blah blah blah");
      t.displaydata();
      return 0;
}
works fine.
0
 
LVL 1

Author Comment

by:nullmind
ID: 9847411
The above post was incorrect, i am EVEN more stupid! A classic habit of doing without EVEN thinking about it on rare occasion:

if(data = NULL)

it was hidden away in a complex || && statement I made. I'm a moron.

Sorry! Points to all!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now