LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-04-2012, 01:52 PM   #16
errigour
Member
 
Registered: May 2009
Posts: 266

Original Poster
Rep: Reputation: 5

what do I do?
 
Old 10-04-2012, 02:02 PM   #17
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,051

Rep: Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100
Quote:
Originally Posted by errigour View Post
what do I do?
I was editing some extra details into my above post while you replied, so maybe it answers that now even if it didn't when you first read it.
 
Old 10-04-2012, 02:16 PM   #18
errigour
Member
 
Registered: May 2009
Posts: 266

Original Poster
Rep: Reputation: 5
doesn't matter I found the answer thanks for all the help.
 
Old 10-04-2012, 02:25 PM   #19
errigour
Member
 
Registered: May 2009
Posts: 266

Original Poster
Rep: Reputation: 5
Here's the answer i think might be off by one:
Code:
struct areas
{
     char **area_map;
};
     struct areas *top;
     top = malloc(sizeof(areas));
     top->area_map = malloc(height);
     for( y = 0; y < height; y++)
     {
          top->area_map[y] = malloc(width);
          for ( x = 0; x < width; x++)
          {
               printf("y: %d, x: %d", y, x);
               top->area_map[y][x] = area_map[y][x];
          }
     }
 
Old 10-04-2012, 02:27 PM   #20
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,051

Rep: Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100
Quote:
Originally Posted by errigour View Post
Code:
     top->area_map = malloc(height);
Change that to
Code:
     top->area_map = malloc(height * sizeof(char*));
 
Old 10-04-2012, 05:39 PM   #21
errigour
Member
 
Registered: May 2009
Posts: 266

Original Poster
Rep: Reputation: 5
Got another multi demension array newb question:
I'm guessing because my program wont let me define the top elements of an array(not sure if my math is
wrong). Do I have to declare one extra number in both the height and the width of my area_map array?

just talkin about the local array not the structure.
 
Old 10-04-2012, 05:50 PM   #22
errigour
Member
 
Registered: May 2009
Posts: 266

Original Poster
Rep: Reputation: 5
It works like that im gonna leave it like that I just need some kind of reassurance that its breaking because
of that is all thanks alot.
 
Old 10-04-2012, 06:00 PM   #23
errigour
Member
 
Registered: May 2009
Posts: 266

Original Poster
Rep: Reputation: 5
also does allocating memory use ram or does it use hard disk space?
 
Old 10-04-2012, 07:00 PM   #24
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,051

Rep: Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100
Quote:
Originally Posted by errigour View Post
Do I have to declare one extra number in both the height and the width of my area_map array?
An array of size N has valid indexes from 0 through N-1 inclusive. Index N is not a valid index of an array of size N.
 
Old 10-04-2012, 07:04 PM   #25
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,051

Rep: Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100
Quote:
Originally Posted by errigour View Post
also does allocating memory use ram or does it use hard disk space?
Not exactly.

If you allocate a lot of memory, that just uses virtual address space (in "demand zero" mode). It doesn't use ram nor hard disk.

As you write to each page (aligned 4KB portion) of the allocated space, that page is forced to exist in ram. If you are short of ram, then forcing some page to exist in ram may push some other page (maybe part of the same allocation) out to swap space. If you are also short of swap space then it may force the OS to kill some process.
 
Old 10-04-2012, 07:10 PM   #26
errigour
Member
 
Registered: May 2009
Posts: 266

Original Poster
Rep: Reputation: 5
what about my maximum ram I only have 512 when will my program cease?
 
Old 10-04-2012, 07:11 PM   #27
errigour
Member
 
Registered: May 2009
Posts: 266

Original Poster
Rep: Reputation: 5
also that means I have to declare 1000 extra bytes just to have an array. seems kinda dumb.
 
Old 10-04-2012, 07:29 PM   #28
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,051

Rep: Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100
Quote:
Originally Posted by errigour View Post
also that means I have to declare 1000 extra bytes just to have an array. seems kinda dumb.
You seem to be missing the point. Think about an array
char[3][3];
Code:
  0 1 2
0 a b c
1 d e f
2 g h i
I showed indexes in blue and showed possible contents a .. i. There is no "extra". A 3x3 array has nine elements and you can use all nine. You can't use index 3, because the starting index is 0.

Quote:
Originally Posted by errigour View Post
also that means I have to declare 1000 extra bytes just to have an array. seems kinda dumb.
Or maybe you meant the multi-level allocation to allow the char** declaration to be correct. For a 500x1000 array, that multi-level allocation costs a lot more than 1000 extra bytes. Each allocation has overhead, plus the pointers are it least 4 bytes each.

If you worry about that overhead, use the alternate declaration that allows you to malloc a single chunk.

But at 500x1000, these details don't matter. That is 500KB of data plus about 6KB of overhead. That is one tenth of one percent of your total ram.

Quote:
Originally Posted by errigour View Post
char area_map[10000][10000];
But that is 100MB. That is a big enough fraction of your ram to take seriously, especially to ask why you want to have one version of that on the stack apparently just to copy it to another version allocated differently.

Quote:
Originally Posted by errigour View Post
what about my maximum ram I only have 512 when will my program cease?
Assuming you have a decent size hard drive, I suggest you allocate a few (2 or 3) GB of swap space, so that when your program uses too much memory it will just get very slow rather than crash. That should make such problems easier to diagnose, quantify and correct.

Last edited by johnsfine; 10-04-2012 at 07:46 PM.
 
Old 10-04-2012, 08:11 PM   #29
errigour
Member
 
Registered: May 2009
Posts: 266

Original Poster
Rep: Reputation: 5
Thankyou
SOLVED
 
  


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
Hang after "Freeing unused kernel memory (...)kb freed" qcinski Linux From Scratch 0 08-01-2009 12:12 PM
The booting hang on "Freeing unused kernel memory : 168k freed" Brian Lu Linux - Software 1 05-27-2008 08:45 PM
'Freeing Unused Kernel Memory: 224k Freed - Laptop Hangs nutnut Linux - Laptop and Netbook 1 10-21-2005 07:09 PM
Freeing unused kernel memory: 136k freed apenguinlinux Debian 8 08-08-2005 03:30 PM
Freeing unused kernel memory: 228k freed bodedeb Linux - General 8 10-28-2003 03:59 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration