• Advertisement

Archived

This topic is now archived and is closed to further replies.

What is wrong with my char* pointer?

This topic is 5626 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a function with this header:
bool cWinSock::Receive(char *Data, int &iBytesRecv)
 
When I return from the function I use this code:
  
iBytesRecv = Vector.size();
Data = Message;
return (true);
  
Message is a char* aswell, located inside my class. It gets deleted before (re)using and in the destructor. However, if I delete from my function the pointer to wich Data is pointer changes and I cannot access the memory again without crashes. What am I doing wrong? Below is the entire function for people who would like to see all code:
  
bool cWinSock::Receive(char *Data, int &iBytesRecv)
{
    vector<char> Vector;
    char         szBuffer[1] = {''\0''};
    int          iError = 0;

    // Is the socket connected?

    if (!bConnected)
        return (false);

    // As long there is data waiting on the socket

    while (IsData() == SOCK_PENDING)
    {
        // Get 1 character of the socket

        iError = recv(Socket, szBuffer, 1, 0);

        // Check if there is a 

        if (iError <= 0)
        {
            iBytesRecv = 0;
            Data       = NULL;
            return (false);
        }

        // Push the character in the vector

        Vector.push_back(szBuffer[0]);
    }

    // Is the message buffer allocated? Delete it if it''s allocated

    if (Message != NULL)
    {
        delete [] Message;
        Message = NULL;
    }

    // Allocate memory for the buffer

    Message = new char[Vector.size() + 1];

    // Cope the entire vector into the buffer

    for (int iIndex = 0; iIndex < Vector.size(); iIndex++)
        Message[iIndex] = Vector[iIndex];

    // Terminate the message

    Message[Vector.size() + 1] = ''\0'';
    
    // Return the data

    iBytesRecv = Vector.size();
    Data = &Message;
    return (true);
}
  
Sand Hawk ---------------- -Earth is 98% full. Please delete anybody you can.
My Site

Share this post


Link to post
Share on other sites
Advertisement
you pass the pointer by value. you can''t manipulate the pointer, just the value it points to.

char*& Data is what you probably want..

"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites
>
1) pointer to wich Data is pointer
2) Receive(char *Data
3) Data = &Message;
<

My understanding of what you''re trying to do: after Receive(), the caller is told the address of the buffer and its size. What''s happening now is that you''re assigning a value to Data, which is thrown away when the function returns (it''s passed by value).
What you need to do is declare Data to be a (reference|pointer) to a pointer to a char:
1): that makes Data a [type]**
change 2) to Receive(char** Data
replace 3) with if(Data) *Data = Message; /* array name gives you address of name */
Calling code would look like this:
int size; /* stuff we get from Receive() */
char* buf;
Receive(&buf, &size);

Tips: read up on calling conventions, parameter types, and pointers/references.
Also, what''s wrong with recv?

HTH
Jan

Share this post


Link to post
Share on other sites
Or.. char ** works to. You want the address of the variable that''s holding the pointer, not the location that it points to.


void SetData(char **Data)
{
char *Memory = new char[10];
*Data = Memory;
}

I beleive will do what you''re looking to do, unless I missed something.

void SetData(char *&Data)
{
char *Memory = new char[10];
Data = Memory;
}

This should also give you your desired results.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Think of this way:

"char *" is just a number (memory address)

so you are saying:

bool func (MemoryAddress Data)
{
Data = some value;
}

thus you have changed Data, but since
you passed by value, Data is created
on the stack and is defined only in the
scope of the function.

Share this post


Link to post
Share on other sites

  • Advertisement