Find the answer to your Linux question:
Results 1 to 4 of 4
Dear Friends, I have a text file from which i read a number of names with their lengths at the run-time. Now i want to created a char array having ...
  1. #1
    Just Joined!
    Join Date
    Oct 2008
    Posts
    9

    Array Creation at run-time in c++/Linux

    Dear Friends,

    I have a text file from which i read a number of names with their lengths at the run-time. Now i want to created a char array having the length and name as already read from the text file at the run-time. There is no compilation involved. Every thing is happening at the run-time. I tried using STL like map along with malloc but i am unable to name an array at run-time. I can keep some type of mapping with previously created arrays , but that would be very inefficient implementation.

    Please help.

    Regards,
    Raghuvendra Kumar

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    So, you want to have a named variable using the name you got at runtime? Sorry, but not possible. As you surmised, you can use some sort of map to hold and lookup that information. As for inefficient, generally it isn't inefficient to use a map, or hashmap to look up names since it uses a binary or hash search which are very efficient in searching large data sets.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Apr 2010
    Posts
    5
    I think you can also try to use double pointer?
    like
    char **names;
    read your number of arrays want to initialize
    names = malloc(sizeof(char*) * number_of_arrays);
    and then individually malloc each array.

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Quote Originally Posted by rajtendulkar View Post
    I think you can also try to use double pointer?
    like
    char **names;
    read your number of arrays want to initialize
    names = malloc(sizeof(char*) * number_of_arrays);
    and then individually malloc each array.
    That is doable, and not uncommon for dynamically created lookup tables. Usually you would sort the array so you could use something like bsearch() for fast lookups in the array.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...