Solved

Class pointers help please

Posted on 2003-11-30
7
275 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

830 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