wat is wrong in my code

P

Pradeep

Hi all,
I want to copy intermediate elements from a vector to another.
The problem is I am getting same value in m_Data[6] ...[11]for all
elements.(&srcPtr[0] value- for exp 205).
Am I missing anything?


std::vector<uint_8*> (m_Data);
uint8_t *srcPtr = new uint8_t[6];
memset(static_cast<uint8_t*>(srcPtr), 0,(sizeof
(uint8_t)* 6));
if(srcPtr)
{
srcPtr= // A API returning
const_uint*
}
m_Data[6] = &srcPtr0];
m_Data[7] = &srcPtrr[1];
m_Data[8] = &srcPtr[2];
m_Data[9] = &srcPtr[3];
m_Data[10]= &srcPtr[4];
m_Data[11]= &srcPtr[5];

Regards
Pradeep
 
S

SG


Hello, Pradeep. Are you related to smilesonisamal@gmail ?
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/3a4a17f01cff3068/
 I want to copy intermediate elements from a vector to another.
 The problem is I am getting same value in m_Data[6] ...[11]for
all elements. (&srcPtr[0] value- for exp 205).
 Am I missing anything?

   std::vector<uint_8*> (m_Data);

What kind of syntax is that?
Why don't you write

std::vector<uint_8*> m_Data;

instead? Note: You're creating an EMPTY vector.
    uint8_t *srcPtr = new uint8_t[6];
    memset(static_cast<uint8_t*>(srcPtr), 0,(sizeof (uint8_t)* 6));

Why the static_cast? It's useless here.
    if(srcPtr)
    {
   srcPtr= // A API returning const_uint*
    }

The expression "new uint8_t[6]" -- provided uint8_t is a type --
returns a non-zero pointer or throws an exception. So the boolean
expression in the if-statement will always be false. Assuming
"const_uint" is equivalent to "const uint8_t" you can't convert these
pointer types without a const_cast.
    m_Data[6] = &srcPtr[0];
    m_Data[7] = &srcPtr[1];
    m_Data[8] = &srcPtr[2];
    m_Data[9] = &srcPtr[3];
    m_Data[10]= &srcPtr[4];
    m_Data[11]= &srcPtr[5];

For this to work safely you need m_Data.size()>=12. But your vector
is empty!

Check the FAQ on how to post questions, how to post code, etc etc ...


Cheers!
SG
 
S

smilesonisamal


Hello, Pradeep.  Are you related to smilesonisamal@gmail ?http://groups..google.com/group/comp.lang.c++/browse_thread/thread/3a4...
 I want to copy intermediate elements from a vector to another.
 The problem is I am getting same value in m_Data[6] ...[11]for
 all elements. (&srcPtr[0] value- for exp 205).
 Am I missing anything?
    std::vector<uint_8*> (m_Data);

What kind of syntax is that?
Why don't you write

      std::vector<uint_8*> m_Data;

instead?  Note: You're creating an EMPTY vector.
    uint8_t *srcPtr = new uint8_t[6];
    memset(static_cast<uint8_t*>(srcPtr), 0,(sizeof (uint8_t)* 6));

Why the static_cast?  It's useless here.
    if(srcPtr)
    {
      srcPtr= // A API returning const_uint*
    }

The expression "new uint8_t[6]" -- provided uint8_t is a type --
returns a non-zero pointer or throws an exception.  So the boolean
expression in the if-statement will always be false.  Assuming
"const_uint" is equivalent to "const uint8_t" you can't convert these
pointer types without a const_cast.
    m_Data[6] = &srcPtr[0];
    m_Data[7] = &srcPtr[1];
    m_Data[8] = &srcPtr[2];
    m_Data[9] = &srcPtr[3];
    m_Data[10]= &srcPtr[4];
    m_Data[11]= &srcPtr[5];

For this to work safely you need m_Data.size()>=12.  But your vector
is empty!

Check the FAQ on how to post questions, how to post code, etc etc ...

Cheers!
SG

vector is not empty.
 
A

Alf P. Steinbach

* Pradeep:
Hi all,
I want to copy intermediate elements from a vector to another.
The problem is I am getting same value in m_Data[6] ...[11]for all
elements.(&srcPtr[0] value- for exp 205).
Am I missing anything?


std::vector<uint_8*> (m_Data);
uint8_t *srcPtr = new uint8_t[6];
memset(static_cast<uint8_t*>(srcPtr), 0,(sizeof
(uint8_t)* 6));
if(srcPtr)
{
srcPtr= // A API returning
const_uint*
}
m_Data[6] = &srcPtr0];
m_Data[7] = &srcPtrr[1];
m_Data[8] = &srcPtr[2];
m_Data[9] = &srcPtr[3];
m_Data[10]= &srcPtr[4];
m_Data[11]= &srcPtr[5];

Hi.

You have completely misunderstood pointers.

And also the "m_" prefix convention, and a lot of other things.

What you should do is just

std::vector<uint8_t> data;

But you need a good textbook, and you need to start at the start of that.

Sort of like Hillary Clinton's effort to "restart" relations with Russia instead
of digging deeper into existing hole, but don't use a button that doesn't work
and is labeled with insulting language; use a good textbook.


Cheers & hth.,

- Alf
 
A

Alf P. Steinbach

* (e-mail address removed):
vector is not empty.

Hi.

You have completely misunderstood the term "vector", plus just about everything
else.

You need a good textbook, and you need to start at the /start/ of that.


Cheers & hth.,

- Alf
 
P

paralysis

Hi Pradeep,
along with the others, I recommend consulting a text book.
*However*, here are a few comments which may serve as a guide.
std::vector<uint_8*> (m_Data);

As others have mentioned, you probably mean to write
"std::vector<uint_8> m_Data". This creates a vector of uint_8's and
this vector is named m_Data. The asterisk (*) after the uint_8 causes
the compiler to create a vector containing the addresses of uint_8's.
As a result, when you do the copying at the end (i.e., "m_Data[6] =
&srcPtr[0]") you are just copying the addresses of various substrings
of srcPtr. I think you want the actual octets. In that case, drop the
* in the vector definition, and drop the & in front of your srcPtr's.

Also, your vector _is_ empty. When you create a std::vector it is
initially empty *using the default constructor*. Before you try to
assign values to this vector you need to
* resize() it, or
* state how large you want the vector to initially be in the
constructor, or
* push_back() the values onto the end of the vector.

See here: http://www.cplusplus.com/reference/stl/vector/
uint8_t *srcPtr = new uint8_t[6];
memset(static_cast<uint8_t*>(srcPtr), 0,(sizeof(uint8_t)* 6));
if(srcPtr) { srcPtr= // A API returning const_uint* }

As noted above, the static_cast, and the "if(srcPtr)" are unnecessary.
The former because you are casting a pointer to the same type; this
literally does nothing. The second because C++ guarantees that srcPtr
exists at this point. Had "new" not worked, your program would have
terminated (it would have thrown an exception which, by default, would
cause your program to terminate since you aren't handling it. Don't
worry about that until you have the basics down, thouhgh...)

The assignment to srcPtr inside the "if" is troublesome. I'm assuming
that you think this copies the values of the const_uint* returned by
your API to the memory you just allocated. It doesn't. It copies the
pointer. Note that the pointer to the array you just allocated is now
lost. This will cause a memory leak 'cause you'll have no way to free
the 6 bytes you allocated by "new".

In fact, you're probably best not doing the "new" at all -- I don't
see the point of copying to the array pointed to by srcPtr, only to
copy it out right away to m_Data.

Probably all you need to write for those three lines is

const_uint* srcPtr = WhateverYourAPIIsCalled();
m_Data[6] = &srcPtr[0];
...

With the adjustments above, you could just loop over the copy:

for (unsigned i = 0; i < 6; ++i) m_Data[6 + i] = srcPtr;

Finally, you probably don't need to use "new" for six bytes. I sense
that you wanted to use srcPtr only temporarily. In which case you
might as well allocate a measly six bytes on the stack, i.e., "uint_8
src[6]". Having to use "new" and then "delete[]" later just adds
complication for a small, local variable.

--Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top