Pages: 1 2 3 [4]   Go Down
Author Topic: LED cube,shift registers and fun.  (Read 8731 times)
0 Members and 1 Guest are viewing this topic.
Toronto
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Love Electronics
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good Morning Tom

What you said makes sence to me. The code I offered in this Fourm was taken from someone elses code. I'd have to check my files to find out who, but I don't think it's important in this case. Oh I found the website:

http://oomlout.com/a/products/ardx/circ-05/
Check out the site and copy the code to study.

The code I got, I changed a few lines to make the 74hc595 turn on the four (4) transistors needed for this 4x4x4 Cube.

Have you look at the code??
Everyones input will be helpfull to me for a better understanding of C and C++.

I'm hoping to use Hippynerds Data to set different patterns, but havent got that far yet. I just keep reading my and others code to see whats been done, Program wise, for the Cube.

As mentioned before, I only have progremmed in Basic many many years ago, and didn't have a need to learn C or C++ until I bought an Arduino Uno two years ago.

But again Tom, Thanks for your input and hope to hear from you again.
Logged

Don't Shoot The Messenger

Toronto
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Love Electronics
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Hippynerd

I found what I think is an answer to your reply #42

It explanes about that for statement:
for(int i=0; i<256; i++)

Aparently it sets up a chunk of memory in the stacks or heap...



http://www.societyofrobots.com/member_tutorials/book/export/html/202


Some microcontroller compilers do allow you to dynamically allocate memory – but with this one enormous proviso:- The memory is automatically freed when the method that allocated it in the first place exits. How does that work? Well its then very similar to the stack. It will just use the heap pointer to allocate new memory and then move it back to its original position when the method exits. Therefore: you can never have any fragmentation in the heap. However: its usage is fairly limited.

<!--[if !supportEmptyParas]--> <!--[endif]-->
 
Consider this code:-
 
--------------------------------------------------------------------------------

void myMethod(){
 
    char buffer[256];
 
    for(int I=0; I<256;I++){
 
            buffer=I;
 
    }
 
}
 
--------------------------------------------------------------------------------

This will allocate space for ‘buffer’ by moving the stack pointer down by 256 bytes.
 
<!--[if !supportEmptyParas]--> <!--[endif]-->
 
Whereas this code:-
 
<!--[if !supportEmptyParas]-->
 
--------------------------------------------------------------------------------

void myMethod(){
 
   char buffer* = malloc(256);
 
    for(int I=0; I<256;I++){
 
            buffer->=I;
 
    }
 
}
 
--------------------------------------------------------------------------------

will allocate space for ‘buffer’ by moving the heap pointer up by 256 bytes. So they are almost identical.

<!--[if !supportEmptyParas]--> <!--[endif]-->
 
But you must be careful not to do the following:-
 
--------------------------------------------------------------------------------

char buffer*;
 
void myMethod(){
 
   buffer = malloc(256);
 
    for(int I=0; I<256;I++){
 
            buffer->=I;
 
    }
 
}
 
--------------------------------------------------------------------------------

since ‘buffer’ is assigned a value in the method – but, unlike a ‘proper’ C compiler - once the method exits then ‘buffer’ is referring to a lump of memory that is now being used by other things. So changing some of its values will corrupt something else. Bad!
 
<!--[if !supportEmptyParas]--> <!--[endif]-->
 
So dynamic memory allocation should generally be avoided and we will not discuss it any further!!
 
<!--[if !supportEmptyParas]--> <!--[endif]-->
Logged

Don't Shoot The Messenger

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1728
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I think I am right in saying that there is an important omission from the example you posted.
This:
Code:
void myMethod(){
    char buffer* = malloc(256);
    for(int I=0; I<256;I++){
        buffer[l]=I;
    }
   //When the function returns, the 'buffer' memory remains allocated, but the pointer to this memory is lost.
   //As nothing now knows the address of the allocated memory, nothing can use it. This is known as a memory leak.
}
Should be:
Code:
void myMethod(){
    char buffer* = malloc(256);
    for(int I=0; I<256;I++){
        buffer[l]=I;
    }
    free(buffer); //free the memory otherwise there is a memory leak.
}
Logged

~Tom~

Toronto
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Love Electronics
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Tom

The message you speak of is one I tol ok from the internet. the explanation was quite extensive so I didn't get it all in my cut and past ;-)

The more I look at all the codes, the more my head spins. I think it'll take some time before I really catch on.
I bought a book on C++ How To Program by Deitel & Deitel and I'm reading it from the start.

Do I understand the the program has a header and a body??
And how long is the body of the program, or is it everything after the header??

Thanks again for your input.
Sparkie
Logged

Don't Shoot The Messenger

Pages: 1 2 3 [4]   Go Up
Jump to: