so guys, i was thinking about finding a way to create arrays, without knowing their size.

for example

create an app that takes unsigned long values until EOF and just saves them to an unknown size array.

i was thinking that, because the array size could be big, to create the array using dynamic memory allocation.

so here is my code which is NOT correct thats why i need your help

#include <stdio.h>
#include <stdlib.h>
int main(){
    unsigned long n,count=0,*array,size=1,i;
    while((scanf("%ul",&n))!=EOF){
         count=count+1;
         array =(long*)malloc(count*sizeof(long));
         array[count] = n;
         if (count>size){
                         size=count;
                         }
                         if (n==5){
                                  break;
                                  }
                                  
           }
           for (i=1;i<=size;i++){
               printf("%ul\n",array[i]);
               }
           free(array);
    return 0;
}

my first problem is that, when the app takes the forth value for variable un it just sticks, and does nothing.

also when i try for 3 numbers, it doesnt print them in the end correctly it prints numbers like 2938928329 etc

i would like some help to achieve that, dont tell me other ways, i know there are other ways.

thanks in advance :)

jephthah commented: "dont tell me other ways" WTF?? -2

1-tip:
Each time you are calling malloc, it's giving you totally different location, so old values that you have stored are left in old location. Try realloc.

if (count>size){
                         size=count;
                         }

What's the purpos of the above code?

You've got a massive memory leak. Each time the loop iterates, you replace your pointer with a new block of memory. I imagine you wanted realloc instead of malloc:

#include <stdio.h>
#include <stdlib.h>

#define CHUNKSIZE 5

int main ( void )
{
    unsigned long *array = 0;
    size_t capacity = 0;
    size_t n = 0;
    unsigned long value;
    size_t i;

    while ( scanf ( "%lu", &value ) == 1 ) {
        if ( n == capacity ) {
            unsigned long *save;

            capacity += CHUNKSIZE;
            save = realloc ( array, capacity * sizeof *array );

            if ( save == NULL )
                break;

            array = save;
        }

        array[n++] = value;
    }

    for ( i = 0; i < n; i++ )
        printf ( "%lu\n", array[i] );

    free ( array );

    return 0;
}

>dont tell me other ways, i know there are other ways.
That's pretty rude, dude. We're only trying to help you, and if your code for this way is so messed up, you probably don't understand the other ways either. Why are you so against learning?

@Neokyrgyz
i first used this program for some other purpose, just forgot it there :P

@Narue
you misunderstood me,

i know there is a way of making that work by allocating memory on stack, which is simpler, but you cant do that for large arrays.

i actually started learning how to dynamic allocate memory few weeks ago and im pretty new to it, thats why i wanted only this method, i really was stupid to say "dont show me other ways" it would be better just to say "show me ways of doing this by making only dynamic memory allocation", sorry for not making that clear

thank you all, now i understand how to do this :)

>i know there is a way of making that work by allocating memory
>on stack, which is simpler, but you cant do that for large arrays.
That's not one of the ways I was thinking about, because your requirements included variable unknown length. In particular, I was thinking of a linked list, especially since you don't seem to need anything but sequential access. It's actually easier to keep track of linked nodes than it is to maintain capacity and current size for a dynamic array:

#include <stdio.h>
#include <stdlib.h>

int main ( void )
{
    struct node {
        unsigned long value;
        struct node *next;
    };
    
    struct node *head = NULL;
    struct node *temp;
    unsigned long value;

    while ( scanf ( "%lu", &value ) == 1 ) {
        temp = malloc ( sizeof *temp );

        if ( temp == NULL )
            break;

        temp->value = value;
        temp->next = head;
        head = temp;
    }

    while ( head != NULL ) {
        temp = head->next;
        printf ( "%lu\n", head->value );
        free ( head );
        head = temp;
    }

    return 0;
}

Of course, that may just be me, as I'm a data structure junkie.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.