November 6th, 2002, 08:05 AM
|
#1 (permalink)
| | Member
Join Date: Apr 2002 Location: Georgia
Posts: 137
| problems with passing arrays of objects into functions
Alright, well, WHAT am I doing wrong???
I have "generalized" the code so that it doesn't have the technical details of the rest of the program with it, but I have narrowed down the problem to this.
Now this code will NOT even compile!!! What is wrong with it??? Keep in mind that it DOES compile if the if statement is left out, but that is checking to make sure there aren't any dangling pointers. Code: #include <stdlib.h>
struct structure{
int a;
int b;
int c;
};
void func(structure* structs){
for(int i=0; i<400; i++){
if(structs[i] == NULL){
//problem
}
structs[i].a = 0;
structs[i].b = 0;
structs[i].c = 0;
}
}
int main(){
structure structs[400];
func(structs);
return 0;
}
__________________
Jüš† ä €öm¶ù†Ê® §ÇÌÈñŒ mÅjÒ®
Last edited by Jüš† ä gü¥ : November 6th, 2002 at 08:16 AM.
|
| |
November 6th, 2002, 08:48 AM
|
#2 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 1,542
|
Welll, structs is of type structure *
So structs[i] = *(struct + i) is of type structure
I'm guessing NULL is of type void * and has value 0.
So comparing structs[i] to NULL will crap out as you're comparing a structure to a pointer.
What you instead want is &structs[i] or, equivalently, just struct + i.
Which shows that the test you made is pointless, as there's no way that's going to equal (void *)(0).
Remember, C is perfectly happy indexing beyond the allocated bounds of an array - that's what a buffer overflow is. |
| |
November 6th, 2002, 08:58 AM
|
#3 (permalink)
| | Ultimate Member
Join Date: Oct 2001
Posts: 1,542
|
A better explanation is:
structure structs[400] allocates a memory block of length 400 * sizeof(structure) and sets structs to point to the beginning of that block.
Then an indexing operation structs[i] moves the pointer structs forward by i * sizeof(structure) and reads the memory from that location as a structure - that is, the first (sizeof int) bytes from ((void *) structs) + i * sizeof(structure) are read as structs[i].a, the next (sizeof int) bytes as structs[i].b, and the next (sizeof int) bytes as structs[i].c.
To get dangling pointers, you'd need to declare Code: structure *structs[400];
for (int i = 0; i < 400; i++) {
structs[i] = malloc(sizeof structure);
} where failure to perform the assignment to structs[i] would leave a dangling pointer (at least, you'd hope so - in fact, if structs[400] above does not have its memory region initialised to zeroes, then structs[i] could have any bytevalue and so point anywhere, giving you a GPF, segmentation fault or similar. |
| |
November 7th, 2002, 11:44 AM
|
#4 (permalink)
| | Junior Member
Join Date: Nov 2002
Posts: 1
|
Another problem i see here is that "structure" hasn't been typdef'ed.
Thus you can't do:
structure structs[400]; and
void func(structure* structs){
but, must use:
struct structure structs[400]; /* say that 10 times fast  */ and
void func(struct structure* structs){
alternatively, just make the typedef.
typedef struct{
int a;
int b;
int c;
} structure; |
| |
November 7th, 2002, 05:43 PM
|
#5 (permalink)
| | Member
Join Date: Apr 2002 Location: Georgia
Posts: 137
| Quote: Originally posted by strangerstill Welll, structs is of type structure *
So structs[i] = *(struct + i) is of type structure
I'm guessing NULL is of type void * and has value 0.
So comparing structs[i] to NULL will crap out as you're comparing a structure to a pointer.
What you instead want is &structs[i] or, equivalently, just struct + i.
Which shows that the test you made is pointless, as there's no way that's going to equal (void *)(0).
Remember, C is perfectly happy indexing beyond the allocated bounds of an array - that's what a buffer overflow is. | strangerstill, Thank you so VERY much!!!
That was EXACTLY what I needed to hear!!! You have given me the exact perfect solution and clearly illustrated to me EXACTLY what I was doing wrong, and EXACTLY what I should be doing instead.
Aleph, no, what you have suggested wasn't the problem, but thanks also for trying to help. I know that you are supposed to precede variable declarations which are declared as a structure with struct, however, on my particular compiler, and on many others, what I have done is allowed. The only time it becomes absolutely necessary to do that (in many cases) would be to avoid name clashes between similarly named structs and unions, or the like. Perhaps on your compiler, it is required. |
| |
November 7th, 2002, 06:09 PM
|
#6 (permalink)
| | Ultimate Member
Join Date: Oct 2002 Location: Scotland, UK
Posts: 3,221
|
Buffer overflows can be useful though. 
You can push shellcode into the stack and badda-bing-badda-boom, you're in!
Obviously not as simple as that, take a look at phrack 49.
__________________
_____
NuKeS
|
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |