0

I am playing around with arrays as pointers.

#include<iostream>
#include<cstring>

using namespace std;

int main(){
    string *myarray[20];
    *myarray[0]="Ich denke sein";
    *myarray[1]="Ich sehe sein";
    cout<<*myarray[0]<<endl;
    cout<<*myarray[1]<<endl;
    return 0;
}

The above code compiles but while executing, there is error, the program hangs. When I debugged using visual studio, it said memory allocation error. What is the error here? As I know, I am dealing with values refered by pointers everywhere.

2
  • There is never a hard limit on answers that can be posted. If you would like to deter users because one of the answers has worked for you, then consider accepting the one that worked by clicking the check mark next to the answer. Even still, if another user thinks they have a better solution they are still free to post even after an answer has been accepted. That's the whole point of Stack Exchange. Commented Mar 17, 2014 at 21:24
  • Sorry, I will delete my comment. Commented Mar 17, 2014 at 21:26

7 Answers 7

3

You have allocated an array of 20 pointers to strings. But you haven't allocated any memory for what the pointers point to. Rather than pursuing this and using new to allocate memory for the strings, it would be better instead to allocate memory for the strings themselves in the array and you will be fine.

string myarray[20];
myarray[0]="Ich denke sein";
myarray[1]="Ich sehe sein";
cout<<myarray[0]<<endl;
cout<<myarray[1]<<endl;

In response to your comment, to make your program work as is, set the pointer to point to a new string object

myarray[0]=new string("Ich denke sein");
myarray[1]=new string("Ich sehe sein");

The other lines of your program remain unchanged. Note

  • the remaining myarray[i], for 2<=i && i<20 will still be garbage. (It would be neater to set them to 0 or nullptr in a for loop.)
  • you also ought to delete the objects you've allocated. (You could use a for loop over the whole array to do this if you'd cleared the rest of the pointers first.)
  • as other answers have commented, in modern C++ it is best not to use pointers directly unless you absolutely have to. And you would also typically prefer to use a vector for arrays rather than using the built-in type (in C++11 the array type can be used for arrays where you know the length at compile time).
Sign up to request clarification or add additional context in comments.

1 Comment

I know this method. How would I solve this using new to allocate. I should have the first line. string *myarray[20]; Let's say this is assignment and I can't modify the first line.
1

You have an array of uninitialized pointers:

string *myarray[20];

Then you treat it as if the pointers pointed to valid objects:

*myarray[0]="Ich denke sein";

This is undefined behaviour. It is unrelated to arrays. It is the same as doing this:

std::string* s;
*s = "boom!";

Besides that, you really should #include <string> if you want to use std::string.

Comments

1

First, you didn't include the right header. For string, you should:

#include <string>

Second, the program hangs because you're assigning data to where the pointer is pointing to. So where does myArray[0] point to? You don't know, I don't know, no one knows. It seems you didn't get the grasp of what pointers are all about, regardless of whether you're dealing with arrays of them or not.

To use a pointer, it must point somewhere valid. If you don't do that, then you can't dereference the pointer and assign data. It's that simple.

Comments

1

May be the problem is that those pointers aren't pointing to any addres in the memory. Pointers should be initialized to somewhere using the "address of operator" (&) or putting them in the free memory, using the "new" keyword, and if you do so, don't forget to free that memory using the "delete" keyword. Hope that works for you.

Comments

1

What is the error here?

The error is, that you are using pointers ;-)

#include <string>
#include <iostream>

int main() {
   std::string myarray[2] = {
      { "Ich denke sein" }, { "Ich sehe sein" } };
   std::cout << myarray[0] << std::endl;
   std::cout << myarray[1] << std::endl;
   return 0;
}

What is the reason behind using pointer? In C++ you mostly never use pointers.

Kind regards - Andreas

Comments

1

You have declared an array of 20 pointers. You will have to allocate memory to the pointer before assigning a value at the location to which it points.

string* myarray[20];
for(int i = 0; i < 20; i++) {
     myarray[i] = new string();//allocating memory to the pointer
}
*myarray[0]="Ich denke sein";
*myarray[1]="Ich sehe sein";
cout<<*myarray[0]<<endl;
cout<<*myarray[1]<<endl;

2 Comments

Did you try to compile and run this? It still hangs.
It works fine on my VC++ compiler. Try using string** myarray=new *myarray[20]; on the first line.
1

Pointers are used to point to memory addresses, like this:

String text ="hello";
String *myptr = &text;

Now the pointer points to the address of the string text.

Or you can use new keyword to allocate memory for an array of pointers. new returns you a pointer to the first position of the array. Like this:

int *myptr = new int[20];

But remember to use delete, to deallocate the allocated memory later. Like this:

delete[] myptr

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.