Link to home
Start Free TrialLog in
Avatar of viertelstrasse
viertelstrasseFlag for United States of America

asked on

Segmentation fault , core dumped in C .

Dear Experts,

I need read  an ascii file , it has data as an array of NxN   the values has the syntax 99.99 as maximum value  , ej.
34.23  9.12 4.7
4.12 6.2 32.08
5.2 83.1 11.27

and I need store the values in a Dynamic array.  I will be use arrays of 30x30, or  5000x5000 or  more.

My code in order to do it works fine as unique file with a main function, I  turned it into a function costs_order in a head file.
and I have a error message:  " Segmentation fault      (core dumped)"  

so, I change the core file size  to unlimited and the stack size  to 15360 kbytes

part of my valgrind output is:
-----------------------------------------------------------------------------------
....
...
[1]  {0} .. Is digit
==16707== Conditional jump or move depends on uninitialised value(s)
==16707==    at 0x8049930: costs_order(bins-hy.h:390)
==16707==    by 0x804A984: main (main.c:265)
==16707==
i:[2]  {.} .. Is a POINT
==16707== Conditional jump or move depends on uninitialised value(s)
==16707==    at 0x80499A5: costs_orderbins-hy.h:396)
==16707==    by 0x804A984: main (main.c:265)
==16707==
==16707== Invalid write of size 1
==16707==    at 0x80499C8: costs_order(bins-hy.h:398)
==16707==    by 0x804A984: main (main.c:265)
==16707==  Address 0xbea749be is not stack'd, malloc'd or (recently) free'd
==16707==
==16707==
==16707== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==16707==  Access not within mapped region at address 0xBEA749BE
==16707==    at 0x80499C8: costs_order (bins-hy.h:398)
==16707==    by 0x804A984: main (main.c:265)
==16707==  If you believe this happened as a result of a stack
==16707==  overflow in your program's main thread (unlikely but
==16707==  possible), you can try to increase the size of the
==16707==  main thread stack using the --main-stacksize= flag.
==16707==  The main thread stack size used in this run was 15728640.
==16707==
==16707== HEAP SUMMARY:
==16707==     in use at exit: 422,141 bytes in 595 blocks
==16707==   total heap usage: 16,784 allocs, 16,189 frees, 10,757,912 bytes allocated
==16707==
==16707== Searching for pointers to 595 not-freed blocks
==16707== Checked 150,844 bytes
==16707==
==16707== 228 bytes in 57 blocks are definitely lost in loss record 37 of 53
==16707==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==16707==    by 0x40CBA50: strdup (strdup.c:43)
==16707==    by 0x804A756: main (main.c:242)
==16707==
==16707== 11,716 bytes in 58 blocks are definitely lost in loss record 51 of 53
==16707==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==16707==    by 0x8049B5C: cutString (rules.h:26)
==16707==    by 0x804A756: main (main.c:242)
==16707==
==16707== LEAK SUMMARY:
==16707==    definitely lost: 11,944 bytes in 115 blocks
==16707==    indirectly lost: 0 bytes in 0 blocks
==16707==      possibly lost: 0 bytes in 0 blocks
==16707==    still reachable: 410,197 bytes in 480 blocks
==16707==         suppressed: 0 bytes in 0 blocks
==16707== Reachable blocks (those to which a pointer was found) are not shown.
==16707== To see them, rerun with: --leak-check=full --show-reachable=yes
------------------------------------------------------------------------------------------------------------------
line 396 is in the code :       len= strlen(ibuffer);
line 398  is in the code :      ibuffer[len+1]= '\0';       

  in the  for (r=0; r< lSize; r++){   also show msg ... Idont know why because the algorithm as unique main funcion c works fine!! and as head file as problems,  is the same code!! for both cases,  in one works in other has problems why?.
   
for an array of 30x30 data the buffer has a lSize with 5431  and I use 5429 it is not a stack overflow !!

pls find below the code.

what can I do? I don't understand what happend!  any idea?

my best regards,


pd.  I use ansii C under kubuntu 12.04,  gcc 4.6.3
part-of-code.txt
Avatar of Hugh McCurdy
Hugh McCurdy
Flag of United States of America image

First question -- What is sizeof(ibuffer) ?
SOLUTION
Avatar of Hugh McCurdy
Hugh McCurdy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
One more question.  What are you trying to accomplish with the following statement?

ibuffer[len+1]= '\0';

strlen found that ibuffer[len] already has a null character.  Why do you need two null characters in a row?  (One reason would be that you are appending a single character to the string.)

Still, I think the issue is worthy enough of discussion that I asked the question.

In any event, I'd really like to see the result from strlen() and the result from sizeof().
Avatar of phoffric
phoffric

Run your program in ddd debugger to get additional clues. Use -ggdb -Wall compiler options, fixing all warnings.

Other options:
   http://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html

>> printf("** LSIZE ES: {%ld}  strlen(buffer): {%d} **\n", lSize,strlen(buffer));
re: strlen(buffer) -- how do you know there is a terminating null byte?
Good point phoffric about the knowing there's terminating null character.

I think that would show up if we tried to compare sizeof with strlen but it's a good point.
If you do not know how to use ddd to isolate this problem(s), then post your entire program including either build instructions or makefile, and one of us should be able to help you better.
Avatar of viertelstrasse

ASKER

Dear  hmccurdy,

ibuffer is a  char ibuffer[8] it is in order to storage a single char in the char array and after . Yes. Im appending a single character to the string.)

 Dear phoffric,  thanks I will compile again with your suggestions,
well,   the >> printf("** LSIZE ES: {%ld}  strlen(buffer): {%d} **\n", lSize,strlen(buffer));
the strleng(buffer)  or lSize  in order to use in my  "for"

 for (r=0; r< lSize; r++){      or      for (r=0; r< strlen(buffer) ; r++){  
because maybe  here is a stack overflow ...  but not it is!.

I add  in the end of the function  strcpy(ibuffer,"");     and now I don't have any problem for an  array of  30x30   but  for an array of 5000x5000 I have  "memory error"  :(

thanks for your suggestions and in advanced!.

chears,
I hadn't noticed the code fragment before.  I see you are printing out as you go.

Assuming you don't get a solution by following phoffric's advice, could you share the last few lines that get printed before the crash?

(I'm assuming you haven't redirected the standard output to a file.  Is that correct?)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dear experts,

-----------------------------
char ibuffer[8];
...
  strcpy(ibuffer,"");   // <--- added before to use in order to appending a single character to the string
...
------------
    Now for arrays of 30x30 works fine ...but for arrays of 5000x5000  I have a single error message       "memory error"    and with the  valgrind , it is hanging ..Im waiting and nothing, does not end. :S

Ok I will test now the ddd  ...

thnx
Dear Experts,

Finally, after a long time running valgrind show this message!! ....ohh! and now? what can I do? I need  use arrays of 5000 x 5000  

thanks in advanced!.
chears!.

--------------------------------------------------------------------------------------------------------------
Memory error==18602==
==18602== HEAP SUMMARY:
==18602==     in use at exit: 2,583,758,982 bytes in 1,279,217 blocks
==18602==   total heap usage: 178,276,343 allocs, 176,977,240 frees, 115,603,705,290 bytes allocated
==18602==
==18602==
==18602==     Valgrind's memory management: out of memory:
==18602==        newSuperblock's request for 5120000 bytes failed.
==18602==        3109568512 bytes have already been allocated.
==18602==     Valgrind cannot continue.  Sorry.
==18602==
==18602==     There are several possible reasons for this.
==18602==     - You have some kind of memory limit in place.  Look at the
==18602==       output of 'ulimit -a'.  Is there a limit on the size of
==18602==       virtual memory or address space?
==18602==     - You have run out of swap space.
==18602==     - Valgrind has a bug.  If you think this is the case or you are
==18602==     not sure, please let us know and we'll try to fix it.
==18602==     Please note that programs can take substantially more memory than
==18602==     normal when running under Valgrind tools, eg. up to twice or
==18602==     more, depending on the tool.  On a 64-bit machine, Valgrind
==18602==     should be able to make use of up 32GB memory.  On a 32-bit
==18602==     machine, Valgrind should be able to use all the memory available
==18602==     to a single process, up to 4GB if that's how you have your
==18602==     kernel configured.  Most 32-bit Linux setups allow a maximum of
==18602==     3GB per process.
==18602==
==18602==     Whatever the reason, Valgrind cannot continue.  Sorry.
>> 3109568512 bytes have already been allocated.
For some reason your program appears to be currently using 3GB memory. Are you allocating without properly freeing? Check this without Valgrind in place.
Dear phoffric

I freeing the  all  memory used.   I use  free() function.   Also for the dynamic vector and all dynamic structure.

pls find enclose the image of the ulimit -a .         I use a  32-bit kubuntu  on processor intel i5 with, 4GB ram.
 

any suggestions?

Thanks in advanced.
message-valgrind3.jpg
Was this from the 30x30matrix?

==16707==    definitely lost: 11,944 bytes in 115 blocks

That's a memory leak.  I think it would be useful to see what valgrind reports at 30x30 and then at 100x100 and so forth.  If the leak gets bigger, it could be the problem.

Regardless, at least at the time of that valgrind test you had a memory leak.
I assume there's a reason, such as the program is proprietary, why we can't get to see the entire program.  Is that correct?

It's a lot easier to debug the code if we have the code.
If the program is proprietary, then the standard practice is for you to sanitize it and shrink the program down while still maintaining the problems you are seeing. Then you can post this minimal program.

Confirm that your program is requiring more memory as processing continues by adding breakpoints and monitor memory usage.
Dear Experts,

It is the Heap Summary for a 30x30 array , the operations in the array is only are extract information . By other hand,  I use newlisp has shared library loaded in my code because I send a string (lisp instructions) are executed and return the value to C.  
 
For this 30x30 array runs OK ..However I don't know why I have memory leaks .

what happend?  
thanks very much in advanced.
my best regards,

The small part of code is attached

reference:
http://www.newlisp.org/CodePatterns.html#toc-24


==3057==
==3057== HEAP SUMMARY:
==3057==     in use at exit: 414,821 bytes in 564 blocks
==3057==   total heap usage: 16,789 allocs, 16,225 frees, 10,758,736 bytes allocated
==3057==
==3057== Searching for pointers to 564 not-freed blocks
==3057== Checked 144,912 bytes
==3057==
==3057== LEAK SUMMARY:
==3057==    definitely lost: 167,197 bytes in 119 blocks
==3057==    indirectly lost: 0 bytes in 0 blocks
==3057==      possibly lost: 0 bytes in 0 blocks
==3057==    still reachable: 247,624 bytes in 445 blocks
==3057==         suppressed: 0 bytes in 0 blocks
==3057== Rerun with --leak-check=full to see details of leaked memory
==3057==
==3057== Use --track-origins=yes to see where uninitialised values come from
==3057== ERROR SUMMARY: 26 errors from 11 contexts (suppressed: 0 from 0)
==3057==
==3057== 1 errors in context 1 of 11:
==3057== Conditional jump or move depends on uninitialised value(s)
==3057==    at 0x4612403: copyCell (in /usr/lib/newlisp.so)
==3057==    by 0x461C7D7: translateCreateSymbol (in /usr/lib/newlisp.so)
==3057==    by 0x46128F8: makeStringSymbol (in /usr/lib/newlisp.so)
==3057==    by 0x40694D2: (below main) (libc-start.c:226)
==3057==
==3057==
==3057== 1 errors in context 2 of 11:
==3057== Conditional jump or move depends on uninitialised value(s)
==3057==    at 0x4612403: copyCell (in /usr/lib/newlisp.so)
==3057==    by 0x461C7D7: translateCreateSymbol (in /usr/lib/newlisp.so)
==3057==    by 0x4612BB3: initialize (in /usr/lib/newlisp.so)
==3057==    by 0x40694D2: (below main) (libc-start.c:226)
==3057==
==3057==
==3057== 1 errors in context 3 of 11:
==3057== Conditional jump or move depends on uninitialised value(s)
==3057==    at 0x4612403: copyCell (in /usr/lib/newlisp.so)
==3057==    by 0x461C7D7: translateCreateSymbol (in /usr/lib/newlisp.so)
==3057==    by 0x4612B89: initialize (in /usr/lib/newlisp.so)
==3057==    by 0x40694D2: (below main) (libc-start.c:226)
==3057==
==3057==
==3057== 1 errors in context 4 of 11:
==3057== Conditional jump or move depends on uninitialised value(s)
==3057==    at 0x4612403: copyCell (in /usr/lib/newlisp.so)
==3057==    by 0x461C7D7: translateCreateSymbol (in /usr/lib/newlisp.so)
==3057==    by 0x4612B5F: initialize (in /usr/lib/newlisp.so)
==3057==    by 0x40694D2: (below main) (libc-start.c:226)
==3057==
==3057==
==3057== 1 errors in context 5 of 11:
==3057== Conditional jump or move depends on uninitialised value(s)
==3057==    at 0x4612403: copyCell (in /usr/lib/newlisp.so)
==3057==    by 0x461C7D7: translateCreateSymbol (in /usr/lib/newlisp.so)
==3057==    by 0x4612B35: initialize (in /usr/lib/newlisp.so)
==3057==    by 0x40694D2: (below main) (libc-start.c:226)
==3057==
==3057==
==3057== 1 errors in context 6 of 11:
==3057== Conditional jump or move depends on uninitialised value(s)
==3057==    at 0x4612403: copyCell (in /usr/lib/newlisp.so)
==3057==    by 0x461C7D7: translateCreateSymbol (in /usr/lib/newlisp.so)
==3057==    by 0x4612B0B: initialize (in /usr/lib/newlisp.so)
==3057==    by 0x40694D2: (below main) (libc-start.c:226)
==3057==
==3057==
==3057== 1 errors in context 7 of 11:
==3057== Conditional jump or move depends on uninitialised value(s)
==3057==    at 0x4612403: copyCell (in /usr/lib/newlisp.so)
==3057==    by 0x461C7D7: translateCreateSymbol (in /usr/lib/newlisp.so)
==3057==    by 0x4612AE1: initialize (in /usr/lib/newlisp.so)
==3057==    by 0x40694D2: (below main) (libc-start.c:226)
==3057==
==3057==
==3057== 1 errors in context 8 of 11:
==3057== Conditional jump or move depends on uninitialised value(s)
==3057==    at 0x4612403: copyCell (in /usr/lib/newlisp.so)
==3057==    by 0x461C7D7: translateCreateSymbol (in /usr/lib/newlisp.so)
==3057==    by 0x4612AB7: initialize (in /usr/lib/newlisp.so)
==3057==    by 0x40694D2: (below main) (libc-start.c:226)
==3057==
==3057==
==3057== 1 errors in context 9 of 11:
==3057== Conditional jump or move depends on uninitialised value(s)
==3057==    at 0x4612403: copyCell (in /usr/lib/newlisp.so)
==3057==    by 0x461C7D7: translateCreateSymbol (in /usr/lib/newlisp.so)
==3057==    by 0x4612A30: initialize (in /usr/lib/newlisp.so)
==3057==    by 0x40694D2: (below main) (libc-start.c:226)
==3057==
==3057==
==3057== 1 errors in context 10 of 11:
==3057== Conditional jump or move depends on uninitialised value(s)
==3057==    at 0x4612403: copyCell (in /usr/lib/newlisp.so)
==3057==    by 0x461C7D7: translateCreateSymbol (in /usr/lib/newlisp.so)
==3057==    by 0x46129FD: initialize (in /usr/lib/newlisp.so)
==3057==    by 0x40694D2: (below main) (libc-start.c:226)
==3057==
==3057==
==3057== 16 errors in context 11 of 11:
==3057== Conditional jump or move depends on uninitialised value(s)
==3057==    at 0x4612403: copyCell (in /usr/lib/newlisp.so)
==3057==    by 0x461C7D7: translateCreateSymbol (in /usr/lib/newlisp.so)
==3057==    by 0x4612C0C: initialize (in /usr/lib/newlisp.so)
==3057==    by 0x40694D2: (below main) (libc-start.c:226)
==3057==
==3057== ERROR SUMMARY: 26 errors from 11 contexts (suppressed: 0 from 0)
code-part2.txt
Dear Experts,

I don't understand why the memory leaks,  for sample for a function that cut String

1.char *cutString(char *str, int start, int len){
2.     char *s;
3.     s= (char*)malloc(len + 1);
4.      if (s){
5.     strncpy(s, str+start, len);
6.    s[len] = '\0';
7.  }
8.  return strdup (s);
9. }

valgrind report memory leak in the line 3  . If I use a free (s) before of return strdup (s);  the value will be lost,   and   by other hand, if the free(s) is not used I will have a memory leak.  so? what is the way to don't have value lost and memory leak?
¿?     :S
thnks in advanced.!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
to solve the leak replace

return strdup (s);

Open in new window


by

return s;

Open in new window


and free the s in the calling function after use.


if your program is c++ i would use std::string and std::vector instead of c arrays. those classes would have a much better memory management than you (probably) will do it now. for example a

std::vector<std::vector<float> >  xarr(5000, std::vector<float>(5000, 0.0));

Open in new window


would create 5.000 "row" arrays each of them is 5.000 x sizeof(float), what is 20.000 bytes each. if you create one single 2-d array of 5000x5000 float, it has a size of 100 mb, and it is a good chance that the request failed because there is not so much contiguous memory available.  

Sara
Let me suggest another approach. If you are using strings arrays, should allocate twice. First for the row (the size of the bigger string), then for the column. Strings have unknown size when defined by pointers, as it is the case. So the compiler assumes a default lenght, not necessarily what we need.

Below, an example of memory allocation with  integers, which lenght is known.
 
void createMatrix(int dimension)
{
int **assigncost;
assigncost = new int*[dimension];
for (int i = 0; i < dimension; i++)
    assigncost[i] = new int[dimension];
}

Open in new window

completed with
delete[] assigncost;

Open in new window

Jose
Im using C .  not C++
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

Did we answer your original question?  If not, where are you stuck?

~Hugh
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
I think the question was answered but the author wouldn't confirm it.  The reason I think this is because he went on to a second question without starting a new thread.

The problem is that we don't know which one of us answered the question.  My guess is that it was a combination.  There should be some way to award points in this muddle because I have pretty good confidence that we did solve the first question.
I agree with hmccurdy as to the first question being answered. The follow-on 2nd question may have required some additional input.

I will leave it to the other experts to make their recommendations.
IMHO, the initial problem was acknowledged as solved by the author with post ID: 38282281 and the statement "Now for arrays of 30x30 works fine..."  What happened is that once we solved the first problem, instead of posting a new problem (as he should have because it was a new problem), he piggybacked.

(I know EE doesn't work this way but the author really did ask 2 difficult questions at different times and, IMHO, should be 500 points for the first and 500 points for the second.  Perhaps 500 points with an A grade for the first and 500 points with an A or  a B grade for the second.  In this case, I would say A.  In any event...back on task...)

My proposal is a grade of A and points awarded as follows for the first question.
ID: 38282197 -- Best answer 350 points
ID: 38281503 -- Assist 150 points.

I also think 400/100 is fair but I did spend time discovering the point of answer and doing this analysis.

-------

As for question #2, if there's some way for EE to award points for the follow-up question that should have been a new question.  (Grade A because the answers were quality.)
ID: 38288904 -- Best answer 300 points
ID: 38282881 -- Assist 100 points
ID: 38284013 -- Assist 100 points

Again, I don't know if these points can be awarded but IMHO, they should be awarded to a "virtual" second post.  (There should have been a second post because it was a distinct question not initially asked with a very different solution.)
Figure it didn't hurt to ask.  The asker was author was asked to post a new question but he didn't, for whatever reason.

In any event, I suggest you either award the points as I suggested in the first section (because of the strong evidence that the first question was indeed answered) or combine the suggestions and divide the points by 2 (so that they add up to 500 instead of 1000).

In any event, I have very high confidence that the original question was answered especially since the Author said it was by saying that it worked now for 30x30.  But once that was fixed, he found another error with his program.
Fine with me.
fair enough. an equal split was the wisest decision cause we don't know whether any of the comments really helped. but a new reader could take benefit from comments.

Sara