void func(void * ptr)
{
          bytearray temp(10);
          *((bytearray*) ptr ) = temp; 
}

This code is not going ahead if the third line while executing while this coding is working fine.

void func(void * ptr)
{
        const char * temp = "decept";
        *((char*)ptr) = temp[0];
}

Why is it happening? First code is kind of "hanged" at the assigning line. It is not going ahead as Working on eclipse. Please tell how I can solve the problem and why it is happening. Thanks.

What's bytearray and how and where is it defined? Unless you posted this in the wrong forum and this is actually C++ (and bytearray is a class that you defined somewhere), I don't understand how the code even compiles (I suppose temp might be a macro, but that wouldn't make any sense).

bytearray is this:

typedef array<byte> bytearray;

and byte is this:

typedef uint8_t byte;

Now, please tell. "array" is just a class which show a array of something; basically a template for array of some type.

So you did post in the wrong forum. And array is your own class, right? Because std::array would require two template arguments.

In any case, assuming that your array class is correctly defined and all the constructors, the destructor and the assignment operator work as one would expexpt, the most likely explanation is that ptr does not point to a correctly initialized instance of the array class and thus calling the assignment operator on it invokes undefined behavior.

array is a pre-defined class in a library. What can I do for that? thanks.

Well, if the class is correctly defined (which one can assume if it's part of a library that you were given), I told you what the most likely problem is. That's really all I can do with the given information.

But the way I am doing is correct or not?

Assuming that the pointer points to a correctly initialized instance of type bytearray and the bytearray class is correctly assignable (creating an independent copy that will still be usable once the original instance goes out of scope), then yes, the part of your code that you've shown should work.

Given that it does not, at least one of those things is not true - probably the former.

It rather depends on what the function is supposed to do, the function you have written assumes that the calling code has cast a bytearray to a void pointer to pass into the function (a somewhat pointless exercise in C++) liek this

bytearry data;

function((void*)&data);

The bytearray create in func (your first one) is copied into the external bytearray. However the only really good reason to use void* in C++ is when calling old legacy C interfaces and that makes be wonder if you were trying to copy data out of a bytearray into a memory buffer which is then used in some mannor, like this

void* data = (void*)malloc(50);

function(data);

If this is what you are trying then what you have written wont necessarily work, it rather depends on the implementation of bytearray, whether it holds the actual data to an internal pointer to the data because in the second case your memory buffer would end up containing the pointer rather than the data.

The problem is that you have not explained what you are doing in very much detail so it is hard to work out what the correct answer is.

If this is what you are trying then what you have written wont necessarily work, it rather depends on the implementation of bytearray, whether it holds the actual data to an internal pointer to the data

Actually what it depends on is whether bytearray's operator = does anything that requires the constructor to have run previously - though technically, I believe, calling operator = on an unitialized object invokes undefined behavior even if that is not the case.

If the OP is indeed using malloc, the memory needs to be initialized using placement new before any members (including the assignment operator) can be used on it. Though preferably malloc shouldn't be used at all.

because in the second case your memory buffer would end up containing the pointer rather than the data.

That completely depends on what operator = does. If the OP's array class acts at all like a standard container class, assigning one array to another should make the assigned-to array have a pointer to newly allocated memory that contains a copy of the other array's contents.

The problem is that you have not explained what you are doing in very much detail so it is hard to work out what the correct answer is.

Indeed.

That completely depends on what operator = does. If the OP's array class acts at all like a standard container class, assigning one array to another should make the assigned-to array have a pointer to newly allocated memory that contains a copy of the other array's contents.

Yes but the point I was trying to make is that if what the OP is trying to do is copy the data out of the bytearray class into a raw memory buffer then

*((bytearray*) ptr ) = temp; 

is not going to do it, it will copy the contents of the class into the raw memory buffer, rightly as you point out with the assignment operator, and if the class contents is not actually the array data, because for example it uses a pointer to allocated memory, then what gets stored in the rawdata buffer is not the array contained by bytearray at all.

I went down this route because I see no other reason for using a void*, if you wanted to copy into a bytearray why not use bytearray*?

I don't know if my reasoning for why this is being done like this is correct but if it is then the line of code

*((bytearray*) ptr ) = temp; 

is ver unlikely to work even if the implementation of bytearray is completely correct for the very reason that the assignment operator is being used rather than specifically copying data out of bytearray and into a raw memory buffer.

If the implementation of array uses a pointer to dynamically allocated memory (which is pretty much a given unless there's some static maximum size for arrays) and the memory that ptr points to has been allocated using malloc without placement new, then calling operator = will very likely call delete on an uninitialized pointer and/or write to memory it does not own¹. That's why it won't work, not because the wrong thing is being copied.

if the class contents is not actually the array data, because for example it uses a pointer to allocated memory, then what gets stored in the rawdata buffer is not the array contained by bytearray at all.

What will or won't be copied is entirely determined by what operator = does, not by whether or not the implementation of array uses pointers.

¹ I'm assuming here that operator = is defined along the lines of the following with data and size being unitialized if malloc has been used without placement new:

array<T>& operator =(const array<T>& other) {
    if(size != other.size) {
        delete data;
        size = other.size;
        data = new T[size];
    }
    for(int i = 0; i < size; i++) {
        data[i] = other.data[i];
    }
    return *this;
}

What will or won't be copied is entirely determined by what operator = does, not by whether or not the implementation of array uses pointers.

Yes I agree with everything you've been saying given the assumption that what is being attempted is copying from a class instance to another class instance via a void*, but that is a very strange use for a void* when a bytearray* would work better.

So I am not sure that is what the OP is trying to do and I wonder if they are trying to copy data out of a class instance and into a byte array because of this strange use of a void*. In this case the whole pointer cast and assignment will not work regardless of class implementation because what is required is something like std::copy

std:vector<unsigned char> dataclass(10, 3);
unsigned char rawBuffer[10];

// Copy data in vector into raw memory buffer
std::copy(dataclass.begin(), dataclass.end() rawBuffer);

Which comes back to the issue that the actual problem has not been well explained, what is the purpose of "assigning" the class to a void pointer?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.