| | |
pointer assignment
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2008
Posts: 8
Reputation:
Solved Threads: 0
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
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
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
•
•
Join Date: Dec 2008
Posts: 8
Reputation:
Solved Threads: 0
•
•
•
•
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.
...
...
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
•
•
•
•
Thanks, I had not pionted "glblclrtab". This is just an array to store the color values from file.
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.
•
•
Join Date: Dec 2008
Posts: 8
Reputation:
Solved Threads: 0
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.
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.
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:
The malloc functions allocates [icode]sizeof(3*tmp)[/code] bytes.
The C Standard:
Suppose the variable tmp is int, integer literal 3 is int by definition. Therefore a type of an expression
If you want allocate tmp*3 bytes, write
3. What's a horrible name - glblclrtab! Try to pronounce it
...
[code=c]
your source texts
[/code]
2. You don't understand operator sizeof functionality. See what happens in your (incorrect) memory allocation:
c Syntax (Toggle Plain Text)
glblclrtab= (char *)malloc(sizeof(3*tmp));
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.
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
c Syntax (Toggle Plain Text)
glblclrtab= malloc(3*tmp); /* No need to convert void* to char* in C explicitly */
... •
•
Join Date: Dec 2008
Posts: 8
Reputation:
Solved Threads: 0
I made the changes as:
I could eliminate the error that was coming from free(). The program nolonger aborts.
However, I still do not get the values as stored.
c Syntax (Toggle Plain Text)
glblclrtab= malloc(3*tmp);
However, I still do not get the values as stored.
Last edited by m24r; Dec 23rd, 2008 at 3:34 pm.
•
•
Join Date: Dec 2008
Posts: 8
Reputation:
Solved Threads: 0
I made the changes as:
I could eliminate the error that was coming from free(). The program nolonger aborts.
However, I still do not get the values as stored.
c Syntax (Toggle Plain Text)
char *glblclrtab; glblclrtab= malloc(3*tmp); for (int i=0; i<(3*tmp); i++) { // effort to sort the data from file, size unknown (tmp), I know when I parse the file 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++; } for (int i=0; i<(tmp); i++) { printf("%d::%d, %d, %d \n",i,*(glblclrtab+i),*(glblclrtab + tmp + i),*(glblclrtab+2*tmp+i)); // [U][B]I receive garbage values here[/B][/U] } free (glblclrtab); // program nolonger aborts
However, I still do not get the values as stored.
![]() |
Similar Threads
- Error:Null Pointer assignment (C++)
- getting null pointer assignment error (C)
- In which case "Null Pointer Assignment" will be displayed on the output ?? (C)
- Why this C code is returning Null Pointer Assignment as output (C)
- Assign content to a function pointer (C)
Other Threads in the C Forum
- Previous Thread: upnp related project topics
- Next Thread: Please help me
Views: 1312 | Replies: 18
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char command convert copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic executable fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc program programming radix recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming spoonfeeding stack standard string strings structures student systemcall testautomation turboc unix user variable visualstudio voidmain() wab win32 windows.h






