Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Class pointers help please

Posted on 2003-11-30
7
Medium Priority
?
279 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 220 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 15

Assisted Solution

by:efn
efn earned 80 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 220 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

782 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