C variant of vector?

This is a discussion on C variant of vector? within the C Programming forums, part of the General Programming Boards category; Hi! if you use scanf("%as", &s); memory will be allocated to store just as many characters plus one null character ...

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    C variant of vector?

    Hi!

    if you use scanf("%as", &s); memory will be allocated to store just as many characters plus one null character as the string contains. But how do scanf know how many characters the string contains? Or does scanf extend the allocated memory in the meantime?

    Ok, here is the problem I want to solve: The user shall enter an arbitrary number of bytes (in integer or hexadecimal form) to the program, and they shall all end up in an array big enough to store them all. When the user has entered all the bytes he will press some special key, like f for finished. Is it possible to make them all end up in an array without first storing them in a linked list? Note that there's no way to tell how many or how few bytes will be entered.
    Come on, you can do it! b( ~_')

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > if you use scanf("%as", &s); memory will be allocated to store just as many characters plus one null character as the string contains.
    Well there's your first problem, no it doesn't.
    What it will do is overwrite memory you don't own without warning. Perhaps you're interpreting the lack of a crash as success.


    Whatever it is you're reading, you need to read it in fixed sized blocks and use a linked list, or use realloc to extend the space available.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by TriKri View Post
    Hi!

    if you use scanf("%as", &s); memory will be allocated to store just as many characters plus one null character as the string contains. But how do scanf know how many characters the string contains? Or does scanf extend the allocated memory in the meantime?
    This is a GNU extension, and can lead you into trouble. %a reads in a floating-point number (according to the C99 standard, anyway), so if you decide to add in -ansi, or -std=c99, or something, it will stop working.

    Quote Originally Posted by TriKri View Post
    Ok, here is the problem I want to solve: The user shall enter an arbitrary number of bytes (in integer or hexadecimal form) to the program, and they shall all end up in an array big enough to store them all. When the user has entered all the bytes he will press some special key, like f for finished. Is it possible to make them all end up in an array without first storing them in a linked list? Note that there's no way to tell how many or how few bytes will be entered.
    You'll have to store them somewhere; if you feel like taking the performance penalty, you can write them to a temporary file while counting bytes, and then read back from that file. I suppose the question is really "what are you trying to do?" If its something you can achieve with buffered input, then go for that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21