Solved

Class pointers help please

Posted on 2003-11-30
7
276 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

738 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