1

I'm trying to create a string array and use a pointer to modify it. I'm not sure how to declare the pointer since strings can vary in length, and I think this is what causes the error.

My code looks something like this:

#includes <string>
#includes <iostream>
using namespace std;

string *users = NULL;
int seatNum = NULL;
cin >> seatNum;
users = new string[seatNum];
string name;
cin >> name;
users[seatNum] = name;

It throws me an Write Access Violation when I try to change its value. From what I've read it's because strings are compiled as read-only, so my question is how would I/what would I do to change it? Easy-to-understand explanations would be preferable.

1
  • 3
    users = new string[seatNum]; creates an array of strings indexed from 0 to seatNum - 1, so users[seatNum] accesses past the bounds of the array. Commented Nov 26, 2013 at 2:29

4 Answers 4

4

You're accessing memory beyond the range of the allocated array

users = new string[seatNum];
users[seatNum] = name;

The first element is [0]. The last is [seatNum-1]

Sign up to request clarification or add additional context in comments.

1 Comment

Doh, I have no idea how I could overlook something as simple as this. Sometimes I guess you just have to ask someone, thanks anyway!
1

You have created an array of seatNum elements. Array element indexing starts at 0 therefore the range of valid indexes is [0, seatNum - 1]. By accessing users[seatNum] = ... you are effectively going past the last valid element of the array. This invokes UB (undefined behavior).


I see you have already made the right choice of using std::string instead of C-style strings. Why not make the same choice over arrays?

#include <string>
#include <array> 
#include <iostream>

int main(int, char*[]) {
    int seatNum = 0;
    std::cin >> seatNum;
    std::vector<std::string> users(seatNum);
    std::cin >> users[0];
    return 0;
}

Try to avoid pointers and C-style arrays, especially dynamic ones.

Comments

0

A few things:

  1. int seatNum will be allocated on the stack and will never be NULL. You should set it to 0.

  2. You are setting users[seatNum] which is out of bounds causing your program to crash. You can only use indices from 0 to seatNum-1.

Updated: Chris is correct. I looked into it and strings are indeed mutable in C++.

1 Comment

The last point isn't really correct. Strings are mutable in C++, so there's no reason to create a copy. It just modifies the object in question. Whether that means doing a deep copy of the contained characters varies.
0

firstly you cannot set null value to int type data

int seatNum = NULL; // wrong 
int seatNum = 0;  // right

secondly string bounds from 0 to seatnum -1

users[seatNum-1] = name; // right

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.