I need to use an array of strings with an unknown size. Here I have an example to see if all works fine. I need to know that array's size in ClassC but without passing that value as an argument. I've see so many ways to do it (here and in google) but as you will see now, they didn't work. They return the number of chars in the first position of the array.
我需要使用大小未知的字符串数组。在这里,我有一个例子,看看是否一切正常。我需要在ClassC中知道该数组的大小,但不将该值作为参数传递。我已经看到了很多方法(在这里和谷歌),但正如你现在所看到的,它们没有用。它们返回数组第一个位置的字符数。
void ClassB::SetValue()
{
std::string *str;
str = new std::string[2]; // I set 2 to do this example, lately it will be a value from another place
str[0] ="hello" ;
str[1] = "how are you";
var->setStr(str);
}
Now, in ClassC if I debug, strdesc[0] ="hello" and strdesc[1] = "how are you"
, so I suppose that class C is getting the info ok....
现在,在ClassC中如果我调试,strdesc [0] =“你好”和strdesc [1] =“你好吗”,所以我认为C类正在获取信息确定....
void classC::setStr(const std::string strdesc[])
{
int a = strdesc->size(); // Returns 5
int c = sizeof(strdesc)/sizeof(strdesc[0]); // Returns 1 because each sizeof returns 5
int b=strdesc[0].size(); // returns 5
std::wstring *descriptions = new std::wstring[?];
}
So.. in classC, how can I know strdesc's array size, that should return 2?? I have also tried with:
那么..在classC中,我怎么知道strdesc的数组大小,应该返回2?我也尝试过:
int i = 0;
while(!strdesc[i].empty()) ++i;
but after i=2
the program crashes with a segmentation fault.
但是在i = 2之后,程序崩溃并出现分段错误。
Thanks,
谢谢,
Edit with the possibles SOLUTIONS:
使用可能的解决方案进行编辑:
Conclusion: There is no way to know the array's size once I pass its pointer to another function
结论:一旦我将指针传递给另一个函数,就无法知道数组的大小
- Pass the size to that function... or...
- 将大小传递给该函数......或者......
- Use vectors with std::vector class.
- 使用带有std :: vector类的向量。
2 个解决方案
#1
1
how can I know strdesc's array size
我怎么知道strdesc的数组大小
You cannot know the size of an array from a pointer to that array.
您无法从指向该数组的指针知道数组的大小。
What you can do is pass the size as another parameter. Or even better, use a vector instead.
您可以做的是将大小作为另一个参数传递。甚至更好,使用矢量代替。
but after i=2 the program crashes with a segmentation fault.
但是在i = 2之后,程序崩溃并出现分段错误。
Accessing beyond the array boundary has undefined behaviour.
超出数组边界的访问具有未定义的行为。
#2
1
With this Kind of code you will get memory leaks and other kind of C-style problems.
使用这种代码,您将获得内存泄漏和其他类型的C风格问题。
use vector:
使用矢量:
#include <vector>
#include <string>
#include <iostream>
...
std::vector<std::string> my_strings;
my_strings.push_back("Hello");
my_strings.push_back("World");
std::cout << "I got "<< my_strings.size() << " strings." << std::endl;
for (auto& c : my_strings)
std::cout << c << std::endl;
#1
1
how can I know strdesc's array size
我怎么知道strdesc的数组大小
You cannot know the size of an array from a pointer to that array.
您无法从指向该数组的指针知道数组的大小。
What you can do is pass the size as another parameter. Or even better, use a vector instead.
您可以做的是将大小作为另一个参数传递。甚至更好,使用矢量代替。
but after i=2 the program crashes with a segmentation fault.
但是在i = 2之后,程序崩溃并出现分段错误。
Accessing beyond the array boundary has undefined behaviour.
超出数组边界的访问具有未定义的行为。
#2
1
With this Kind of code you will get memory leaks and other kind of C-style problems.
使用这种代码,您将获得内存泄漏和其他类型的C风格问题。
use vector:
使用矢量:
#include <vector>
#include <string>
#include <iostream>
...
std::vector<std::string> my_strings;
my_strings.push_back("Hello");
my_strings.push_back("World");
std::cout << "I got "<< my_strings.size() << " strings." << std::endl;
for (auto& c : my_strings)
std::cout << c << std::endl;