Sponsored Content
Top Forums Programming problem with realloc( i think is gcc :/ ) Post 302611425 by giampoul on Thursday 22nd of March 2012 08:21:47 PM
Old 03-22-2012
problem with realloc( i think is gcc :/ )

Hi everyone,
i made this program. is a simple one for practising malloc, realloc and structs.
I have a struct named shop as global variable in which i take the size of the matrix from the keyboard and after i malloc it.
I insert the values with the fullarray() and after i print the matrix with printproductlist().
After i ask to give from the keyboard to realloc some more rows in the array and again fullarray() and printproductlist().
I compile it with gcc in linux and i get no errors or warnings, things are going well in the run malloc is done and the program gives me the thinks i write into array magazi.
Problems begun when its time for the realloc to be done.
The code is this :
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct shop
{
        char product[10];
        char code[10];
        int instock;
        float cost;
};

struct shop *magazi;

void fullarray(int start,int end)
{
	int i;
	for ( i=start;i<end;i++ )
  	{
    	printf("-Name of product No %d: ",i+1);
    	scanf("%s",magazi[i].product);
    	printf("\n\tProduct Code : ");
    	scanf("%s",magazi[i].code);
    	printf("\n\tIn stock : ");
    	scanf("%d",&magazi[i].instock);
    	printf("\n\tValue : ");
    	scanf("%f",&magazi[i].cost);
    	printf("\n");
  	}
}

void printproductlist(int pro)
{
	int i;
	printf("|A/A\t|Name\t|Code\t|Stock\t|Cost\n");
	printf("+-------+-------+-------+-------+-------+\n");
  	for(i=0;i<pro;i++)
  	{
    		printf(" %d\t %s\t %s\t %d\t %.2f\n",i+1,magazi[i].product,magazi[i].code,magazi[i].instock,magazi[i].cost);
  	}
}

int main()
{
  	int proionta;
  	printf("Number of products : ");
  	scanf("%d",&proionta);
  
  	magazi=(struct shop *)malloc(proionta*sizeof(struct shop *));
  	if(magazi==NULL)
    		printf("\nMalloc Error");
  
  	fullarray(0,proionta);
  	printproductlist(proionta);
  
  	int proionta2;
  	printf("How many more products? ");
  	scanf("%d",&proionta2);
  
	if(proionta2>0)
  	{
    	magazi=(struct shop *)realloc(magazi,proionta2*sizeof(struct shop *));
    	//realloc memory check
    
    	fullarray(proionta,(proionta2+proionta));
    	printproductlist(proionta+proionta2);
  	}
  
	free(magazi);
  	//scanf("%d",&proionta);
  
  return 0;
}

I get a very strange thing in the terminal. Like this :
Code:
*** glibc detected *** ./a.out: realloc(): invalid next size: 0x0000000000db4010 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7a6e6)[0x7fbf5de346e6]
/lib/x86_64-linux-gnu/libc.so.6(+0x7d3e7)[0x7fbf5de373e7]
/lib/x86_64-linux-gnu/libc.so.6(realloc+0xf9)[0x7fbf5de38b39]
./a.out[0x4009d8]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7fbf5dddb30d]
./a.out[0x4005c9]

and after a memory map...
After a few tries i tried the same code in DevCpp in Windows. It works perfectly.
I dont know what kind of problem is this...? Any ideas? Also, if you run it put product names lower than 6 characters.

Last edited by giampoul; 03-22-2012 at 09:31 PM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Problem installing gcc

Hi! I have downloaded a precompiled gcc package but I'm having problems installing it. It is supposed to install in /usr/local and there is plenty of space left there but during the installation I get the following error message “No space left on device”. Is it some kind of temp folder that is out... (2 Replies)
Discussion started by: alfabetman
2 Replies

2. Programming

gcc Link problem

I have some crypting functions in object-file - for example,func.o Main file,from where crypting functions called, named as main.cpp . "gcc main.cpp -o main func.o" print "undefined reference to ... " But if I rename main.cpp to main.c and execute "gcc main.c -o main func.o" - ok. If anybody,... (3 Replies)
Discussion started by: Gigachel
3 Replies

3. Programming

A problem of GCC

Hi mates, I am a new comer of this forums. I have a problem while using function "fread(buffer, size, number, file-pointer)" to read a binary file. While I used the "fread()" to read the binary file under Solaris UNIX System, it worked very well. But it gets a incorrect result... (6 Replies)
Discussion started by: cquedugdylp
6 Replies

4. Solaris

gcc 3.4 Regex problem

Hello All, I have been using String.h with gcc 2.95. Now I have upgraded to gcc 3.4. The support for String.h has been removed and I believe Regex.h support is also not inbuilt. So I tried to build my own library for String.h. I had to use Regex.h and rx.h for a succesful library compilation.... (0 Replies)
Discussion started by: manishs13
0 Replies

5. AIX

The problem after install gcc on AIX 5.3

The problem after install gcc on AIX 5.3 I download 5 rpm packages from IBM AIX Toolbox Download Page - Alphabetical Listing and install them. rpm -ivh gcc-4.0.0-1.aix5.3.ppc.rpm rpm -ivh libgcc-4.0.0-1.aix5.3.ppc.rpm rpm -ivh libstdcplusplus-4.0.0-1.aix5.3.ppc.rpm rpm -ivh... (1 Reply)
Discussion started by: zither
1 Replies

6. Red Hat

GCC version problem

I have a host PC and a target system . Host System -> gcc (3.2) Redhat linux kernel 2.4.20-8 , glibc 2.3 Target System -> gcc (4.1) Fedora linux kernel 2.6.18-1.2798.fc6, glibc 2.5 I would like to build test program that can be run on both systems --------------------------- ... (0 Replies)
Discussion started by: mugdha
0 Replies

7. Solaris

gcc problem on Solaris

I just installed gcc from the latest package at sunfreeware.com using pkgadd. I write a simple 'Hello World!' program to test it. I try to compile it and I get this error: rockstar# gcc hello.c -o hello ld.so.1: gcc: fatal: libiconv.so.2: open failed: No such file or directory Killed I... (0 Replies)
Discussion started by: Bradj47
0 Replies

8. Programming

Problem with static compiling - GCC

Hi guys. I want to compile three files: gcc -static main.c fib.c fib.h it is pure C i mean i use standard C library. but it gives me this error: /usr/bin/ld: cannot find -lc collect2: ld returned 1 exit status what should i do? (4 Replies)
Discussion started by: majid.merkava
4 Replies

9. Programming

gcc problem

Hi, I just try to solve the problem why one app failing on box1 and runs ok on box2, they have diff gcc version as I see from /proc/version: box1:: 3.4.1 box2:: 4.1.2 It's RedHat 64b boxes, so I can not run some <make all> command on box1, do you think I need to upgrade (if I'm using... (1 Reply)
Discussion started by: mario17
1 Replies

10. Red Hat

Installatin problem in installing gcc-4.9.0

Hi to all, i'm installing gcc-4.9.0 into my RedHat 6.4 pc. I configure like this :- # mkdir objdir # ./../gcc-4.9.0/configure # make and now having this problem :- checking for x86_64-unknown-linux-gnu-gcc... /software/gcc/objdir/./gcc/xgcc -B/software/gcc/objdir/./gcc/... (0 Replies)
Discussion started by: rahulvishwa
0 Replies
MALLOC(2)							System Calls Manual							 MALLOC(2)

NAME
malloc, free, realloc, calloc - memory allocator SYNOPSIS
#include <u.h> #include <libc.h> void* malloc(long size) void free(void *ptr) void* realloc(void *ptr, long size) void* calloc(long nelem, long elsize) DESCRIPTION
Malloc and free provide a simple memory allocation package. Malloc returns a pointer to a new block of at least size bytes. The block is suitably aligned for storage of any type of object. No two active pointers from malloc will have the same value. The argument to free is a pointer to a block previously allocated by malloc; this space is made available for further allocation. It is legal to free a null pointer; the effect is a no-op. Realloc changes the size of the block pointed to by ptr to size bytes and returns a pointer to the (possibly moved) block. The contents will be unchanged up to the lesser of the new and old sizes. The call realloc(0, size) means the same as Calloc allocates space for an array of nelem elements of size elsize. The space is initialized to zeros. Free frees such a block. Alef Except for calloc, these routines are available from Alef; they use the same arena as alloc. Malloc and realloc execute a check when they fail, rather than return nil. Memory returned by malloc is cleared. Realloc does not guarantee new memory is cleared unless ptr is nil. SOURCE
/sys/src/libc/port/malloc.c SEE ALSO
brk(2) DIAGNOSTICS
Malloc, realloc and calloc return 0 if there is no available memory. Errstr is likely to be set. BUGS
The different specification of calloc is bizarre. User errors can corrupt the storage arena. The most common gaffes are (1) freeing an already freed block, (2) storing beyond the bounds of an allocated block, and (3) freeing data that was not obtained from the allocator. When malloc and free detect such corruption, they abort. MALLOC(2)
All times are GMT -4. The time now is 11:13 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy