LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-17-2020, 12:55 AM   #1
Rishabhh Rawat
LQ Newbie
 
Registered: Mar 2020
Posts: 1

Rep: Reputation: Disabled
realloc () : invalid pointer error


Question;-

Today, I write a program on realloc() in visual studio code.

Input;-

#include<stdio.h>
#include<stdlib.h>
void main()
{
int n,b;
int *ptr;
int *p,*q;
int i,c=0;
printf("\nEnter the size of array = ");
scanf("%d",&n);
ptr = (int*)malloc(n*sizeof(int));
if(ptr==NULL)
{
printf("Error,its envolved");
exit(0);
}
p = ptr;
printf("\n\nEnter Element in block;-\n\n");
for(i=0;i<n;i++)
{
scanf("%d",ptr);
c = c + *ptr;
ptr++;
}
printf("\nSum = %d\n",c);
printf("\nElements in Block;-\n\n");
for(i=0;i<n;i++)
{
printf("\n%d",*p);
p++;
}
printf("\nNew size of array = ");
scanf("%d",&b);
ptr = (int*)realloc(ptr,b*sizeof(int));
if(ptr==NULL)
{
printf("Error,its envolved");
exit(0);
}
q = ptr;
printf("\n\nEnter Element in block;-\n\n");
for(i=0;i<b;i++)
{
scanf("%d",ptr);
c = c + *ptr;
ptr++;
}
printf("\nSum = %d\n",c);
printf("\nElements in Block;-\n\n");
for(i=0;i<b;i++)
{
printf("\n%d",*q);
q++;
}

}

output;-

realloc(): invalid pointer
Aborted (core dumped)


{DEAR SIR/MAM, HOW CAN I FIX THIS? }
 
Old 03-17-2020, 01:12 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,779

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
first of all you need to use code tags to post your code, that will keep the original formatting. It is now quite hard to read.
 
1 members found this post helpful.
Old 03-17-2020, 02:15 AM   #3
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
I had submitted a great answer, but then I found I misunderstood your program.

Yes, please use code tags.

Last edited by berndbausch; 03-17-2020 at 02:17 AM.
 
Old 03-17-2020, 02:24 AM   #4
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by Rishabhh Rawat View Post
for(i=0;i<n;i++)
{
scanf("%d",ptr);
c = c + *ptr;
ptr++;
}

realloc(): invalid pointer
Aborted (core dumped)


{DEAR SIR/MAM, HOW CAN I FIX THIS? }
You move the pointer after the end of the allocated region, if I understand your program right. Since nothing is allocated there, the pointer is invalid.

Troubleshooting advice: Find the most recent location in the program where the pointer is set.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
linux C programming : realloc(): invalid pointer problem golden_boy615 Programming 11 01-09-2014 06:59 AM
[SOLVED] problem with realloc(): invalid next size golden_boy615 Programming 2 07-19-2011 05:24 AM
Won't get realloc to work - glibc: invalid old size ruh31 Programming 5 06-08-2011 05:39 PM
Error during cimserver start : *** glibc detected *** realloc(): invalid pointer MaverickLikesMustang Linux - Software 0 02-14-2008 06:42 AM
*** glibc detected *** ./input: realloc(): invalid next size: custode Programming 4 05-04-2007 03:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration