Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 12-15-2005, 01:00 PM   #1 (permalink)
 
Newb Techie

Join Date: Dec 2005

Posts: 8

eugensth

Default memory allocation in C

Hi,

I have a problem with memory allocation in C. After allocating with malloc a matrix of integers :

int*a = malloc(10 * sizeof (int));

I would like to free the memory occupated by some values like a[3], or a[5], but keeping the other elements of the matrix and the matrix itself.
Can anybody help me?

Thanks anticipated
eugensth is offline  
Old 12-15-2005, 01:31 PM   #2 (permalink)
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

The only way to do this that I would reccomend, would be creating a dynamically resizable array.

Start with 10, if you like. As you delete one, create a new array of 9. Copy all remaining elements to that one, and free() the original array of 10.
__________________
Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache)
TheHeadFL is offline  
Old 12-15-2005, 01:42 PM   #3 (permalink)
 
Newb Techie

Join Date: Dec 2005

Posts: 8

eugensth

Default

But what happens when I deal with big matrixes of structures ? I guess this will consume a lot of resources, ain't?
eugensth is offline  
Old 12-15-2005, 02:33 PM   #4 (permalink)
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

This is how its always been done if you want to use simple arrays and matrices.

Unless you would like to use a linked list instead....
__________________
Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache)
TheHeadFL is offline  
Old 12-15-2005, 02:42 PM   #5 (permalink)
 
Newb Techie

Join Date: Dec 2005

Posts: 8

eugensth

Default

That's what I wanted to avoid, but I see I have no other choice.
eugensth is offline  
 
Closed Thread

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On