Member Avatar for SalicBlu3

Hi guys,

I'm not sure how to enter new entries into an array that is full.

scanf() allows us to input a number.
array[i] allows us to put the number into the array

The problem with int array[x] is that you have to define the number of values in the array.

What I'm hoping for is to input a new number and place it at the end to make it an array
with size x+1.

So the user increases the number of values as they enter successive values.

I've had a look at dynamic arrays and it has something to do with malloc or realloc.
But I don't really understand it.

I'm hoping someone could lend a hand and explain it for me.

Thank you heaps! (in advance)
~SB

There's already lots of explanations floating around the web about malloc and realloc and how to properly use them
Example
You need to to be more specific, which concept are you having troubles with so we can help clarify some things?

The only way u can make space for new data while the user input, i.e simultaneously is through dynamic allocation... U can look up the functions 'new' and 'delete' which are used for allocating memory dynamically and releasing dynamically alloted memory respectively..

I would opt for dynamically allocating the variable. Then you can "realloc" as you need. Let us know how you get on.

malloc new bigger size, copy all data there :) free old memmory and make old pointer to point to new memmory i think this is the concept

Member Avatar for SalicBlu3

Sorry for being so vague guys.
I'll try to flesh it out a bit more.
I'm really new to computing so don't know what key words to use.

The end result looks like this:

Input number: 34
array[0] = 34

Input number: 23
array[0] = 34
array[1] = 23

...
and it keeps going until your desired break loop condition.

post your code we will comment

The end result looks like this:

Input number: 34
array[0] = 34

Input number: 23
array[0] = 34
array[1] = 23

...
and it keeps going until your desired break loop condition.

That seems like a situation where a linked list is better suited than a dynamic array.

Why not just create array as a large array? Say int array[1000]?

The concept of increasing the array size by one for each and every value entered is so resource heavy that I don't really think a useful solution.

Why not just create array as a large array? Say int array[1000]?

While this would work if this is just homework, it's not a good idea in general, as it creates a huge security hole in the application. Here's an interesting thread I found regarding that matter.

The concept of increasing the array size by one for each and every value entered is so resource heavy that I don't really think a useful solution.

It's not that bad, actually. If the user enters 10000 integers before (s)he stops, the total resizing and copying process would take less than 100 milliseconds. But, of course, it's not the wisest thing to do.

In my opinion, the best thing you can do is emulate the inner workings of a std::vector. That is, create an dynamic array of, let's say, 100 elements, at first. If the user enters more than 100 integers, resize that array to 200 elements (using the functions mentioned above -- no, you can't escape that). If the user enters more than 200 elements, resize your array to hold 400 elements, etc... Once the user decides to stop, you can optionally resize your array one last time, if saving memory is really that important.

Member Avatar for SalicBlu3

@Master Roshi,
Thank you for telling me the problems with this.
I guess I was trying to optimize from using too much memory.
That method of memory management is very interesting and I will definitely look into that.

@Sokurenko, @zeroliken,
Thanks guys. I was trying not to bog you guys down with everything in the problem.
I was trying to make an array that stored the numbers that were entered by the user and sorts them in ascending order.

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


int main(){
    int userInput;
    int array[10];
    int i;
    int currentCount=0;

//initialize array
    for (i=0;i<10;i++){
        array[i]=0;
    }

//Keep gettin numbers until user types in a number < 0
    while(userInput>0){

    //get number from user
        scanf("%d",&userInput);

    //store number in array
        array[currentCount]=userInput;
    }

//Sort the array of numbers

    return 0;
}

@deceptikon: Thanks. I'm not sure what a linked list is, but I will check it out.
@WaltP: I was thinking about that, but when I sort it, I will have to deal with all the zeros at the front. Also, I've got another project which i might need to make a 1,000,000 sized int array and the amount of memory (or something else) crashes my terminal when using codeblocks IDE.

Really appreciate the help.
I will keep researching on Master Roshi's method and see if I can nut it out. Else, I'll just stick with using a large array for now.

dont haveto deal with zeroes in the front, you must sort only length with needed data, no need to sort whole array
just hold variable iLen look at bubble sort
if it crashes then malloc array dynamically at the start

While this would work if this is just homework, it's not a good idea in general, as it creates a huge security hole in the application

How does this create a huge security hole and reallocating the array by 1 for each input doesn't? Keep in mind the info we have been given by the OP, not making unfounded assumptions about what we haven't been told...

@WaltP: I was thinking about that, but when I sort it, I will have to deal with all the zeros at the front.

Why? If you read in currentCount numbers, why would you sort the entire array? Just sort the currentCount values entered...

Also, I've got another project which i might need to make a 1,000,000 sized int array and the amount of memory (or something else) crashes my terminal when using codeblocks IDE.

This is what malloc() is used for. Large arrays that won't fit on the stack.

How does this create a huge security hole and reallocating the array by 1 for each input doesn't? Keep in mind the info we have been given by the OP, not making unfounded assumptions about what we haven't been told..

maybe if you check for array overflow and realloc error then no holes

commented: Yes, if you program properly, there will be no holes. +14

How does this create a huge security hole and reallocating the array by 1 for each input doesn't?

The answer to the first part of your question is provided in the link I posted above. The answer to the second part of your question was provided by Sokurenko.

Keep in mind the info we have been given by the OP, not making unfounded assumptions about what we haven't been told...

I'm not sure I understand what you mean here. Either this is homework or it's not. I cover both cases in my post: If it's homework, a large static array is fine. If it's not homework, a large static array is a problem. You're the one who's making assumptions, by ignoring the fact that this may not be homework. Unless you mean something else.

The answer to the first part of your question is provided in the link I posted above.

No it isn't. All that's provided in the link is a possible way to halt the system given the circumstanses set forth in the problem definition. It has nothing to do at all with the problem stated here.

The answer to the second part of your question was provided by Sokurenko.

Really? All he said was program properly and you won't have a problem. Where have I suggested not programming properly?

'm not sure I understand what you mean here. Either this is homework or it's not.

Being homework or not is immaterial, unless you are saying there are homework rules and non-homework rules.

I am making no assumtions. Your assumtion was how the input is being made as shown by linking to that article. I have no idea how the input is designed therefore have not commented on how the input is going to be dangerous with a large array vs. malloc/realloc array. AAMOF, the dynamic memory situation would be identical even in the link you posted. Try it...

Your assumtion was how the input is being made as shown by linking to that article.

It's not an assumption. The OP mentioned scanf and (s)he also provided an example of how the input is made in his/her program.

Where have I suggested not programming properly?

The OP provided the way (s)he gets input and you suggested a large static array as a solution. This implies one or more of the following:

(1) You didn't know the way the OP gets input (perhaps because you didn't bother to read the other posts).
(2) You didn't know that this combination of input - storing strategy could lead to a problem.
(3) You knew that this combination of input - storing strategy could lead to a problem, but you didn't want to tell the OP (either because you didn't think it's important or because you didn't want the OP to know).

In the end, either you didn't notice there was a problem or you suggested a bad programming practice on purpose. Which one is it?

the dynamic memory situation would be identical even in the link you posted

Not quite. Corrupting the heap in an exploitable way is different than corrupting the stack in an exploitable way (e.g. AFAIK, you can't directly modify the instruction pointer by corrupting the heap. All you can do is mess up malloc's internal structures).

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.