LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 06-27-2009, 08:08 PM   #1
Guilherme
Member
 
Registered: Jul 2006
Posts: 83

Rep: Reputation: 15
pointer to pointer segmentation fault


Hi
I have been googling trying to fix this ...
basicly I have a (int **) variable, and when I try to pass a (int) value to a specific position, I have seg.fault.

Resuming what I have is:

int ** p;
p[0][0] = 1; //segmentation fault


:\ sorry for this question, but ...


can you please help me ?
 
Old 06-27-2009, 08:10 PM   #2
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71
Well, you need to malloc some memory before you start to write into it...
 
Old 06-27-2009, 08:15 PM   #3
Guilherme
Member
 
Registered: Jul 2006
Posts: 83

Original Poster
Rep: Reputation: 15
ok, I though it was the problem.
but, malloc needs a specific length.
Is it possible with C to use an unlimited array? or ... at least with some kind of undefined length?

Last edited by Guilherme; 06-27-2009 at 08:17 PM.
 
Old 06-27-2009, 08:19 PM   #4
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71
Not quite, but there are workarounds. First, the length (actually, the size) you request with malloc can be a variable, which is defined at runtime as opposed to compilation time. Second, you can try to realloc() if you really need more.
But it's impossible to request an infinite array from the beginning.

PS. There are also linked lists. Those can be extended at will (when needed).

Here's a small tutorial on malloc
http://ee.hawaii.edu/~tep/EE160/Book...on2.1.2.2.html

Last edited by Uncle_Theodore; 06-27-2009 at 08:24 PM.
 
Old 06-27-2009, 08:22 PM   #5
Guilherme
Member
 
Registered: Jul 2006
Posts: 83

Original Poster
Rep: Reputation: 15
hum ...
ok thanks a lot.
realloc may help a lot in fact.

thanks
 
Old 06-27-2009, 09:00 PM   #6
Guilherme
Member
 
Registered: Jul 2006
Posts: 83

Original Poster
Rep: Reputation: 15
hum ... sorry ... still can't fix it

Code:
int ** matriz_inserir(int dimV, int dimH)
{
        int h, v, tmp;
        int ** matriz;
        matriz = (int**) calloc(dimH*dimV,sizeof(int));
        if(dimH < 1) dimH = 1; 
        if(dimV < 0) dimH *= -1; 
        if(dimV != 0)
        {
                puts(MSG_INSERIR);
                for (h = 0; h < dimH; h++)
                        for (v = 0; v < dimV; v++)
                        {
                                if(dimH > 1) printf(" %d -",h+1);  
                                printf(" %d: ",v+1);
                                scanf("%d",&matriz[h][v]);
                        }
        }
        return matriz;
}
:\
 
Old 06-27-2009, 10:00 PM   #7
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71
In your code the line
matriz = (int**) calloc(dimH*dimV,sizeof(int));
does not create a double array.

Here's a little primer
Code:
#include<stdio.h>
#include<stdlib.h>

int main()
{
  int **p;
  int numrows = 3;
  int numcolumns = 4;
  int i, j;

  if(!(p = (int **)malloc(numrows * sizeof(int *)))) return 1;
  for(i=0;i<numrows;i++)
    if(!(p[i] = (int *)malloc(numcolumns * sizeof(int)))) return 1;

  for(i=0;i<numrows;i++)
    for(j=0;j<numcolumns;j++)
      {
	p[i][j] = i+j;
	printf("p[%d][%d]=%d,\n", i,j,p[i][j]);
      };
  for(i=0;i<numrows;i++)
    free(p[i]);
  free(p);
  return 0;
}
 
Old 06-28-2009, 09:47 AM   #8
Guilherme
Member
 
Registered: Jul 2006
Posts: 83

Original Poster
Rep: Reputation: 15
hum ok ...
so it needs to alloc from the "last" pointer to the "first" ... ok ...


thank you a lot indeed
 
  


Reply



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
pass pointer,, return pointer??? blizunt7 Programming 3 07-23-2005 01:36 PM
returning data to main() via a pointer to a pointer. slzckboy Programming 3 05-30-2005 01:20 PM
seg. fault when allocating memory via a pointer inside a struct elmafiacs Programming 4 02-20-2005 07:26 AM
checking pointer is non-null causes segmentation fault in c++ markhod Programming 10 01-11-2005 11:28 AM
hot to set value of pointer to pointer to a structure in C alix123 Programming 2 11-17-2004 06:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:45 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration