0

so here's the thing I've been working on a code for 5 hours now and logic looks good the program seems to do it's job, the only thing that keeps bugging me is dynamic memory allocation of strings. The question does not specify the initial number of strings user has to enter. Here's what I've been trying to do to dynamically take the strings :

      int t;
      cin>>t  //number of strings user wishes enter 
      char *s1[1000009];  //1000009 is the maximum number of digits each string can have
      for(i=0;i<t;i++)
      {
                s1[i]=(char *)malloc(1000009) 
                cin>>s1[i];
      }

not sure if it is the right way or not. A way through which i could store a 2D character array would also do if not dynamic strings.

Thanking you, gaurav

3
  • 5
    I think you should use std::vector and std::string. Commented Nov 5, 2015 at 15:58
  • 4
    Don't use malloc for manual dynamic allocation. Use new instead. Don't use manual dynamic allocation at all. Use std::string instead. Commented Nov 5, 2015 at 15:59
  • Creating array of 1000009 elements on stack is a bad idea. Commented Nov 5, 2015 at 16:04

3 Answers 3

5

Use a vector of strings instead of using malloc/new.

int t;
std::cin >> t;
std::vector<std::string> strings(t); //Create t strings
for(int i = 0; i < t; i++)
    std::cin >> strings[i]; //Read into each string
Sign up to request clarification or add additional context in comments.

4 Comments

sir that works like a charm thank you, clearly I've got a lot to learn on vectors !
well apparently it did work for the first time now it's just depicting runtime error which says : terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
Make sure that t is valid. std::vector takes an unsigned value for the size so if t is negative the unsigned value will be very large.
pretty sure t is never negative :3
1

Since you tagged this question as C++ I would do it like this:

std::vector<std::string> s1(1000009);

That's all - no need to use malloc, no need to take care about destruction. If you are using C++ use the Tools you have available.

Comments

0

i would do it like this :

int i = 0;
char **s1;
s1 = (char **)malloc(t * sizeof(char *));
while (i < t)
{
 s1[i] = (char *)malloc(1000009 * sizeof(char));
 i++;
}

the first malloc create you t line. The second one fill alloc 100000009charactere for each lines

5 Comments

but as said above, a vector of std::string would be more efficiency.
as he said doesn't compile, and I've still got no clue how vectors work any way using malloc or new would be great
std::string test; std::vector<std::string> vec; int t; int i = 0; std::cin >> t; while(i < t) { std::cin>> test; i++; vec.push_back(test); test.clear(); }</code>
@ThomasGrockowiak yes works fine using vectors Thank You !
sorry to bother you again, the code seems to work fine but it takes a lot of time here take a look, link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.