Malloc string

Hello i need allocate memory block in my string and then free memory but I don't know how use it correctly.
1
2
3
4
5
6
7
8
    string retez1, retez2;
    unsigned int velikost = 0;

    char * vysledek;
    if (retez1.size() > retez2.size())
        velikost = retez1.size();
    else velikost = retez2.size();
     vysledek = (char*)malloc(velikost+1);


How can i allocate memory for string retez1,retez2 and vysledek ?
It should be size of string I enter as input(retez1,retez2) and (vysledek) represent result of retez1+retez2.
retez1 and retez2 can be long aroud 16000 sings. So what im doing wrong ?
Why not new? It is C++, not C right?
vysledek = new char[velikost+1];
How can i allocate memory for string retez1,retez2 and vysledek ?

you don't need to allocate memory to retez1 and retez2 since they are std::strings, memory is automatically managed by the string class.

vysledek on the other hand is a pointer to char which should be dynamically allocated to store a string.

So what im doing wrong ?


instead of doing all those memory allocations, you can just declare vysledek as string also, then copy value of retez1, and 2 using operator+ to the newly created one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str1 = "Hello";
    string str2 = "World";
    
    string result = str1 + " " + str2;
    
    cout << result;
   
   return 0;
}


will output :

Hello World
JockX can you post whole code ? If i use this i have typical problem with output and result is empy.

shadow fiend i can't use this because str1 and str2 is binary number (100101 1001010) and i need make sum of this and this is result. If i use this program crash.
1
2
3
4
5
6
7
8
9
10
11
string retez1, retez2;
    unsigned int velikost = retez1.size() + retez2.size () +1;

    char * vysledek;
     vysledek = (char*)malloc(velikost);
  strcpy (vysledek, retez1.c_str());
strcpy (vysledek+retez1.size(), retez2.c_str());

//print it to screen or w/e u want

free (vysledek);
well, i don't know what is wrong :( sometimes i got right output, sometimes i got this
Input: 01010111 01010101
Output: 10101100BC0↓-"


and sometimes program crash...

You should enter two binary numbers in one line divided by white space and result should be sum of this two binary numbers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>
#include <string>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main()
{
   /*string retez1, retez2;
    unsigned int velikost = 0;
    string vysledek = "----------------";
    char * vysledek;
    if (retez1.size() > retez2.size())
    velikost = retez1.size();
    else velikost = retez2.size();
    vysledek = (char*)malloc(velikost+1); */
  /*string len="        ";
    string vysledek(len, 0);*/

    string retez1, retez2;
    unsigned int velikost = retez1.size() + retez2.size () +1;

    char * vysledek;
    vysledek = (char*)malloc(velikost);
    strcpy (vysledek, retez1.c_str());
    strcpy (vysledek+retez1.size(), retez2.c_str());



    char carry = '0';


    bool tester = true;


    /* cin >> retez1 >> retez2; */

    cin >> retez1;
    getline(cin, retez2);

        for(int i = 0; i < retez1.length() ; i++)
                {
                    if(retez1.at(i)!= '1' && retez1.at(i)!= '0')
                    {
                       tester = false;

                    }
                }
    if (tester == false)                // testovani jestli je retez1 okay
        {
        cout << "Nespravny vstup." << '\n';
        return 0;
        }


    if (retez2.find(' ') != string::npos) {
    cout << "found!" << '\n';
    retez2.erase (retez2.begin());

     for(int i = 0; i < retez2.length() ; i++)
                {
                    if(retez2.at(i)!= '1' && retez2.at(i)!= '0')
                    {
                       tester = false;

                    }
                }
    if (tester == false)                // testovani jestli je retez2 okay
        {
        cout << "Nespravny vstup." << '\n';
        return 0;
        }
}


    while (retez1.size() > retez2.size())
    {
    retez2 = "0" + retez2;
    }
    while (retez1.size() < retez2.size())
    {
    retez1 = "0" + retez1;
    }

/*
    tester = false; //neni zadna 1cka
    if (retez1.find("1") && retez2.find("1"))
    {
    tester = true;
    }

    if(tester == true){

    soucet = 0;
    cout << "Soucet2: " << soucet << '\n';
    return 0;
    }
*/
    // VYPOCET:

      for (int i = retez1.length() - 1; i >= 0; i--)
    {
                    if (carry == '0')
                    {
                                    if (retez1[i] == '0' && retez2[i] == '0')
                                    {
                                        vysledek[i] = '0';
                                        carry       = '0';
                                    }
                                    else if (retez1[i] == '1' && retez2[i] == '0')
                                    {
                                        vysledek[i] = '1';
                                        carry       = '0';
                                    }
                                    else if (retez1[i] == '0' && retez2[i] == '1')
                                    {
                                        vysledek[i] = '1';
                                        carry       = '0';
                                    }
                                    else if (retez1[i] == '1' && retez2[i] == '1')
                                    {
                                        vysledek[i] = '0';
                                        carry       = '1';
                                    }
                    }
                    else
                    {
                                    if (retez1[i] == '0' && retez2[i] == '0')
                                    {
                                        vysledek[i] = '1';
                                        carry       = '0';
                                    }
                                    else if (retez1[i] == '1' && retez2[i] == '0')
                                    {
                                        vysledek[i] = '0';
                                        carry       = '1';
                                    }
                                    else if (retez1[i] == '0' && retez2[i] == '1')
                                    {
                                        vysledek[i] = '0';
                                        carry       = '1';
                                    }
                                    else if (retez1[i] == '1' && retez2[i] == '1')
                                    {
                                        vysledek[i] = '1';
                                        carry       = '1';
                                    }
                    }
    }
if (carry == '1')
    {
    vysledek = '1' + vysledek;
    }




/*
tester = false; //neni zadna 1cka
    if (vysledek.find("1"))
    {
    tester = true;
    }

    if(tester == true){

    soucet = 0;
    cout << "Soucet2: " << soucet << '\n';
    return 0;
    }
*/
    cout << "Soucet: " << vysledek << '\n';
    free (vysledek);
    return 0;

}
Last edited on
it working fine when i have something like this:
string retez1, retez2;
string vysledek = "----------------";

Input: 101 111
Output: 1100------------


but i can't use this because sometimes are inputs 5 sings long and sometimes 100 sings long..
There is a problem with the order in which events are happening. It doesn't make sense to set the size of vysledek at line 23
unsigned int velikost = retez1.size() + retez2.size () +1;

... because the user will enter the input strings afterwards, at line 40.

Instead, insert at line 86:
 
    string vysledek(retez1.size(), '0');


If you really wanted to use a character array instead of a std::string, then you could do this - but it's more complicated:
 
    char * vysledek = new char[ retez1.size() + 2];


It needs to be 2 bytes longer than the longest input string (a) to allow for null terminator and (b) to allow for possible carry from adding the final pair of digits.

By the way, in the validation of correct input, you may find this built-in function very useful:
http://www.cplusplus.com/reference/string/string/find_first_not_of/

1
2
3
4
5
    if (retez1.find_first_not_of("01") != string::npos)                
    {
        cout << "Incorrect input." << '\n';
        return 0;
    }

Last edited on
thank you, once again :) last thing i must check is if i have inputs like 00000001 and 000000001.
my output is 000000010 but i should get only "10" or when i get 00000000 000000 i should get output "0" and not 0000000000

edit: I got it :)
Last edited on
Topic archived. No new replies allowed.