-3

In my code, I've been trying to find the length of a specific string in a string array. However, when I do it, no matter what method I try to find the length of the specific string, always returns -858993460

The code I've been working with is

void drawChoices(int x, std::string s [], int iChoice)
{
    setCurPos(x, 1); //this function just sets the console cursor
    for (int i = 0; i <= iChoice; i++)
    {
        color(15, 4); //this function just sets the console color
        std::cout << "[" << s[i] << "]";
        color(8, 8);
        int asfa = sizeof s[i]; //this is the one causing trouble
        std::cout << " \n";
    }
}

I tried finding the length of the string in the array with sizeof, string::length, and strlen, but those yielded the same results.

JoeyChor
  • 63
  • 1
  • 7
  • How did you determine that asfa has this negative value? – Vlad from Moscow Oct 26 '14 at 13:08
  • **-1** the provided code does not illustrate the claimed behavior (of negative size). – Cheers and hth. - Alf Oct 26 '14 at 13:14
  • 4
    -858993460 is a magic number when you use Visual C++. Converted to hex, it is 0xcccccccc. Which is the value that all variables are initialized with in the Debug build. Whenever you see it back while debugging you go "Ah, I'm using an uninitialized variable!" Let the debugger tell you which one is the problem, the off-by-one bug in your for(;;) loop is notable. – Hans Passant Oct 26 '14 at 13:24
  • -858993460 = 0xCCCCCCCC which means [you've accessed uninitialized memory](https://stackoverflow.com/q/370195/995714) – phuclv Aug 18 '18 at 11:12

2 Answers2

0

You can never obtain the length of a string with sizeof. The sizeof operator is evaluated at compile time and returns the size of a type or variable. Your use of sizeof is:

sizeof s[i]

Since s[i] has type std::string, that expression evaluates to whatever size your implementation's std::string happens to be. But that is determined at compile time, and so has no dependency on the contents of the buffer, and is something that you are simply not interested in knowing.

Use either std::string::size() or std::string::length() to obtain the length of a std::string.

So, in your code you would use

s[i].length()

to obtain the length, that is the number of character elements, in the string s[i]. Do note also that the size() and length() methods, which are interchangeable, return std::string::size_type which you should use rather than int.

As for where the negative value comes from, we cannot say. Your code doesn't output the value that sizeof yielded, which would not be negative in any case.

Should your loop really be

for (int i = 0; i <= iChoice; i++)

rather than

for (int i = 0; i < iChoice; i++)

Rather than passing, and presumably storing, arrays of std::string, it would be more idiomatic to use a C++ container. In this case std::vector<std::string>.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • @David Heffernan s[i] has type std::string and its size even placed in an object of type int can not be negative.:) – Vlad from Moscow Oct 26 '14 at 13:09
  • Thank you for your comment, but it still doesn't work. As I said in my post, I already tried `std::string::length`. However, I did swap `int` with `size_t`, but that generated a large positive number. Also, for your other suggestion, I find that useful so I don't have to subtract one every time I call the function with the parameter. Thank you. – JoeyChor Oct 26 '14 at 22:37
  • No. `length` and `size` are known to work well. `strlen` is for C strings, and `sizeof` just wrong for this application. I cannot comment on your uninitialized value since I cannot see your code. – David Heffernan Oct 26 '14 at 22:57
0

From what @VladfromMoscow mentioned, I have figured out the problem.

The problem was when I was using Visual Studio 2013's debugging and local windows, I was debugging the same time the variable was initializing.

With the local window compiling the order of the line and how Visual Studio's debugging works, it gives out a random number because the variable wasn't initialized.

It reads size_t asfa first, stops it, local window gets the value, and then executes s[i].length(); once I continue it.

I thought it would be the program's fault because I tried outputting std::cout << asfa;, but I remebered that you can't have an int or size_t to a string directly, without a cast or conversion. Thank you for all of you who helped.

JoeyChor
  • 63
  • 1
  • 7