#Dynamic memory, Realloc in question,

1 messages · Page 1 of 1 (latest)

keen quest
#

messing around with it, just noticed, i have part of a code here saying c arr = (int*)realloc(arr, (size + part) * sizeof(int));
that should mean, the array size should increase by part, Right? what i get here, if just the array becomes the size of part, ill show the full code, but it is pretty long and scuffed.

sick jettyBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

keen quest
#

just ping me if you reply ^^

slate acorn
#

Just paste the relevant parts where at least all the variables are defined.

keen quest
ripe holly
#

@keen quest Didn't understand your question clearly

keen quest
#

ok so uh

#

basically arr is a pointer

#

that is already allocated

#

and i want to ADD more slots to it

#

so uhm

#

when i do something like that

#

size is known and part is input btw!

#

so say my length is 3, and i enter 2, it makes my array length 2 somehow

ripe holly
#

No, that is not right, because, if you enter 2, then your size will be 3+2 =5

#

@keen quest

keen quest
#

thats what im saying

#

i dont know why its happening

ripe holly
#

Ok so now i got your question @keen quest

keen quest
#

yeah, my bad. i didnt word it correctly

ripe holly
#

You want to make the array length equal to the inpt

ripe holly
#

@keen quest

keen quest
#

no

#

not equal

#

but add it to the length

ripe holly
#

Ok got it, it should be total of both, but it is 2 instead of 5

#

But how did you know that it has became 2 and not 5?

#

@keen quest Can you show me where you have declared and defined size variable?

keen quest
#

uhh

#

wait

#

its basically

#

i have a list of functions to make

#

uhh

#

wait

#

one of them is print values

#

basically

#

and just i see all the values get removed

#

so its easy to tell

#

@ripe holly

ripe holly
#

@keen quest Ohk

keen quest
#

so uhm, what can i do about it?

ripe holly
#

@keen quest Possible reason is, value of size can be 0 due to some reason, print size before you reallocate the memory and check if size is 0 or not

#

@keen quest Also, i didn't get that how did you know that it has became 2 instead of 5?

keen quest
#

because

#

i have a command that prints all values

#

meaning i see 5 values, after the change i get only 2 values

slate acorn
#

Do you always print the values from the most recent return value of realloc?

#

Remember that realloc isn't guaranteed to return the same pointer value you give it.

sick jettyBOT
#

This question is being automatically marked as stale.
If your question has been answered, run !solved.
If your question is not answered feel free to bump the post or re-ask.
Take a look at !howto ask for tips on improving your question.

keen quest
keen quest
#

oh ive solved it, thank you though. you gave me a nice hint

#

!solved