accessing array with pointer giving an extra element - c++

Ok a very basic question, but I am stuck at it. I cannot figure out the extra mysterious value at the end of the array. I just tried to traverse the array through its base address, plain and simple. But the garbage value at the end of the array remains constant every time I execute it
g++ complier 64 bit machine.
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
void printarray(int * arr){
while(*(arr)){ cout<<*arr<<" "; arr++; }
cout<<endl;
}
int main(){
int arr[] = { 3,2,4,1,5 };
printarray(arr); // prints 3,2,4,1,5,32765
return 0;
}
EDIT 1: I understand that the while will terminate whenever it comes across a 0 in the array , or if no 0 is found will go insane . But still I would want you guys to look at the following test cases
3,2,0,4,1,5 // outputs 3,2
3,2,4,1,5,7,8,9 // outputs same array correctly
3,2,4,1,5,7,8,9,10 // outputs array+ 1 varying garbage at last
//observations, this method works for even sized arrays not containing
//0, while for odd it emits an additional garbage value.
My question is if the while only breaks at 0 , why does it break at
arrayLength+1 everytime ? Also what's with this even odd length ?

An array is not terminated in any special way. What you are trying to do is not working because while(*arr) relies on a wrong assumption: that there is a 0 element at the end of the array (a sentinel) but you declare the array a {3,2,4,1,5} so there is no end element.
I think your misconcept comes from the fact that you are not getting the point that an int* is just a memory address, whenever you increment it by ++arr you are basically adjusting it by sizeof(int). Nothing more, when you reach the end of the array then the address just points after the array, to whatever value could be there.

You get the extra element(s) because there is nothing about a pointer that tells the compiler how many elements the pointer points to. When you do
while(*(arr)){...}
The while loop will continue running untill *arr == 0. Since you array doesn't contain a 0 it will keep going past the end of the array untill it finds a 0. This is undefined behavior as you are accessing memory you do not own with that pointer.
I think you may be confusing how char arrays(c-strings) work compared to other data types. When you have
char arr[] = "something";
while(*(arr)){...}
This ends at the end of the array as c-strings get a null terminator(0) added to the end of the string automatically. This allows you to do things like the above loop as you know that null terminator will be there and if it is not then that is on the person you created the string.

An array decays into a pointer when passed to a function, and this function knows nothing about the array's length.
That while(*arr) stuff is incorrect. It will stop only when some value this pointer points to is zero. But who said a zero is placed at the end of an array?? When you increment arr, you can easily get out of bounds of your array (the function doesn't know its size!), and then *arr will give you whatever the heck is stored at the memory address arr points to at the moment.
To iterate over an array, pass the array itself and its length. Otherwise this will iterate over and over until the value of *arr will be zero.

Pointers aren't terminated in C++ by any character.
This works on other types like char* only because it's terminated by an \0(0).
An INT-Array u need to count the Elements before you can pass them into something like that, for example here with the ending 5 from your Pointer:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void printarray(int * arr){
int *saved=arr;
while(*saved!=5)
{
cout<<*saved++<<" ";
}
*saved=0;
cout<<endl;
}
int main(){
int arr[] = { 3, 2, 4, 1, 5 };
printarray(arr); // prints now without ending 5.
return 0;
}
Otherwise, you need to pass the counter of elements.
There is no way C++ could know how many elements your pointer points to.

Related

Assigning value to struct's field, change also other variable

Simple code
#include <iostream>
using namespace std;
struct foo {
int bar;
};
struct foo tab[2];
int sum = 0;
int main()
{
tab[2].bar = 3; //this change 'sum' value!
cout << sum << endl;
return 0;
}
result in 3 instead of 0. It is unbelievable, so problably I am missing something. What I have done wrong?
Arrays start at 0, so tab[2] would be the third element, but you only allocated 2 of them.
In this case, sum is in the memory directly after tab, so when you go to where the third tab would be, you're actually in the memory for sum.
Notice that you access tab[2] which is an overflow (its size is 2 so valid indices are 0 and 1).
So tab[2] accesses the memory address of sum.
When you declare your variable
struct foo tab[2];
tab[2] does not exist.
You can only do
tab[0].bar = 3
tab[1].bar = 3
because arrays index starts from 0 and ends at arraySize-1.
If you look closely tab has a length of 2. By accessing the index 2, you are accessing memory out of the tab, which means you are accessing sum.
This is the reason why you are changing sum.
First of all, turn on compiler warnings! If you'd allow the compiler to help you, then it would very likely point out the exact error in this line:
tab[2].bar = 3; //this change 'sum' value!
Depending on which compiler you use, the warning may be as follows:
warning: array subscript is above array bounds
struct foo tab[2]; has two elements with indices 0 and 1, you try access a non-existing 3rd element. This results in undefined behaviour of your program. Whatever results you got, it was just random. Your program could also randomly crash.
Note that your code is also half C and half C++. That's not good. You don't need to write struct foo when you want to refer to the foo type, it's enough to write foo. Instead of a raw array, std::array<Foo, 2> can be used. And using namespace std; should not be used by default.

memory allocation details using strcat

I'm using following code to print strings.
#include<bits/stdc++.h>
using namespace std;
int main(){
char s[100];
while(1){
char f[10000];
cin>>s;
strcat(f,s);
cout<<f<<endl;
}
return 0;
}
in each iteration a new allocation of f character array is done.
input
a
b
c
d
I expected that the output would be like this:
a
b
c
d
but actual output is:
a
ab
abc
abcd
Why is this happening ? Though I'm declaring a new array in each iteration
so why is this kind of output ??
Because: undefined behavior.
You are not initializing the array. You're only declaring it. Just because you declare
char f[10000];
Does not mean that it's going to be initialized to an empty array, automatically.
So, you get a declared array, containing random data.
At this point, you cannot expect to get any predictable behavior. The results you got would be one plausible outcome. But not the only one.
EDIT: Each time around the loop, the array ends up at the same place on the stack. The first time in, your operating system set up a new page, for the stack, cleared to 0. You strcat()ed your string to it. The next time around the loop, your old data, from the previous iteration, is still there. So, you strcat() more stuff, appending it to the end.

What are the ramifications of simply checking a pointers value in a conditional statement?

Here goes my code:
#include <iostream>
using namespace std;
int countX(char*, char);
int main() {
char msg[] = "there are four a's in this sentence a a";
//char *ptr = msg; // <----- question 2!
cout << countX(msg, 'a');
cin.get();
}
int countX(char* ptr, char x) {
int c = 0;
for (; *ptr; ptr++) {
if (*ptr == x) c++;
}
/*
while(*ptr) {
if(*ptr==x) c++;
ptr++;
}
*/
return c;
}
I was wondering a few things specifically regarding safe practice and pointers:
My conditional statement in the for-loop ; *ptr ;, is this safe practice? Will it every break if there happens to be something stored in the memory address right next to the last element in the array? Is that even possible? How does it know when to terminate? When is *ptr deemed unacceptable?
(concerning the commented out char *ptr = msg; in the main): I understand a pointer and an array are very similar, however, is there a difference between passing the actual array to countX vs. passing a pointer (which points to the beginning of the array?).
In countX I've provided two different ways to approach the simple problem. Is one considered superior over the other?
Q My conditional statement in the for-loop ; *ptr ;, is this safe practice?
A Yes, most of the time. See below for more details.
Q Will it every (I know you meant ever) break if there happens to be something stored in the memory address right next to the last element in the array?
A Yes.
Q Is that even possible?
A Yes. You can easily access the memory one past the last character of the array and make it something other than the null character.
Q How does it know when to terminate?
A It will terminate when you encounter the terminating null character of a string. If the null character has been replaced by something else, the behavior is going to be unpredictable.
Q When is *ptr deemed unacceptable?
A If the string length is len, it is OK to set ptr in the range msg and msg+len. If ptr points to anything beyond that range, the behavior is undefined. Hence, they should be considered unacceptable in a program.
Q (concerning the commented out char *ptr = msg; in the main): I understand a pointer and an array are very similar, however, is there a difference between passing the actual array to countX vs. passing a pointer (which points to the beginning of the array?).
A No. They are identical.
Q In countX I've provided two different ways to approach the simple problem. Is one considered superior over the other?
A No they are not. It comes down to personal taste. I happen to like to use for loops while I know people that like to use while loops.
Q1 : My conditional statement in the for-loop ; *ptr ;, is this safe practice? Will it every break if there happens to be something stored in the memory address right next to the last element in the array? Is that even possible? How does it know when to terminate? When is *ptr deemed unacceptable?
Ans : When used with c-style strings, yes it is a safe practice. Any c-style string necessarily ends with '\0', which is basically 0. The behavior is undefined when the '\0' is not there. So the loop would break at the end of the string. *ptr would never terminate if it is anything other than a c-style string. For example, a c-style "hello" is actually an array containing 'h', 'e', 'l', 'l', 'o', '\0'. So, the loop exists at '\0', never accessing the memory after it.
it is possible to access the memory after the last element of an array. For example,
int a[5] = {0,1,2,3,4,5};
int *p = a+5;
p is accessing the element after the last element of the array a.
Q2 :(concerning the commented out char *ptr = msg; in the main): I understand a pointer and an array are very similar, however, is there a difference between passing the actual array to countX vs. passing a pointer (which points to the beginning of the array?).
Ans : Arrays and pointers are not exactly similar. Its just that an array name is nothing but a constant pointer pointing to the first element of the array. Consider the previous example i wrote. In that, a[3], 3[a], *(a+3) and *(p+3), all refer to the same element. Since you are passing by value, the value of the constant pointer msg would just be copied to ptr. So, no, it would make no difference.
Q3 : In countX I've provided two different ways to approach the simple problem. Is one considered superior over the other?
Ans : I am not an expert, but i'd say no.
Also, you probably dont need the cin.get().
This is very bad practice.
What you're doing in the for condition is basically if (*ptr), in other words, does the memory pointed to by ptr contain a non-zero value?
So if the memory location after the string contains a non-zero value (maybe from another variable using the space) or a garbage value then your loop could go infinite, or give you an incorrect value. Instead you should run the loop from 0 to the length of your string.

Pointer to int array, passing and using it from another method

I haven't cemented my learning of C++ arrays and have forgotten how to do this properly. I've done it with char array before but its not working as well for int array.
I declare a new blank int array:
int myIntArray[10];
So this should be an array of nulls for the moment correct?
Then I assign a pointer to this array:
int *pMyArray = myIntArray
Hopefully thats correct to there.
Then I pass this to another method elsewhere:
anotherMethod(pMyArray)
where I want to assign this pointer to a local variable (this is where I'm really not sure):
anotherMethod(int *pMyArray){
int myLocalArray[];
myLocalArray[0] = *pMyArray;
}
I'm not getting any compilation errors but I'm not sure this is right on a few fronts. Any and all help and advice appreciated with this.
Edit:
I should have said what I am trying to do.
Very simple really, I'd just like to modify a local array from another method.
So I have:
Method 1 would contain:
int myArray1[10] = {0};
Method 2 would be passed the pointer to myArray:
Then some code to modify the variables in the array myArray.
int myIntArray[10];
This is an uninitialized array. It doesn't necessarily contain 0's.
int *pMyArray = myIntArray
Okay, pMyArray points to the first element in myIntArray.
anotherMethod(int *pMyArray){
int myLocalArray[10];
myLocalArray[0] = *pMyArray;
}
This doesn't assign your pointer to anything, it assigns the first value of the local array to the int pointed to by pMyArray, which, remember, was uninitialized. I added the 10 there because you can't have an array of unknown size in C++.
To modify what pMyArray points to, you need to pass it by reference:
anotherMethod(int *& pMyArray)
Also, if you assign it to some values in automatic storage, it will result in undefined behavior, as that memory is no longer valid when the function exits.
int myIntArray[10];
So this should be an array of nulls for the moment correct?
No, this is an array of 10 integers containing values depending on the storage specification.
If created locally, it has random garbage values.
If created globally it is value initialized which is zero initialized for POD.
Besides that your method just assigns the local array with the first vale of the array you pass.
What are you trying to do exactly? I am not sure.
int myIntArray[10];
So this should be an array of nulls for the moment correct?
Not correct, it is an array of 10 uninitialized ints.
int *pMyArray = myIntArray
Hopefully thats correct to there.
Not quite correct, pMyArray is a pointer to the 1st element, myIntArray[0].
where I want to assign this pointer to a local variable (this is where
I'm really not sure):
If you really need to assign the pointer, you have to use this code
int *p_myLocalArray;
p_myLocalArray = pMyArray;
There are a few mistakes here.
First, array of zeros (not nulls) is achieved by using the initializer syntax:
int myIntArray[10] = {0};
Second, int myLocalArray[]; has a size of 0. And even if it did have a size of, say, 10, writing myLocalArray[0] = *pMyArray; will assign the first int from pMyArray into mLocalArray, which is not what you meant.
If you want to assign a pointer of the array, then simply:
int *myLocalPointer;
myLocalPointer = pMyArray;
If you want a local copy of the array, you will need to copy it locally, and for that you also need the size and dynamic allocation:
void anotherMethod(int *pMyArray, int size){
int *myLocalArray = (int *)malloc(size * sizeof(int));
memcpy(myLocalArray, pMyArray, size * sizeof(int));
...
free(myLocalArray);
}

C++ Why is this passed-by-reference array generating a runtime error?

void pushSynonyms (string synline, char matrizSinonimos [1024][1024]){
stringstream synstream(synline);
vector<int> synsAux;
int num;
while (synstream >> num) {synsAux.push_back(num);}
int index=0;
while (index<(synsAux.size()-1)){
int primerSinonimo=synsAux[index];
int segundoSinonimo=synsAux[++index];
matrizSinonimos[primerSinonimo][segundoSinonimo]='S';
matrizSinonimos [segundoSinonimo][primerSinonimo]='S';
}
}
and the call..
char matrizSinonimos[1024][1024];
pushSynonyms("1 7", matrizSinonimos)
It's important for me to pass matrizSinonimos by reference.
Edit: took away the & from &matrizSinonimos.
Edit: the runtime error is:
An unhandled win32 exception occurred in program.exe [2488]![alt text][1]
What's wrong with it
The code as you have it there - i can't find a bug. The only problem i spot is that if you provide no number at all, then this part will cause harm:
(synsAux.size()-1)
It will subtract one from 0u . That will wrap around, because size() returns an unsigned integer type. You will end up with a very big value, somewhere around 2^16 or 2^32. You should change the whole while condition to
while ((index+1) < synsAux.size())
You can try looking for a bug around the call side. Often it happens there is a buffer overflow or heap corruption somewhere before that, and the program crashes at a later point in the program as a result of that.
The argument and parameter stuff in it
Concerning the array and how it's passed, i think you do it alright. Although, you still pass the array by value. Maybe you already know it, but i will repeat it. You really pass a pointer to the first element of this array:
char matrizSinonimos[1024][1024];
A 2d array really is an array of arrays. The first lement of that array is an array, and a pointer to it is a pointer to an array. In that case, it is
char (*)[1024]
Even though in the parameter list you said that you accept an array of arrays, the compiler, as always, adjusts that and make it a pointer to the first element of such an array. So in reality, your function has the prototype, after the adjustments of the argument types by the compiler are done:
void pushSynonyms (string synline, char (*matrizSinonimos)[1024]);
Although often suggested, You cannot pass that array as a char**, because the called function needs the size of the inner dimension, to correctly address sub-dimensions at the right offsets. Working with a char** in the called function, and then writing something like matrizSinonimos[0][1], it will try to interpret the first sizeof(char**) characters of that array as a pointer, and will try to dereference a random memory location, then doing that a second time, if it didn't crash in between. Don't do that. It's also not relevant which size you had written in the outer dimension of that array. It rationalized away. Now, it's not really important to pass the array by reference. But if you want to, you have to change the whole thingn to
void pushSynonyms (string synline, char (&matrizSinonimos)[1024][1024]);
Passing by reference does not pass a pointer to the first element: All sizes of all dimensions are preserved, and the array object itself, rather than a value, is passed.
Arrays are passed as pointers - there's no need to do a pass-by-reference to them. If you declare your function to be:
void pushSynonyms(string synline, char matrizSinonimos[][1024]);
Your changes to the array will persist - arrays are never passed by value.
The exception is probably 0xC00000FD, or a stack overflow!
The problem is that you are creating a 1 MB array on the stack, which probably is too big.
try declaring it as:
void pushSynonyms (const string & synline, char *matrizSinonimos[1024] )
I believe that will do what you want to do. The way you have it, as others have said, creates a 1MB array on the stack. Also, changing synline from string to const string & eliminates pushing a full string copy onto the stack.
Also, I'd use some sort of class to encapsulate matrizSinonimos. Something like:
class ms
{
char m_martix[1024][1024];
public:
pushSynonyms( const string & synline );
}
then you don't have to pass it at all.
I'm at a loss for what's wrong with the code above, but if you can't get the array syntax to work, you can always do this:
void pushSynonyms (string synline, char *matrizSinonimos, int rowsize, int colsize )
{
// the code below is equivalent to
// char c = matrizSinonimos[a][b];
char c = matrizSinonimos( a*rowsize + b );
// you could also Assert( a < rowsize && b < colsize );
}
pushSynonyms( "1 7", matrizSinonimos, 1024, 1024 );
You could also replace rowsize and colsize with a #define SYNONYM_ARRAY_DIMENSION 1024 if it's known at compile time, which will make the multiplication step faster.
(edit 1) I forgot to answer your actual question. Well: after you've corrected the code to pass the array in the correct way (no incorrect indirection anymore), it seems most probable to me that you did not check you inputs correctly. You read from a stream, save it into a vector, but you never checked whether all the numbers you get there are actually in the correct range. (end edit 1)
First:
Using raw arrays may not be what you actually want. There are std::vector, or boost::array. The latter one is compile-time fixed-size array like a raw-array, but provides the C++ collection type-defs and methods, which is practical for generic (read: templatized) code.
And, using those classes there may be less confusion about type-safety, pass by reference, by value, or passing a pointer.
Second:
Arrays are passed as pointers, the pointer itself is passed by value.
Third:
You should allocate such big objects on the heap. The overhead of the heap-allocation is in such a case insignificant, and it will reduce the chance of running out of stack-space.
Fourth:
void someFunction(int array[10][10]);
really is:
(edit 2) Thanks to the comments:
void someFunction(int** array);
void someFunction(int (*array)[10]);
Hopefully I didn't screw up elsewhere....
(end edit 2)
The type-information to be a 10x10 array is lost. To get what you've probably meant, you need to write:
void someFunction(int (&array)[10][10]);
This way the compiler can check that on the caller side the array is actually a 10x10 array. You can then call the function like this:
int main() {
int array[10][10] = { 0 };
someFunction(array);
return 0;
}