reallocation is giving an exception.

Hello ,

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");



system("pause");
return 0;
}
This expression

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.

You should rewrite this expression as

int *marks = (int *)malloc( 3 * sizeof( int ) );
Last edited on
mark shoud be array and loop of three or size like n ..
cheers
xxx
Last edited on
you are rignt about the size vlad
I think this will work ..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#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;
}
Last edited on
You can check it youself.L)
there we go

thanks vlad

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// 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;

}
Thanks it works. vlad.
Topic archived. No new replies allowed.