Thread: size of the char arry

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    90

    size of the char arry

    i have to store a data more than 100000.
    i don't know the size of the data whether it may be 100000 or 1000000.
    so how can i define variable size;
    example
    char abc[1000000];
    but i don't know the size so how can i give array size??
    thank u
    sree

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    That's a large array for the stack, consider putting it on the heap so to speak.
    Code:
    /* a pointer pointing at nothing */
    char * abc = NULL;
    size_t iDunnoTheSizeVar = 1000000;
    
    if((abc = malloc(iDunnoTheSizeVar)) == NULL)
    {
        /* error */
    }
    
    /* use as a normal array or whatever */
    
    free(abc);
    abc = NULL;
    abc is a pointer to an array of iDunnoTheSizeVar characters on the heap.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    90
    but if is more than 1000000
    then wht should i do

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Two potential solutions:
    1. Make sure you use a size MUCH larger than you could possibly ever need. Drawback is that it wastes memory, and sooner or later someone _WILL_ find a case where this is not enough!
    2. Use some form of dynamic memory management, using for example realloc() or storing data in blocks, allocating blocks as needed, that you then combine later on in some way [e.g. a linked list of 1024 bytes each, or some such].

    If you can calculate how much space you actually need before you need it, then you could also use dynamically allocated memory using malloc().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    >but if is more than 1000000
    >then wht should i do

    Resize abc with realloc(). I think I've missed the point... read matsp's post.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    51
    yea, im pretty sure you dont have to define the size of your array if you use a heap.. it can adjust to the size of what ever you put into your array using dynamic memory of up to 2megs... so yea.. definatly go the heap route i would tell you more but i have only just learnt this myself best to have alot on the rest of the forums for more info on how to use a heaps.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > you dont have to define the size of your array if you use a heap
    You still do, you have to ask for x amount of bytes on the heap.

    > dynamic memory of up to 2megs
    No, it's limited by the amount of memory the OS could give you (on 32bit systems this is upto 4GB), on your system it would be physical memory + swap.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Generic heapsort
    By Sephiroth1109 in forum C Programming
    Replies: 15
    Last Post: 12-07-2007, 06:14 PM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM