| |

VerySource

 Forgot password?
 Register
Search
View: 659|Reply: 13

How does a function return an array of indefinite length?

[Copy link]

3

Threads

10

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-2-29 23:00:01
| Show all posts |Read mode
in is an input parameter
out is the output parameter
outlength is also an output parameter and is the length of the out array
void fun (unsigned char * in, unsigned char * out, int outlength)
{


}

Since I don't know the length of out, how do I pass out?
Reply

Use magic Report

0

Threads

78

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-5-10 12:00:01
| Show all posts
Just pass the pointer,
The length is given by outlength, and the outlength can be passed by reference.

void fun (unsigned char * in, unsigned char * out, int&outlength)
{
  .... // Assign * out and outlength here, you can pass the content out
}
Reply

Use magic Report

1

Threads

8

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-5-10 14:30:01
| Show all posts
#include <iostream>
using namespace std;
void fun (unsigned char * in, unsigned char * out, int * outlength)
// or yin yong
{
strcat ((char *) out, (char *) in);
strcat ((char *) out, "but add a tail !!");
* outlength = strlen ((char *) out);
}

void main ()
{
char _in [1024];
char _out [1024];
int len ​​= 0;
memset (_in, 0, sizeof (_in));
memset (_out, 0, sizeof (_out));
strcpy (_in, "thi is in buf !!");
fun ((unsigned char *) _ in, (unsigned char *) _ out,&len);
cout << _ in << endl;
cout << _ out << endl;
cout << len << endl;
system ("pause");
}
You can't pass the length of this function definition!
Reply

Use magic Report

0

Threads

25

Posts

19.00

Credits

Newbie

Rank: 1

Credits
19.00

 China

Post time: 2020-5-10 15:00:01
| Show all posts
Change to this

void fun (unsigned char * in, unsigned char ** out, int * outlength)
{
  * outlength = 200;
  * out = malloc (outlength + 1);

}

char * dest = NULL;
char * in;
int len;
fun (in,&out,&len)
Reply

Use magic Report

0

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 Invalid IP Address

Post time: 2020-5-10 20:00:01
| Show all posts
void fun (unsigned char * in, char ** out, int&outlength)
{
* out = new char [10];
strcpy (* out, "aaa");
outlength = 10;
}
int main ()
{
char * p;
int n;
fun (NULL,&p, n);

delete [] p;
return 0;
}
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 Invalid IP Address

Post time: 2020-5-10 21:30:01
| Show all posts
Is it bad to pass a vector object out?
Reply

Use magic Report

0

Threads

14

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-5-11 10:15:02
| Show all posts
vector <basic_string <unsigned char>> fun (const vector <basic_string <unsigned char>>&in)
{


}
Reply

Use magic Report

0

Threads

78

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-5-11 13:30:01
| Show all posts
such as:

#include <cstring>
#include <cstdlib>
#include <iostream>

using namespace std;
void fun (unsigned char * in, unsigned char *&out, int&outlength) / * The parameters are all quoted * /
{
  outlength = strlen ((char *) in) +1;
  out = new unsigned char [outlength];
  strcpy ((char *) out, (char *) in);
}

int main ()
{
unsigned char src [] = "Hello World";
unsigned char * dest = NULL;
int len;
The
fun (src, dest, len);
cout << "Length is" << len << endl << dest;
free (dest);
cout << endl << endl;
The
strcpy ((char *) src, "GoodBye");
fun (src, dest, len);
cout << "Length is" << len << endl << dest;
free (dest);
cout << endl << endl;
   
    system ("pause");
    return 0;
}
Reply

Use magic Report

0

Threads

8

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-5-14 12:45:01
| Show all posts
In addition to the method of allocating memory inside the function posted by LS, another idea is to allocate the memory area outside the function (that is, before the function call):
1. Allocate a memory area of ​​SIZE size before the function call;
2. Pass the address and size of the memory buffer as parameters into the function, and the memory size used in the function as the return value, so the prototype of the function can be like this:
  int foobar (void * in, void * out, int buffersize);
3. Inside the function we know how much memory we need:
  If buffersize> = the required memory size, fill the result into the buffer;
  If buffersize <the required memory size, the buffer remains unchanged, and the required memory size is returned as the return value;
4. Judge whether the operation is successful according to the transformation of the buffer. If it is unsuccessful, use the return value (the size of the memory required) to reallocate a sufficiently large memory area and call this function again.
5. After using the buffer, release the buffer

Compared with the function of allocating memory outside the function, the problem of who releases the memory allocated inside the function.
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-5-15 12:00:01
| Show all posts
In this way, the return value type will be problematic:
vector <basic_string <unsigned char>> fun (const vector <basic_string <unsigned char>>&in)
{

}


This is almost the case, but it is not recommended for internal distribution and external use.
void fun (unsigned char * in, unsigned char ** out, int * outlength)
{
  * outlength = 200;
  * out = malloc (outlength + 1);

}
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list