pointer assignment

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2008
Posts: 8
Reputation: m24r is an unknown quantity at this point 
Solved Threads: 0
m24r m24r is offline Offline
Newbie Poster

pointer assignment

 
0
  #1
Dec 21st, 2008
I am a novice in C. I am trying to assign a value from a file to a pointer as below. However I am gettign a seg fault. Can somebody explain ?

int i1;
char *glblclrtab;
...

for (int i=0; i<20; i++)
{ while ( (i1= fgetc(ipFile))==NULL);
*(glblclrtab+i)= char (i1);
...
}

The pointer assignment in causing segmentation fault.

PLZ HELP
Thanks in Advance
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 148
Reputation: devnar will become famous soon enough devnar will become famous soon enough 
Solved Threads: 16
devnar's Avatar
devnar devnar is offline Offline
Junior Poster

Re: pointer assignment

 
0
  #2
Dec 21st, 2008
What's glblclrtab pointing to? It'll have a NULL value or a garbage value which is causing segmentation fault. After you make it point to a character array, you can use *(glblclrtab+i)= (char) i1; or glblclrtab[i]= (char) i1; for simplicity.

By the way, use code tags while posting your code.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: pointer assignment

 
0
  #3
Dec 21st, 2008
char *glblclrtab; by itself will not allocate any memory to be use.
malloc() must be invoked to set apart as much memory as needed to hold the data you intend to assigned (point) to it.
Last edited by Aia; Dec 21st, 2008 at 4:00 pm.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 8
Reputation: m24r is an unknown quantity at this point 
Solved Threads: 0
m24r m24r is offline Offline
Newbie Poster

Re: pointer assignment

 
0
  #4
Dec 21st, 2008
Thanks, I had not pionted "glblclrtab". This is just an array to store the color values from file.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 8
Reputation: m24r is an unknown quantity at this point 
Solved Threads: 0
m24r m24r is offline Offline
Newbie Poster

Re: pointer assignment

 
0
  #5
Dec 23rd, 2008
Originally Posted by Aia View Post
char *glblclrtab; by itself will not allocate any memory to be use.
malloc() must be invoked to set apart as much memory as needed to hold the data you intend to assigned (point) to it.
I am using malloc now, however when I read the data back it gives garbage data. Following is the part of the code:

...
...
glblclrtab= (char *)malloc(sizeof(3*tmp));

for (int i=0; i<(3*tmp); i++)
{
if (tmp2==3)tmp2=0;
i1=fgetc( ipFile );
if (tmp2==0) {*(glblclrtab + clrcnt) = (char)i1;}
else if (tmp2==1) {*(glblclrtab + tmp +clrcnt) = (char)i1;}
else if (tmp2==2) {*(glblclrtab + 2*tmp + clrcnt)= (char)i1;}

tmp2++;
}
free (glblclrtab);


When I use free as shown above, program dumps backtrace memory range in the end and aborts.

Thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 319
Reputation: Luckychap is on a distinguished road 
Solved Threads: 42
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz

Re: pointer assignment

 
0
  #6
Dec 23rd, 2008
Originally Posted by m24r View Post
Thanks, I had not pionted "glblclrtab". This is just an array to store the color values from file.
You want 'glblclrtab' to be an array, but C compiler have no idea what your intentions are. So allocate memory before storing.
As arrays have predefined space in the memory, we do not allocate memory for them but in your case its need to be done manually.

You have no idea where 'glblclrtab' is pointing in the memory space. and neither anyone in the world have.
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 8
Reputation: m24r is an unknown quantity at this point 
Solved Threads: 0
m24r m24r is offline Offline
Newbie Poster

Re: pointer assignment

 
0
  #7
Dec 23rd, 2008
That is is reason I used the malloc before storing in the for() statement. However, I cannot use array as the size is not predefined. I have to use it on the fly. I had assigned glblclrab as char pointer as:

char *glblclrtab;

I do not know what is the right way of using the pointer here.
Last edited by m24r; Dec 23rd, 2008 at 2:06 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: pointer assignment

 
0
  #8
Dec 23rd, 2008
1. Please, USE code tag for your snippets properly:
[code=c]
your source texts
[/code]
2. You don't understand operator sizeof functionality. See what happens in your (incorrect) memory allocation:
  1. glblclrtab= (char *)malloc(sizeof(3*tmp));
The malloc functions allocates [icode]sizeof(3*tmp)[/code] bytes.
The C Standard:
The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand.
Suppose the variable tmp is int, integer literal 3 is int by definition. Therefore a type of an expression tmp*3 is int. Now sizeof(tmp*3) == sizeof(int) . For example, you allocate 4 bytes only in 32-bit environment.
If you want allocate tmp*3 bytes, write
  1. glblclrtab= malloc(3*tmp);
  2. /* No need to convert void* to char* in C explicitly */
3. What's a horrible name - glblclrtab! Try to pronounce it ...
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 8
Reputation: m24r is an unknown quantity at this point 
Solved Threads: 0
m24r m24r is offline Offline
Newbie Poster

Re: pointer assignment

 
0
  #9
Dec 23rd, 2008
I made the changes as:
  1. glblclrtab= malloc(3*tmp);
I could eliminate the error that was coming from free(). The program nolonger aborts.

However, I still do not get the values as stored.
Last edited by m24r; Dec 23rd, 2008 at 3:34 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 8
Reputation: m24r is an unknown quantity at this point 
Solved Threads: 0
m24r m24r is offline Offline
Newbie Poster

edited: pointer assignment

 
0
  #10
Dec 23rd, 2008
I made the changes as:
  1. char *glblclrtab;
  2. glblclrtab= malloc(3*tmp);
  3. for (int i=0; i<(3*tmp); i++)
  4. { // effort to sort the data from file, size unknown (tmp), I know when I parse the file
  5. if (tmp2==3)tmp2=0;
  6. i1=fgetc( ipFile );
  7. if (tmp2==0) {*(glblclrtab + clrcnt) = (char)i1;}
  8. else if (tmp2==1) {*(glblclrtab + tmp +clrcnt) = (char)i1;}
  9. else if (tmp2==2) {*(glblclrtab + 2*tmp + clrcnt)= (char)i1;}
  10. tmp2++;
  11. }
  12.  
  13. for (int i=0; i<(tmp); i++)
  14. { printf("%d::%d, %d, %d \n",i,*(glblclrtab+i),*(glblclrtab + tmp + i),*(glblclrtab+2*tmp+i));
  15. // [U][B]I receive garbage values here[/B][/U]
  16. }
  17. free (glblclrtab); // program nolonger aborts
I could eliminate the error that was coming from free(). The program nolonger aborts.

However, I still do not get the values as stored.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1312 | Replies: 18
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC