I am doing a program for dynamic memory allocation.Here we present the user to store three student marks and if the user wants to enter more marks we increase the size to three more. I have used realloc to increase the size to three more.
The program is running fine for first four elements.But when I increase the size it is closing unexpectedly.
I am using devcpp to run this program:
#include<stdio.h>
#include<stdlib.h>
//using namespace std;
int main()
{
/* Here we allocate a buffer of 3 integers first and increase the size by
three bytes every time buffer overflows. */
int *marks = (int *)malloc(sizeof(3 * sizeof(int)));
/* mark is the current student mark and n is the number of marks input upto now*/
int mark,n=0,i;
/* siz is the current size of the array */
int siz = 3;
printf("\n Enter marks and -1 to stop\n");
scanf("%d",&mark);
while(mark != -1)
{
if(n >= siz)
{
siz = siz+3;
printf("\n reallocating three more integers");
marks = (int *)realloc(marks,siz * sizeof(int));
if(!mark)
{
printf("\nmemory not allocated");
exit(1);
}
else
printf("\n success");
}
marks[n] = mark;
scanf("%d",&mark);
n++;
}
printf("\n the entered marks are:");
for(i=0;i<n;i++)
printf("%3d",marks[i]);
printf("\n");
int *marks = (int *)malloc(sizeof(3 * sizeof(int)));
is incorrect. The operator sizeof() does not evalute an expressiion inside it. It only defines its type. The type of the expression 3 * sizeof( int ) is size_t (usually it is unsigned int) so the value of sizeof( 3 * sizeof( int ) ) is only 4 bytes on a 32-bit platform.
#include<stdio.h>
#include<stdlib.h>
//using namespace std;
int main()
{
/* Here we allocate a buffer of 3 integers first and increase the size by
three bytes every time buffer overflows. */
int siz = 3;
int *marks = (int *)malloc(siz * sizeof(int));
/* mark is the current student mark and n is the number of marks input upto now*/
int my_mark = 0 ,n=0,i , count = 0 ;
/* siz is the current size of the array */
printf("\n Enter the number o f marks \n");
scanf("%d",&n);
if(n >= siz)
{
siz += n;
printf("\n reallocating three more integers");
marks = (int *)realloc(marks,siz * sizeof(int));
}
if(!marks)
{
printf("\nmemory not allocated");
exit(1);
}
else
printf("\n success");
while(count < ( siz + n ) )
{
printf("\n Enter marks and -1 to stop\n");
scanf("%d",&my_mark);
marks[count] = my_mark;
scanf("%d",&my_mark);
count++;
}
printf("\n the entered marks are:");
for(i=0;i<n;i++)
printf("%3d",marks[i]);
printf("\n");
return 0;
}
// malloc310320121322.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
int _tmain(int argc, _TCHAR* argv[])
{
int siz = 3;
int my_mark = 0 ,n=0,i , count = 0 ;
int *marks = (int *)malloc(siz * sizeof(int));
printf("\n Enter the number of marks \n");
scanf("%d",&n);
if(n >= siz)
{
siz += n;
printf("\n reallocating three more integers");
marks = (int *)realloc(marks, n * sizeof(int));
}
if(!marks)
{
printf("\nmemory not allocated");
exit(1);
}
else
printf("\n success");
while(count < ( n ) )
{
printf("\n Enter marks and -1 to stop\n");
scanf("%d",&my_mark);
marks[count] = my_mark;
count++;
}
printf("\n the entered marks are:");
for(i=0;i<n;i++)
{
printf("%3d",marks[i]);
printf("\n");
}
free( marks );
return 0;
}