C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 13th, 2013, 02:51 PM
ddeakpeti ddeakpeti is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 14 ddeakpeti User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 25 sec
Reputation Power: 0
What's the error?

Hi,
I have the following array, which doesn't want work. After I debug, it prints out nothing and shows an application error.

#include <stdio.h>
#include <stdlib.h>

int main()
{
int nr=4, nc=6;

float** pmtx = (float**)malloc(nr*sizeof(float*));
for (int i=0; i<nr; ++i)
pmtx[i] = (float*)malloc(nc*sizeof(float));

for (int i=0; i<nr; ++i)
for (int j=0; i<nc; ++j)
pmtx[i][j] = (i+1)*(j+1);

for (int i=0; i<nr; ++i)
{
for (int j=0; j<nc; ++j)
printf("%8.lf", pmtx[i][j]);
printf("\n");
}
printf("\n");

for (int i=0; i<nr; ++i)
free (pmtx[i]);
free (pmtx);
return 0;
}

Reply With Quote
  #2  
Old January 13th, 2013, 03:05 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 25 m 12 sec
Reputation Power: 1774
Here is why you should use code tags when posting code.
Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
  int nr = 4, nc = 6;

  float **pmtx = (float **) malloc(nr * sizeof(float *));
  for (int i = 0; i < nr; ++i)
    pmtx[i] = (float *) malloc(nc * sizeof(float));

  for (int i = 0; i < nr; ++i)
    for (int j = 0; i < nc; ++j)
      pmtx[i][j] = (i + 1) * (j + 1);

  for (int i = 0; i < nr; ++i) {
    for (int j = 0; j < nc; ++j)
      printf("%8.lf", pmtx[i][j]);
    printf("\n");
  }
  printf("\n");

  for (int i = 0; i < nr; ++i)
    free(pmtx[i]);
  free(pmtx);
  return 0;
}


Now, debugging is more than just running the code and watching it crash.
Debugging means run the code in the debugger and have a good look around to see what caused the problem
Code:
$ gdb ./a.out 
GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/sc/Documents/a.out...done.
(gdb) run
Starting program: /home/sc/Documents/a.out 

Program received signal SIGSEGV, Segmentation fault.
0x000000000040067b in main () at foo.c:14
14	      pmtx[i][j] = (i + 1) * (j + 1);
(gdb) print i
$1 = 0
(gdb) print j
$2 = 33776
(gdb) list
9	  for (int i = 0; i < nr; ++i)
10	    pmtx[i] = (float *) malloc(nc * sizeof(float));
11	
12	  for (int i = 0; i < nr; ++i)
13	    for (int j = 0; i < nc; ++j)
14	      pmtx[i][j] = (i + 1) * (j + 1);
15	
16	  for (int i = 0; i < nr; ++i) {
17	    for (int j = 0; j < nc; ++j)
18	      printf("%8.lf", pmtx[i][j]);
(gdb) 

The first output shows the segfault, AND the line of code causing the problem.

OK, so i and j are involved, so print them out
Woah - WTF is that huge number in j, where did that come from!?

Now print the code around the offending line - do you see it yet?
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old January 13th, 2013, 03:30 PM
ddeakpeti ddeakpeti is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 14 ddeakpeti User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 25 sec
Reputation Power: 0
Thank you, and sorry for not formatting.
Code:
 
#include <stdio.h>

void main ()
{
printf("this is how to use this code tag, cool\n");
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > What's the error?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap