free freeing data twice or data that shoulnd't be freed
Maybe I'm declaring top->desc wrong but where I don't see it
Code:
#include <stdlib.h> ---------- Post added 10-02-12 at 08:26 PM ---------- on this topic anyone know of a better debugger for linux? ---------- Post added 10-02-12 at 08:27 PM ---------- or how to debug this problem better? |
Code:
ch = malloc(1); Quote:
|
still crashing
|
also if you add top->next = NULL;
after line top->descr[strlen(desc) + 1] = 0x00; top->name equals nothing. wierd. now it seems to be when freeing name |
Hi.
Let's debug your program using valgrind. Code:
$ gcc -g crash.c && valgrind ./a.out Code:
ch = malloc(1); Code:
ch = malloc(1); Fixed. Repeat the procedure: Code:
$ gcc -g crash.c && valgrind ./a.out Code:
fgets(name, 100, fp); Code:
if ((fp = fopen(".hometown.map", "r")) == NULL) Code:
if ((fp = fopen(".hometown.map", "r")) == NULL) { Continue: Code:
$ touch .hometown.map Line 68: Code:
top->name = malloc(strlen(name) + 1); Code:
top = malloc(sizeof(top)); Code:
struct areas *top; Code:
top = malloc(sizeof(struct areas)); Continue: Code:
$ gcc -g crash.c && valgrind ./a.out Code:
top->name = malloc(strlen(name) + 1); Code:
top->name = malloc(strlen(name) + 1); Code:
$ gcc -g crash.c && valgrind ./a.out |
I'm sorry I didn't include .hometown.map I thought I gave you everything you needed to debug and I myself
came up short. I must say I am much oblidged to you, Firstfire. top = malloc(sizeof(struct areas)); was exactly the problem. everything else is actually really cool. Again thanks to Firstfire. I read it fifteen times and fifteen times I missed it. And special thanks to Ntubski for pointing out my character. Sharing my Cool skeleton project: http://o0oo0.net16.net/shared ch is a pointer cause fread doesn't like plain characters for some reason. heres .hometown.map http://o0oo0.net16.net/shared/.hometown.map |
errigour, if you really think that post was helpful, please click on the "Yes" (bottom right corner). Firstfire made a very good job.
|
Quote:
Code:
char ch; Code:
int ch; /* getc returns a character or EOF so you need to use an int */ Code:
top = malloc(sizeof(*top)); |
I can't figure out what you are doing, linked-listing?
I think you are making life very difficult for yourself. Why are you poncing about with malloc may I ask? It's a recipe for mistakes avoid if you can. If you must you can use strdup(3) to copy strings. If it's a fire-and-forget application don't even waste your time calling free the OS will recover any allocated space when you exit. And don't do everything in one main function unless it's very trivial. And I wouldn't use fread if I can get away with using stdio.h (Let the OS do the work where you can and use your time for the interesting stuff). ddd is a good front end for gdb. |
well im using malloc because I can't get everything to compile right. I'm being difficult intentionally to master what I cannot do casually.
since you mention ddd maybe you can help me use it. My program seems to be changing the address of char name[0] to 0x00. or I think because strlen of name = 0; so how can I use ddd valgrind and gdb to find where the registry error is easily if I can. maybe you could show me an example use of ddd for this. |
char area_map[10000][10000];
crashes my program but when its not there the program runs great |
Quote:
One example is: http://www.linuxquestions.org/questi...2/#post4688720 In the example in that post it is important that the call to increase the stack limit happens before the call to the function that includes the declaration of the large array. I think C99 allows you to declare the large array within a function after doing work, such as increasing the stack limit, earlier in the same function. That might work. With certain added tricks specific to C99, it could work reliably. But in most cases the compiler will move the stack allocation to the beginning of the function, so changing the limit earlier in the same function is not good enough. It needs to be changed in a different function executed before this function is called. I expect you want that array declared in main() in some program similar to the one at the top of this thread. In C, that makes it very hard to execute code before the function is called. There are other methods, but I suggest you have too much code in main anyway. Consider moving almost everything from main to some other function called by main. I would tend to leave inside main certain steps like opening of required files (".hometown.map") and aborting if those files are missing, then pass the file pointer to the function that takes over most of main's previous job. |
.hometown.map:
http://o0oo0.net16.net/shared/.hometown.map Can I ask one more thing here instead of posting: Code:
top->area_map = calloc(height, width); top->area_map is char **area_map is that a stack problem or am I defining it wrong? i really gotta know how to do this thanks. |
char area_map[500][1000]
it gives a segmentation fault at top->area_map[0][0] = area_map[0][0]; |
Quote:
char **area_map together with area_map[y][x] implies area_map points to a vector of pointers and each of those points to a separately allocated vector of data. You could replace the single allocation step with multiple allocations to build up the space implied by your current declaration and access code. Alternately, you could change the declaration to fit your existing allocation and access: I think that would be: Code:
typedef char data_row[1001]; |
what do I do?
|
Quote:
|
doesn't matter I found the answer thanks for all the help.
|
Here's the answer i think might be off by one:
Code:
struct areas |
Quote:
Code:
top->area_map = malloc(height * sizeof(char*)); |
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. |
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. |
also does allocating memory use ram or does it use hard disk space?
|
Quote:
|
Quote:
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. |
what about my maximum ram I only have 512 when will my program cease?
|
also that means I have to declare 1000 extra bytes just to have an array. seems kinda dumb.
|
Quote:
char[3][3]; Code:
0 1 2 Quote:
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:
Quote:
|
Thankyou
SOLVED |
All times are GMT -5. The time now is 12:18 AM. |