Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Dynamic allocation of array as the user types the string.

Dynamic allocation of array as the user types the string.

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AhtiA Offline
    AhtiA Offline
    Ahti
    wrote on last edited by Ahti
    #1
    This post is deleted!
    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by sierdzio
      #2

      @Ahti said in Dynamic allocation of array as the user types the string.:

      and my english is bad ( which they consider a mistake as well lol )

      English is the basic tool of software development. All languages and all docs are written primarily in English. I don't think it is wise to try to turn it into a joke. At the same time, it's worth to remember that for most programmers, English is a secondary language, so it's silly to consider that as a mistake. Everybody should have a chance to learn, after all.

      OK, sorry for that digression. On to the topic:

      the above program doesn't work why ?

      How exactly does it fail? Crash? Username is empty at the end? It skips some chars? Fails to print the result (you have no final printf after the loop is done)?

      Assuming the problem is with the reallocation itself: perhaps I should not mention it, but it is a very good idea to use some safe and ready-made components for such tasks. For example, containers like std::vector, or if you want to go the Qt way, the QVector. It will handle the memory automatically for you, and make it in a fast and efficient manner.

      If you still want to go this way, however, here are my notes:

      • I have not run your code, and quite frankly I've not used calloc/realloc for a looong time, so I'll be guessing a bit :) Take my hints with a grain of salt please
      • calloc() requires 2 parameters, but you provide only one
      • more crucially, realloc() will return a null pointer (or dereference the memory!) if size parameter is empty. It's not the case in your code, but something to watch out for
      • this code may "break" on UTF-8 characters
      • you never allocate memory for more than 1 character (always only for sizeof(char*)). If you want to get a longer array, allocate for more characters: length * sizeof(char*) (assuming you increment length first)

      I suspect the last hint is what you need to do to make it work. Good luck!

      (Z(:^

      1 Reply Last reply
      1
      • S Offline
        S Offline
        Sunfluxgames
        wrote on last edited by
        #3

        your issue is the char is only 1 and your gonna keep relocating 1 char each time.

        me personally i would use a for loop

        for(char *username  = 0; username < 15; username ++)
        // if size is less than 3 return else if size greater than 15 return
        // getche and calloc and length realloc it..
        
        
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved