Best way to get length of const char * in c++ - c++

i know two way's to get length of const char *
const char * str = "Hello World !";
int Size = 0;
while (str[Size] != '\0') Size++;
and other way is very simple
const char * str = "Hello World !";
size_t Size = strlen(str);
but i don't want to use str lib functions like strlen and i think this function use my first way behavior too. because in the pc world when we want to count something we need to count each block's and there is no magic to get the length with one movement so i think the first way is the best option to get length of const char *. other way i think the first way is maybe too heavy for heavy string's. so im confused. which way is better and why other way is not ?

Let's inspect the assembly listing of these two methods.
#include <cstddef>
#include <cstring>
int string_size_1()
{
const char * str = "Hello World !";
int Size = 0;
while (str[Size] != '\0') Size++;
return Size;
}
int string_size_2()
{
const char * str = "Hello World !";
size_t Size = strlen(str);
return Size;
}
Using Clang 4.0.0 with flags -std=c++14 -O2
string_size_1(): # #string_size_1()
mov eax, 13
ret
string_size_2(): # #string_size_2()
mov eax, 13
ret
Link: https://godbolt.org/g/5S6VSZ
Both methods end up with exactly the same assembly listing. Also, the compiler optimizes away everything and just return a constant because the string literal is known during compile-time. So, in terms of performance, they are equally good.
But in terms of readability, strlen(str) is definitely better. A function call states the intention through the function name. A loop cannot do that.
Besides, std::string and std::string_view are more preferable than C-string in many cases. Consider them.

In this case the answer is known at compile time:
template <std::size_t S>
constexpr std::size_t string_length
(
char const (&)[S]
)
{
return S - 1;
}
usage:
std::cout << string_length("example") << std::endl;
For cases where the string is not a compile time constant use strlen if only the pointer to the string is available, std::distance if both pointers to the beginning and end are available, or .size() if your dealing with a std::string

3 years late but better late that never.
Short answer
#define length(array) ((sizeof(array)) / (sizeof(array[0])))
Long answer
So using sizeof(array) returns the size of the type of the array * the amount of elements. Knowing this, we could achieve this:
#define length(array) ((sizeof(array)) / (sizeof(array[0])))
and you would use it like:
type yourArray[] = {your, values};
length(yourArray); // returns length of yourArray
For example:
#include <stdlib.h>
#include <stdio.h>
#define length(array) ((sizeof(array)) / (sizeof(array[0])))
int main()
{
const char *myStrings[] = {"Foo", "Bar", "Hello, World!"}; // 3 elements
int myNums[] = {0, 1, 5, 7, 11037}; // 5 elements
char myChars[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}; // 7 elements
printf("Length of myStrings array: %lu\n", length(myStrings));
printf("Length of myNums array: %lu\n", length(myNums));
printf("Length of myChars array: %lu\n", length(myChars));
return 0;
/* Output:
Length of myStrings array: 3
Length of myNums array: 5
Length of myChars array: 7 */
}
I tested it and it also works with uninitialized arrays, probably because they contain garbage (from being uninitialized) from the same type. Integer uninitialized arrays contain random integer numbers and const char* uninitialized arrays contain (null) which is treated as a const char*.
Now, this only works with arrays on the stack. Pointers pointing to space reserved in the heap used as an array would give unexpected results. For example:
int *myNums = (int *)malloc(3 * sizeof(int)); // Space for 3 integers
printf("Length of myNums: %lu\n", length(myNums)); // Outputs 2 instead of 3
So be advised. Who uses arrays on the heap anyways so whatever.
Note: This is relevant to this question as it works with const char * as requested. Works with other types too.

Related

C++: Efficient way to turn part of char array to int

I have a char buffer that contains several integers one after the other without any breaks between them, where the offsets of each separate number are known in advance. For example "1234123123456", where from position [0] to [3] it would be the first number, [4] to [6] the second, and [7] to the end would be the third.
Is there any way to convert these different sections to ints without altering the buffer? Please no usage of std::string since it is inefficient for my purposes.
You can write a simple conversion function like this:
int convert_slice(const char *s, size_t a, size_t b) {
int val = 0;
while (a < b) {
val = val * 10 + s[a++] - '0';
}
return val;
}
int main(void) {
char buffer[] = "12345678901234567890";
printf("%d\n", convert_slice(buffer, 0, 4);
printf("%d\n", convert_slice(buffer, 4, 6);
printf("%d\n", convert_slice(buffer, 6, 11);
printf("%d\n", convert_slice(buffer, 11, 20);
return 0;
}
Will produce:
1234
56
78901
234567890
Note that I use 2 index arguments, the first is included and the second is excluded. This is more consistent with C and C++ practices. You could also use a more classic C++ API such as int convert_slice(const char *a, const char *b);
One clean way to do this without writing your own parsing code or making temporary copies is to use sscanf:
sscanf(src+start_offset, "%4d", &result);
If the length of the field (4 in the example) is not constant, though, you'll need either snprintf to construct a format string to pass to sscanf, or an array of possible format strings for the sizes you need.

How can I find the size of a (* char) array inside of a function?

I understand how to find the size using a string type array:
char * shuffleStrings(string theStrings[])
{
int sz = 0;
while(!theStrings[sz].empty())
{
sz++;
}
sz--;
printf("sz is %d\n", sz);
char * shuffled = new char[sz];
return shuffled;
}
One of my questions in the above example also is, why do I have to decrement the size by 1 to find the true number of elements in the array?
So if the code looked like this:
char * shuffleStrings(char * theStrings[])
{
//how can I find the size??
//I tried this and got a weird continuous block of printing
int i = 0;
while(!theStrings)
{
theStrings++;
i++;
}
printf("sz is %d\n", i);
char * shuffled = new char[i];
return shuffled;
}
You should not decrement the counter to get the real size, in the fist snippet. if you have two element and one empty element, the loop will end with value , which is correct.
In the second snippet, you work on a pointer to a pointr. So the while-condition should be *theStrings (supposing that a NULL pointer ist the marker for the end of your table.
Note that in both cases, if the table would not hold the marker for the end of table, you'd risk to go out of bounds. Why not work with vector<string> ? Then you could get the size without any loop, and would not risk to go out of bounds
What you are seeing here is the "termination" character in the string or '\0'
You can see this better when you use a char* array instead of a string.
Here is an example of a size calculator that I have made.
int getSize(const char* s)
{
unsigned int i = 0;
char x = ' ';
while ((x = s[i++]) != '\0');
return i - 1;
}
As you can see, the char* is terminated with a '\0' character to indicate the end of the string. That is the character that you are counting in your algorithm and that is why you are getting the extra character.
As to your second question, seem to want to create a new array with size of all of the strings.
To do this, you could calculate the length of each string and then add them together to create a new array.

go equivalents of c types

What are the right equivalent of unsigned char or unsigned char* in go? Or am I even doing this right?
I have this C++ class:
class ArcfourPRNG
{
public:
ArcfourPRNG();
void SetKey(unsigned char *pucKeyData, int iKeyLen);
void Reset();
unsigned char Rand();
private:
bool m_bInit;
unsigned char m_aucState0[256];
unsigned char m_aucState[256];
unsigned char m_ucI;
unsigned char m_ucJ;
unsigned char* m_pucState1;
unsigned char* m_pucState2;
unsigned char m_ucTemp;
};
I am trying to rewrite it to go:
type ArcfourPRNG struct {
m_bInit bool
m_aucState0 [256]byte
m_aucState [256]byte
m_ucI, m_ucJ []byte
*m_pucState1 []byte
*m_pucState2 []byte
m_ucTemp []byte
}
func (arc4 *ArcfourPRNG) SetKey(pucKeyData []byte, iKeyLen int) {
func (arc4 *ArcfourPRNG) Reset() {
func (arc4 *ArcfourPRNG) Rand() uint {
Well, I just started with go a few hours ago. So this is still confusing me.
A function
for(i=0; i<256; i++)
{
m_pucState1 = m_aucState0 + i;
m_ucJ += *m_pucState1 + *(pucKeyData+m_ucI);
m_pucState2 = m_aucState0 + m_ucJ;
//Swaping
m_ucTemp = *m_pucState1;
*m_pucState1 = *m_pucState2;
*m_pucState2 = m_ucTemp;
m_ucI = (m_ucI + 1) % iKeyLen;
}
memcpy(m_aucState, m_aucState0, 256); // copy(aucState[:], aucState0) ?
Hopefully this can clear a few things up for you.
For storing raw sequences of bytes, use a slice []byte. If you know exactly how long the sequence will be, you can specify that, e.g. [256]byte but you cannot resize it later.
While Go has pointers, it does not have pointer arithmetic. So you will need to use integers to index into your slices of bytes.
For storing single bytes, byte is sufficient; you don't want a slice of bytes. Where there are pointers in the C++ code used to point to specific locations in the array, you'll simply have an integer index value that selects one element of a slice.
Go strings are not simply sequences of bytes, they are sequences of UTF-8 characters stored internally as runes, which may have different lengths. So don't try to use strings for this algorithm.
To reimplement the algorithm shown, you do not need either pointers or pointer arithmetic at all. Instead of keeping pointers into the byte arrays as you would in C++, you'll use int indexes into the slices.
This is kind of hard to follow since it's virtually all pointer arithmetic. I would want to have a description of the algorithm handy while converting this (and since this is probably a well-known algorithm, that should not be hard to find). I'm not going to do the entire conversion for you, but I'll demonstrate with hopefully a simpler example. This prints each character of a string on a separate line.
C++:
unsigned char *data = "Hello World";
unsigned char *ptr = 0;
for (int i = 0; i < std::strlen(data); i++) {
ptr = i + data;
std::cout << *ptr << std::endl;
}
Go:
data := []byte("Hello World")
for i := 0; i < len(data); i++ {
// The pointer is redundant already
fmt.Println(data[i:i+1])
}
So, learn about Go slices, and when you do reimplement this algorithm you will likely find the code to be somewhat simpler, or at least easier to understand, than its C++ counterpart.

How to push char array? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I use arrays in C++?
I have no idea how to push a char array.
For example if i have "HI.MY.NAME.IS" in a char array,
I would like to put one char in the middle and push the char's right to it.
So it would be something like "HI.MY.SLAME.IS" or something like that.
Any possible solutions?
Use string::insert.
std::string s("test");
s.insert(s.begin()+2, 'c');
There is no "automatic" push which relocates elements in an array. At the lowest levels, there is only read from an array element and write to an array element.
That means you need to copy each element after the insert "one index" down the array. Then you need to set the "inserted" value at its inserted index.
Library routines will do this for you without you noticing; however, you should be aware of the underlying mechanism. Otherwise, you might fall under the impression that inserting into arrays at arbitrary indexes is cheap (when it usually isn't).
//This allocates new memory that you receive responsibility for freeing after use.
char *push(char *charArray, char *charsToPush, int pushPos) {
char *thePushed = new char[strlen(charArray) + strlen(charsToPush) + 1];
memcpy(thePushed, charArray, pushPos);
memcpy(thePushed + pushPos, charsToPush, strlen(charsToPush));
memcpy(thePushed + pushPos + strlen(charsToPush), charArray + pushPos, strlen(charArray) - pushPos);
thePushed[strlen(charArray) + strlen(charsToPush)] = '\0';
return thePushed;
}
To do that you'd have to:
Create a new array that is large enough to hold the original and the new items. Arrays cannot be resized.
Copy all items from the old to the new array, leaving the places for the new characters open.
Insert the new characters.
This is quite complex. But C++ offers a simpler way - std::string:
#include <string>
#include <iostream>
int main() {
std::string text = "HI.MY.NAME.IS";
std::string new_text = text.substr(0, 6) + "SL" + text.substr(7, 6);
std::cout << new_text << std::endl;
}
This program prints HI.MY.SLAME.IS. Or, even better, use insert, as #rasmus suggests.
If you are limited to c-strings or require it to be replaced in-buffer, then you can do something like the below, but your buffer must be large enough to hold the modified string since it's pushing all characters down. That said, you'd be much better off using the std::string as the others suggest.
// ASSUMES buffer is large enough to store one more char
void insertAt(char* buffer, char insertMe, size_t at)
{
size_t len = strlen(buffer);
if (at <= len)
{
memcpy(buffer + at + 1, buffer + at, len - at + 1);
buffer[at] = insertMe;
}
}
char* string = (char*) malloc(14);
string = "HI.MY.NAME.IS";
realloc(string, 15);
for (int i = 14; i > 5; --i) {
string[i+1] = string[i];
}
string[5] = 'S';
string[6] = 'L';
Here you go...lol

Is it possible to pass char[][] to a function requesting char**?

I am trying to call a function that takes char** as a parameter. Its job is to fill an array of strings (i.e. an array of char*). I know the max length of the strings, and I can pass the max number to fill as another parameter, so I was hoping to stack allocate it like this:
fill_my_strings(char** arr_str, int max_str); // function prototype
char fill_these[max_strings][max_chars_per_string]; // allocating chars
fill_my_strings(fill_these, max_strings); // please fill them!
Of course, I get the "cannot convert char[max_strings][max_chars_per_string] to char**" error.
I know this is some subtle (or not-so-subtle) problem with my understanding of the difference between arrays and pointers. I'm just not sure why it's not possible to pass this block of memory to something wanting a char** and have it fill in my stack-allocated chars. Could somebody please explain if this is possible, or if not, why not?
Is it possible to call a function like this without calling malloc / new?
The simple answer to your question is no; a two dimensional array is different than a pointer-to pointer type. Arrays decay to pointers to their first element, but pointers actually are that value.
The difference between these types is clear, if you cast both to char*
int x;
char *arr_pp[] = {"foo", "bar", "baz"};
char arr_2d[][4] = {"foo", "bar", "baz"};
char *cp = (char*)arr_pp;
for(x=0; x<3; x++)
printf("%d ", cp[x]);
printf("\n");
cp = (char*)arr_2d;
for(x=0; x<3; x++)
printf("%d ", cp[x]);
printf("\n");
The output (on my computer) is:
-80 -123 4
102 111 111
Where the first row is gibberish formed by the fact that I'm printing an address cast into bytes, and the second row is the ascii values of "foo".
In a function taking a char ** the compiler can't know to decay array types, which don't actually contain pointers.
Suppose you have n pointers to strings of m-1 maximum characters (m characters including the NULL).
So, in pure C:
sizeof(char[n][m]) will return n*m.
sizeof(char**) will return the size of a pointer in your architecture, probably 32 (if x86) or 64 (if x86_64).
char[n][m] actually allocates the n*m byte contiguously. char** allocates a single pointer. This pointer references a memory stripe of *n bytes. Each of these n pointers points to a memory stripe of m characters.
So, considering that sizeof(char) == u, if you declare char a[n][m], when you use a[i][j], the compiler understands *(a + i*m*u + j*u).
So, considering that sizeof(char *) == w, if you declare char **a, when you use a[i][j], the compiler understands ((a + i*w) + j*w).
Completely different data management.
The closes thing you could do to handle your special case is to create a char** variable, and populate it with the addresses of your stack allocated matrix.
char **tmp = malloc(max_strings * sizeof(char *));
int i;
for(i = 0; i < max_strings; i++){
tmp[i] = &(fill_these[i][0]); //you probably can't reference a char[][] with a single index - not confirmed
}
I am not sure why fill_my_strings() need a char** parameter. From your example, caller have already allocated the memory from stack. So using a char* should be OK.
But if you want to use char** or you can't modify the fill_my_strings() function, try following example code:
void fill_my_strings(char** arr_str, int max_chars_per_string, int max_strings)
{
for(int i = 0; i < max_strings; ++i)
{
//Make sure you have enough space
memcpy(*arr_str, "ABCD", sizeof("ABCD"));
*arr_str += max_chars_per_string;
}
}
char fill_these[max_strings][max_chars_per_string];
char* pointer = (char*)fill_these;
fill_my_strings(&pointer, max_strings, max_chars_per_string);
The obvious thing to do is build an index
In c use something like:
char string_list[num_strings][str_length];
// ...
char**index = calloc( (num_strings+1), sizeof(*index) ); // calloc insures NULL termination
for (int i=0; i<num_strings; ++i) {
index[i] = string_list[i]
}
In c++ prefer new[] to calloc;