Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi all can anyone tell me please w

Status
Not open for further replies.

Bangieff

Technical User
Feb 12, 2001
52
BG
Hi all
can anyone tell me please why this piece of code returns me segmentation fault:

void func(int typ)
{
char **abc;
...
switch(sys)
{
case 1:
char *tmp[]={"a","b","c");
abc=tmp; //when reach this row "segmentation
//fault" occures
break;
case 2:
...
default:
...
}

}
 
You're not allocating any memory for abc. Try

man malloc

for some info on this.
Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
I tryed bot notting happens
and else I initialize the **tmp so abc get's it's memory.
Am I wrong?
I tried with abc=(char **)malloc(sizeof(char)*10) but the same result...
 
No, you don't need malloc() as the assignment sets abc to point to the 1st element of tmp.

I can't see anything wrong with your code. Are you sure you're getting the seg fault on this line? I'd be surprised if that's the case because a seq fault usually occurs when you try to access or write to memory that doesn't belong to you. I suspect that there is a problem elsewhere in your code.

Russ
bobbitts@hotmail.com
 
ofcourse it's possible there to be mistake somewhere else but I'm sure this is the place of the "segmaentation sault" because of the break points I put in the code

...
cout<<&quot;bp1\n&quot;;
abc=tmp
cout<<&quot;bp2\n&quot;;

simple but effective I think ;)

there is easyer way to make what I want to but I don't want to waste the memory...

well I'll keep trying
 
This may not help too much, but have you considered using the string class? Like so:
void func(int typ)
{
string *abc;
...
switch(sys)
{
case 1:
string tmp[]={&quot;a&quot;,&quot;b&quot;,&quot;c&quot;);
abc=tmp; //when reach this row &quot;segmentation
//fault&quot; occures
break;
case 2:
...
default:
...
}
}
I'm a big advocate of STL usage - it can make life a WHOLE lot easier!
 
Hi,

The assignment abc = tmp is right but you have declared the pointer variable abc outside the switch and you declared the tmp inside the switch. The memory allocated for the variable tmp [ and for the constants ] will be removed at the end of block [ switch ] but life time of the variable abc is upto the function func. So, if u access the variable abc outside the switch you will get segmentation fault.

So, check wheather your are using the variable abc outside the switch block.

If you want to do so, then use dynamic memory manipulation.

 
Yes I do...that's the idea ... ;)
And that's what I was afraid of...I mean the release of memory reserved for tmp after the end of the block...
but I already solved this problem.

Thank You.
If I can help some way:
Bangieff@borsabg.com
 
As an aside, you shouldn't rely on doing stuff like:

cout << &quot;I am here&quot; << endl;
/* do something */
cout << &quot;I am there&quot; << endl;

for determining where segfaults and the like occur. Sometimes the problem can occur after the line that reads &quot;I am there&quot; and &quot;I am there&quot; will not be printed to the console because the output buffer didn't get flushed in time. It's better to step through it line by line in a source debugger, then you can be sure.

Russ
bobbitts@hotmail.com
 
there is a tracer in linux called strace but it's not too comfortable.... would you please reccomend me a better one
 
You can use gdb to step through the code line by line. If you don't like the command line interface, there are a number of graphical interfaces out there. I use ddd, which I've been really happy with. You can download it here:


Other good tools are dmalloc:


And Electric Fence:


Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top