is that valid to use (int*)calloc(size-1,2)?

is that valid to use (int*)calloc(size-1,2)?



I am making a program where user can edit array.
I am new in programming.code is not finished yet.



Can I use calloc(size-1,2)? Is that valid ?
Does it creates array size-1?


calloc(size-1,2)


size-1


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

int main()


int *pointer1,*pointer2,size,i,choice,j=0,index;

printf("Enter Elements of Arrays " );
scanf("%d",&size);

pointer1=(int*)calloc(size,2);

for(i=0;i!=size;i++)
scanf("%d",pointer1+i);


printf("Enter your choice n" );
scanf("%d",&choice);

switch(choice)

case 0:
printf("Enter Index for Deletation ");
scanf("%d",&index);
/* I know that code is not finish. but calloc(size-1,2) is that valid or not ?*/
pointer2=(int*)calloc(size-1,2);
for(i=0;i!=size-1;i++)
if(i!=index)
*pointer2+i=*pointer1+i+j;

else
j++;


for(i=0;i<=size;i++)

printf("n%d",*pointer2+i);


break;

default:
printf("Error!");

return 0;





It might be but it is not the best way. Please read stackoverflow.com/questions/7097678/… for better ideas
– Antti Haapala
Sep 5 '18 at 10:03





Your calloc call seems assume that sizeof(int) is 2, which might not be correct and certainly is not portable. If sizeof(int) is in fact 2, your calloc call allocates space for size-1 elements, but your loop accesses elements at index size-1 and size which will be beyond the end of the memory allocated by calloc.
– Ian Abbott
Sep 5 '18 at 10:19


calloc


sizeof(int)


sizeof(int)


calloc


size-1


size-1


size


calloc





Your loop is for(i=0;i<=size;i++) so you certainly don't want size-1 elements in the array, you need size+1 elements. Moreover pointer1[i+j] is likely to index beyond the bounds of the memory allocation although we don't see the allocation for pointer1.
– Weather Vane
Sep 5 '18 at 10:20



for(i=0;i<=size;i++)


size-1


size+1


pointer1[i+j]


pointer1





That first loop will never finish as you have i-- and i++ cancelling each other out, so it'll get stuck when i==index
– Chris Turner
Sep 5 '18 at 10:27


i--


i++


i==index




2 Answers
2



Since you do not tell us what is pointer2, it's difficult to answer.


pointer2



But, if you write:


int *pointer2 = NULL;
size_t size = 10;

if (size-1 > 0)

pointer2 = calloc(size-1, sizeof *pointer);



The pointer2 will points to an portion of memory able to store size-1 integer (int) values.


pointer2


size-1


int


[m|c|re]alloc


2


calloc(..., 2);


pointer2



Function signature for calloc is as below.


void * calloc( size_t num, size_t size );



Make sure that num > 0 and size > 0


num > 0


size > 0





thanks for ans! but can i do calloc(size-1,2);. user will enter size.
– Owais Qureshi
Sep 5 '18 at 10:08






@OwaisQureshi I suggest pointer2 = calloc(size + 1, sizeof *pointer2);
– Weather Vane
Sep 5 '18 at 10:26


pointer2 = calloc(size + 1, sizeof *pointer2);





@WeatherVane but i want to delete element! that's why calloc(size-1,sizeof(pointer2));. thank you.
– Owais Qureshi
Sep 5 '18 at 10:47





@OwaisQureshi your code is for(i=0;i<=size;i++){ ... pointer2[i]= ... etc. In the final iteration you are indexing pointer2[size] and therefore there must be size+1 elements.
– Weather Vane
Sep 5 '18 at 10:50



for(i=0;i<=size;i++){ ... pointer2[i]= ...


pointer2[size]


size+1



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)