LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   free freeing data twice or data that shoulnd't be freed (https://www.linuxquestions.org/questions/programming-9/free-freeing-data-twice-or-data-that-shoulnd%27t-be-freed-4175430177/)

errigour 10-04-2012 01:52 PM

what do I do?

johnsfine 10-04-2012 02:02 PM

Quote:

Originally Posted by errigour (Post 4797374)
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.

errigour 10-04-2012 02:16 PM

doesn't matter I found the answer thanks for all the help.

errigour 10-04-2012 02:25 PM

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];
          }
    }


johnsfine 10-04-2012 02:27 PM

Quote:

Originally Posted by errigour (Post 4797410)
Code:

    top->area_map = malloc(height);

Change that to
Code:

    top->area_map = malloc(height * sizeof(char*));

errigour 10-04-2012 05:39 PM

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.

errigour 10-04-2012 05:50 PM

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.

errigour 10-04-2012 06:00 PM

also does allocating memory use ram or does it use hard disk space?

johnsfine 10-04-2012 07:00 PM

Quote:

Originally Posted by errigour (Post 4797563)
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.

johnsfine 10-04-2012 07:04 PM

Quote:

Originally Posted by errigour (Post 4797576)
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.

errigour 10-04-2012 07:10 PM

what about my maximum ram I only have 512 when will my program cease?

errigour 10-04-2012 07:11 PM

also that means I have to declare 1000 extra bytes just to have an array. seems kinda dumb.

johnsfine 10-04-2012 07:29 PM

Quote:

Originally Posted by errigour (Post 4797613)
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 (Post 4797613)
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 (Post 4797242)
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 (Post 4797610)
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.

errigour 10-04-2012 08:11 PM

Thankyou
SOLVED


All times are GMT -5. The time now is 06:04 AM.