Solved

Class pointers help please

Posted on 2003-11-30
7
272 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

707 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

18 Experts available now in Live!

Get 1:1 Help Now