Assign wrong memory address

I

Immortal Nephi

The member function has one or two parameters. First parameter is
reference to array and second parameter may not be needed or assign
array size.

For example to use reference

void Foo::read( const char arrayName[], int arraySize )
{
int i = 0;
do
{
cout << arrayName[ i++ ];
}
while ( i != arraySize );

cout << endl;
}

Sometimes, programmer prefers to use pointer instead because they
want to test null terminator.

For example to use pointer

void Foo::read( const char* arrayName )
{
do
{
cout << *arrayName;
arrayName++;
}
while ( *arrayName != ‘\0’ );

cout << endl;
}

I prefer to test arraySize rather than null terminator because I
don’t trust null terminator. Sometimes, null terminator may be
inserted in the middle of the arrayName. The loop allows to ignore
reading null terminator and checks for arraySize before loop is
terminated.
If you have 16 elements in an arrayName, it is your responsibility to
provide arraySize or null terminator. The operating system will
trigger violation security denied if more elements are read or walked
out outside the arrayName range.
The arrayName is often located in the file scope like global variable
and also it is often located in the function body like local
variable. After you instantiate a class, you will assign local
variable – arrayName and arraySize to the member function read().
Then, member function read() does its own algorithm to manipulate
arrayName in memory directly with the help of arraySize information.
Do you have the solution if you provide wrong array size information?
 
J

Jonathan Lee

Do you have the solution if you provide wrong array size
information?

Use a container like std::vector. Don't pass arrays this
way.

As an aside, this has got to be the 9th or 10th post of yours
where the most obvious answer is "use std::vector".

--Jonathan
 
M

mike

Use a container like std::vector. Don't pass arrays this
way.

As an aside, this has got to be the 9th or 10th post of yours
where the most obvious answer is "use std::vector".

--Jonathan

blah
 
B

Balog Pal

void Foo::read( const char arrayName[], int arraySize )
{
int i = 0;
do
{
cout << arrayName[ i++ ];
}
while ( i != arraySize );
cout << endl;
}
<<

This is hardly a read function and will do wild things if 0 is passed as
size.
Sometimes, programmer prefers to use pointer instead because they
want to test null terminator.

Yes, in C for char arrays it is common to have space for X characters but
store less than that amount, and indicate end with 0 terminator. as size of
the array is fixed at compile time, and the content is so often created
in-place from fragments or read from outside. Size of the array object is
only good to avoid buffer overrun, not to represent the actual length.
If you have 16 elements in an arrayName, it is your responsibility to
provide arraySize or null terminator. The operating system will
trigger violation security denied if more elements are read or walked
out outside the arrayName range.

You just think so. If you do that the behavior is undefined. And in practice
you can see stuff like crash, corrupt data -- or the "execute arbitrary code
from remote" attack on the system...
Do you have the solution if you provide wrong array size information?

As others suggested, forget C-style ararys and use std::vector in general,
and std::string to hold and manipulate string... They know the size
properly and have many other useful features.
 
I

Immortal Nephi

"Immortal Nephi" <[email protected]>



void Foo::read( const char arrayName[], int arraySize )
{
 int i = 0;
 do
 {
 cout << arrayName[ i++ ];
 }
 while ( i != arraySize );
cout << endl;}

<<

This is hardly a read function and will do wild things if 0 is passed as
size.
Sometimes, programmer prefers to use pointer instead because they
want to test null terminator.

Yes, in C for char arrays it is common to have space for X characters but
store less than that amount, and indicate end with 0 terminator. as size of
the array is fixed at compile time, and the content is so often created
in-place from fragments or read from outside. Size of the array object is
only good to avoid buffer overrun, not to represent the actual length.
If you have 16 elements in an arrayName, it is your responsibility to
provide arraySize or null terminator.  The operating system will
trigger violation security denied if more elements are read or walked
out outside the arrayName range.

You just think so. If you do that the behavior is undefined. And in practice
you can see stuff like crash, corrupt data -- or the "execute arbitrary code
from remote" attack on the system...
Do you have the solution if you provide wrong array size information?

As others suggested, forget C-style ararys and use std::vector in general,
and std::string to hold and manipulate string...  They know the size
properly and have many other useful features.

I agree that you suggest either vector or string. I always use string
for ASCII and Unicode. I have an issue to say if array contains 1,000
elements or may be 1 MB elements. My code reads some elements from
big array and copy them into sub array group and assign memory address
to class' member function.

My question is -- you decide to assign data type to int. You use
pushback to copy each element from big array into sub array. Will
pushback do 1,000 times in loop? After vector is done, sub array will
be copied back to big array.

And another question. Do vector support 2D arrays and 3D arrays?
 
I

Immortal Nephi

"Immortal Nephi" <[email protected]>



void Foo::read( const char arrayName[], int arraySize )
{
 int i = 0;
 do
 {
 cout << arrayName[ i++ ];
 }
 while ( i != arraySize );
cout << endl;}

<<

This is hardly a read function and will do wild things if 0 is passed as
size.
Sometimes, programmer prefers to use pointer instead because they
want to test null terminator.

Yes, in C for char arrays it is common to have space for X characters but
store less than that amount, and indicate end with 0 terminator. as size of
the array is fixed at compile time, and the content is so often created
in-place from fragments or read from outside. Size of the array object is
only good to avoid buffer overrun, not to represent the actual length.
If you have 16 elements in an arrayName, it is your responsibility to
provide arraySize or null terminator.  The operating system will
trigger violation security denied if more elements are read or walked
out outside the arrayName range.

You just think so. If you do that the behavior is undefined. And in practice
you can see stuff like crash, corrupt data -- or the "execute arbitrary code
from remote" attack on the system...
Do you have the solution if you provide wrong array size information?

As others suggested, forget C-style ararys and use std::vector in general,
and std::string to hold and manipulate string...  They know the size
properly and have many other useful features.

Let me give you an example. It replaces referance example and pointer
example like I described above earlier.

#include <iostream>
#include <vector>
#include <iterator>
using namespace std;

void printVector( const vector< int > &vecArrayName )
{
vector< int >::const_iterator constIterator;

for ( constIterator = vecArrayName.begin();
constIterator != vecArrayName.end(); ++constIterator )
cout << *constIterator << ' ';

cout << endl;
}

int main()
{
int arrayName[ 16 ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16 };

vector< int > vecArrayName( arrayName, arrayName + 16 );

printVector( vecArrayName );

return 0;
}

My question is still not answered. Do vector copy each element from
arrayName to vecArrayName? Or do vector receive address memory
through reference?

You saw reference example above. You provide arraySize in the second
parameter of the function. You create vecArrayName and you have to
provide arraySize in the constructor. What if you provide wrong
arraySize. Wrong arraySize may look like 10 elements instead of 16
elements.

Thanks for being patience.
 
V

Vladimir Jovic

Immortal said:
"Immortal Nephi" <[email protected]>



void Foo::read( const char arrayName[], int arraySize )
{
int i = 0;
do
{
cout << arrayName[ i++ ];
}
while ( i != arraySize );
cout << endl;}

<<

This is hardly a read function and will do wild things if 0 is passed as
size.
Sometimes, programmer prefers to use pointer instead because they
want to test null terminator.
Yes, in C for char arrays it is common to have space for X characters but
store less than that amount, and indicate end with 0 terminator. as size of
the array is fixed at compile time, and the content is so often created
in-place from fragments or read from outside. Size of the array object is
only good to avoid buffer overrun, not to represent the actual length.
If you have 16 elements in an arrayName, it is your responsibility to
provide arraySize or null terminator. The operating system will
trigger violation security denied if more elements are read or walked
out outside the arrayName range.
You just think so. If you do that the behavior is undefined. And in practice
you can see stuff like crash, corrupt data -- or the "execute arbitrary code
from remote" attack on the system...
Do you have the solution if you provide wrong array size information?
As others suggested, forget C-style ararys and use std::vector in general,
and std::string to hold and manipulate string... They know the size
properly and have many other useful features.

Let me give you an example. It replaces referance example and pointer
example like I described above earlier.

#include <iostream>
#include <vector>
#include <iterator>
using namespace std;

void printVector( const vector< int > &vecArrayName )
{
vector< int >::const_iterator constIterator;

for ( constIterator = vecArrayName.begin();
constIterator != vecArrayName.end(); ++constIterator )
cout << *constIterator << ' ';

cout << endl;
}

int main()
{
int arrayName[ 16 ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16 };

vector< int > vecArrayName( arrayName, arrayName + 16 );

printVector( vecArrayName );

return 0;
}

My question is still not answered. Do vector copy each element from
arrayName to vecArrayName? Or do vector receive address memory
through reference?

It copies. Imagine what would happen if the array gets out of scope. It
would be destroyed, and the vector would hold invalid reference.
You saw reference example above. You provide arraySize in the second
parameter of the function. You create vecArrayName and you have to
provide arraySize in the constructor. What if you provide wrong
arraySize. Wrong arraySize may look like 10 elements instead of 16
elements.

Then your vector would try to copy elements past the array. I am not
100% sure, but that might be UB. Your vector will contain garbage
 
I

Immortal Nephi

Immortal said:
"Immortal Nephi" <[email protected]>
void Foo::read( const char arrayName[], int arraySize )
{
 int i = 0;
 do
 {
 cout << arrayName[ i++ ];
 }
 while ( i != arraySize );
cout << endl;}
<<
This is hardly a read function and will do wild things if 0 is passed as
size.
Sometimes, programmer prefers to use pointer instead because they
want to test null terminator.
Yes, in C for char arrays it is common to have space for X characters but
store less than that amount, and indicate end with 0 terminator. as size of
the array is fixed at compile time, and the content is so often created
in-place from fragments or read from outside. Size of the array object is
only good to avoid buffer overrun, not to represent the actual length.
If you have 16 elements in an arrayName, it is your responsibility to
provide arraySize or null terminator.  The operating system will
trigger violation security denied if more elements are read or walked
out outside the arrayName range.
You just think so. If you do that the behavior is undefined. And in practice
you can see stuff like crash, corrupt data -- or the "execute arbitrary code
from remote" attack on the system...
Do you have the solution if you provide wrong array size information?
As others suggested, forget C-style ararys and use std::vector in general,
and std::string to hold and manipulate string...  They know the size
properly and have many other useful features.
Let me give you an example.  It replaces referance example and pointer
example like I described above earlier.
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
void printVector( const vector< int > &vecArrayName )
{
   vector< int >::const_iterator constIterator;
   for ( constIterator = vecArrayName.begin();
           constIterator != vecArrayName.end(); ++constIterator )
           cout << *constIterator << ' ';
   cout << endl;
}
int main()
{
   int arrayName[ 16 ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16 };
   vector< int > vecArrayName( arrayName, arrayName + 16 );
   printVector( vecArrayName );
   return 0;
}
My question is still not answered.  Do vector copy each element from
arrayName to vecArrayName?  Or do vector receive address memory
through reference?

It copies. Imagine what would happen if the array gets out of scope. It
would be destroyed, and the vector would hold invalid reference.
You saw reference example above.  You provide arraySize in the second
parameter of the function.  You create vecArrayName and you have to
provide arraySize in the constructor.  What if you provide wrong
arraySize.  Wrong arraySize may look like 10 elements instead of 16
elements.

Then your vector would try to copy elements past the array. I am not
100% sure, but that might be UB. Your vector will contain garbage- Hide quoted text -

Thanks for the answer. It does make sense. Providing wrong arraySize
to vector. Extra elements will receive garbage somewhere in memory
(maybe violation security denied) **OR** they MIGHT receive data from
another variable. I stick with reference example. I don't need
vector because I don't do search or grow / shrink. Anyway...(I mean
arrayName is always fixed at compile-time.) Thanks for your patience.
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top