Like that cool XOR swap routine? The one that swaps without needing an extra variable. Or is it better to just use an extra variable if you have unlimited variables (like C has)?
_player1537 wrote:
Like that cool XOR swap routine? The one that swaps without needing an extra variable. Or is it better to just use an extra variable if you have unlimited variables (like C has)?


Well, you don't have "unlimited" variables - they still take up space. The XOR swap is a neat trick, but it isn't really any better unless you can't afford the extra variable.

Just do the straightforward approach.

Also, do you want it have to a single block of memory (2d array) or an array of pointers? The two are different, and Kerm is kind of flip/flopping between the two. With the former, you must memcpy, with the later you just copy pointers. With the former, you malloc once, with the later you malloc n + 1 times. The two are declared slightly differently, but behave very differently.
I was assuming that he was dealing with an array of pointers, and was writing my code as such. If I wrote some code for the former, I made an error.
KermMartian wrote:
I was assuming that he was dealing with an array of pointers, and was writing my code as such. If I wrote some code for the former, I made an error.


Well, you talked about memcpy which is pointless for an array of pointers. He also asked for a 2D char array (even said char[n][100]), not an array of pointers.
Not pointless for an array of pointers, just uselessly inefficient, but conceptually simpler than exchanging pointers if the teacher didn't explain that sort of thing and expects them not to use those types of concepts. But yeah, he did indeed ask for that, my error.
Excerpt of code:


Code:
struct studentRecord
{
   int ID;
   char firstName [100];
   char lastName [100];
   char major [100];
   float GPA;
};

struct studentRecord *recordArray = malloc( max_entries*sizeof(studentRecord) );

The very last line produces an error:

Code:
error: ‘studentRecord’ undeclared here (not in a function)


Now, I figure I'm probably doing something grossly incorrect but, being new to this concept, I can't see it for the life of me. Would one of the more intelligent and adept users kindly point out my mistakes and berate me for them? Very Happy

[edit] the code itself is global, not residing within main() or another such function.
You can't use that kind of code from outside a function. Declare a function, something like this:

Code:
int test()
{
     studentRecord *recordArray = malloc( max_entries*sizeof(studentRecord) );
}

And you should be able to use the variable recordArray from there. I don't know too much about C, but this would probably be your issue. You can declare the variable outside of a function, but you can't assign it like that from outside.
SirCmpwn wrote:
You can't use that kind of code from outside a function. Declare a function, something like this:

Code:
int test()
{
     studentRecord *recordArray = malloc( max_entries*sizeof(studentRecord) );
}

And you should be able to use the variable recordArray from there. I don't know too much about C, but this would probably be your issue. You can declare the variable outside of a function, but you can't assign it like that from outside.


Ah, I see, but how exactly am I supposed to use this struct then? If I assign it in a function, it apparently is inaccessible to other functions (I may just be referencing it wrong) and I'm not sure how I could pass such a behemoth or return it.
TsukasaZX wrote:
Ah, I see, but how exactly am I supposed to use this struct then? If I assign it in a function, it apparently is inaccessible to other functions (I may just be referencing it wrong) and I'm not sure how I could pass such a behemoth or return it.

Keep in mind that this has mostly become guesswork with my lack of language-specific knowledge, but here's what I think will work:

Code:
struct studentRecord
{
   int ID;
   char firstName [100];
   char lastName [100];
   char major [100];
   float GPA;
};

struct studentRecord *recordArray;

void initializeStudentRecords()
{
     *recordArray = malloc(max_entries*sizeof(studentRecord));
}


Make sure that initializeStudentRecords() is called at least once before using *recordArray.
I am pretty sure you can do it like this:
Code:
struct studentRecord *recordArray;
void main() {
  recordArray = malloc( max_entries*sizeof(studentRecord) );
  recordArray[0].GPA = 20; // I had -> instead of . here, but I think . is correct, actually.
}
(I'd love someone to double-check that.)

Edit: SirCmpwn, that will not work exactly. By doing *recordArray =, you are taking the value recordArray is pointing to (garbage) and trying to store stuff to it. Just recordArray = should work. (double-check would be great still...)
Either way, I get errors stating that "studentRecord" is undeclared in both of my functions now.
TsukasaZX wrote:
Either way, I get errors stating that "studentRecord" is undeclared in both of my functions now.

You should be using recordArray instead, studentRecord is the name of the struct and cannot be accessed from the context of a variable.
SirCmpwn wrote:
TsukasaZX wrote:
Either way, I get errors stating that "studentRecord" is undeclared in both of my functions now.

You should be using recordArray instead, studentRecord is the name of the struct and cannot be accessed from the context of a variable.


D'oh! >_<

Dynamic memory, why are you so difficult? `-`

Thanks, Sir and _player
TsukasaZX wrote:


D'oh! >_<

Dynamic memory, why are you so difficult? `-`

Thanks, Sir and _player

I take it that worked? If so, you're welcome.

Code:
*** glibc detected *** ./records: free(): invalid next size (fast): 0x09314008 ***
======= Backtrace: =========
/lib/libc.so.6(+0x6c501)[0x623501]
/lib/libc.so.6(+0x6dd70)[0x624d70]
/lib/libc.so.6(cfree+0x6d)[0x627e5d]
./records[0x8048d34]
/lib/libc.so.6(__libc_start_main+0xe7)[0x5cdce7]
./records[0x80484f1]
======= Memory map: ========
0022b000-00247000 r-xp 00000000 08:06 490717     /lib/ld-2.12.1.so
00247000-00248000 r--p 0001b000 08:06 490717     /lib/ld-2.12.1.so
00248000-00249000 rw-p 0001c000 08:06 490717     /lib/ld-2.12.1.so
005b7000-0070e000 r-xp 00000000 08:06 490737     /lib/libc-2.12.1.so
0070e000-00710000 r--p 00157000 08:06 490737     /lib/libc-2.12.1.so
00710000-00711000 rw-p 00159000 08:06 490737     /lib/libc-2.12.1.so
00711000-00714000 rw-p 00000000 00:00 0
00d84000-00d9e000 r-xp 00000000 08:06 490639     /lib/libgcc_s.so.1
00d9e000-00d9f000 r--p 00019000 08:06 490639     /lib/libgcc_s.so.1
00d9f000-00da0000 rw-p 0001a000 08:06 490639     /lib/libgcc_s.so.1
00fbe000-00fbf000 r-xp 00000000 00:00 0          [vdso]
08048000-0804a000 r-xp 00000000 08:07 450936     /home/subaru/Desktop/records
0804a000-0804b000 r--p 00001000 08:07 450936     /home/subaru/Desktop/records
0804b000-0804c000 rw-p 00002000 08:07 450936     /home/subaru/Desktop/records
09314000-09335000 rw-p 00000000 00:00 0          [heap]
b7700000-b7721000 rw-p 00000000 00:00 0
b7721000-b7800000 ---p 00000000 00:00 0
b78d2000-b78d3000 rw-p 00000000 00:00 0
b78df000-b78e3000 rw-p 00000000 00:00 0
bfcf6000-bfd17000 rw-p 00000000 00:00 0          [stack]
Aborted


Erm, this happened on exit of my program. What does it mean?
TsukasaZX wrote:
Erm, this happened on exit of my program. What does it mean?


It looks like you gave an invalid argument to free.
Weird....


Code:
free(recordArray);


That's my free statement. Pointer recordArray is the only thing I malloc'd/realloc'd in the program.
Did you make sure recordArray had memory allocated for it first, ie initialized?
At the beginning of main(), I have very explicitly written

Code:
recordArray = malloc( max_entries*sizeof(recordArray) );


I'm wondering if maybe I actually do need to free every individual element of recordArray before freeing the array itself?
You shouldn't. Would you mind posting all the stuff pertaining to recordArray as you have it in your code?
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
» Goto page Previous  1, 2, 3, 4, 5, 6  Next
» View previous topic :: View next topic  
Page 5 of 6
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement