[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Good explanation on dynamic allocation

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 14
Thread images: 4

File: 1436415651911.png (97KB, 752x454px) Image search: [Google]
1436415651911.png
97KB, 752x454px
I'm a normie in C programming and generally any advance programming and I have never before used dynamic allocation (I usually just copy it from stackoverflow if I need something).

Can someone point me to a good online tutorial or an explanation of pointers and dynamic allocation in C ? My collage lectures so far have gone too much in depth on the topic so although I get the concept and all the details and still can't use them in real life programming

I had some homeworks and so far I had at some points I had to :
>Make a dynamic array
>Make a dynamic matrix (2d array)
> Make a dynamic string

I managed to somehow do the array and matrix by modifying some string overflow snippets I found but the string thing is killing me. At one point I need a structure/function that would read what the user is inputting and create a dynamic string as he types characters in


Can someone elaborate
>>
>>60301403
are u retarded?
dynamic string is 2 fields in a struct, one for length and one for pointer to allocated memory.
and u need one function called write which checks if curr len + len of data u want to write > curr size then realloc.

fucking CIA nigger posting 9gag programming jokes
>>
>>60301433
look nigger, I asked a question, either give me an informative answer/point me to a good page or fuck off

I don't care how big your programming ego is, I need a good explanation on this shit
>>
>>60301507
>someone on the internet was mean to me
>they didn't answer me as nicely as I wanted

fuck off you whiny cunt
>>
>>60301403
bumpin
>>
File: g_web.png (674KB, 787x830px) Image search: [Google]
g_web.png
674KB, 787x830px
>>60301403
>Make a dynamic array

This will allocate some memory to your heap
// Not sure what type of array but here is one that
// will address each byte in memory

uint8_t *byte_array
size_t array_size = 1024;
byte_array = malloc(array_size);



> Make a dynamic string

Do the same thing except use char
>>
>>60301895
could you perhaps explain what each line means ? I know it sounds stupid, but I need to <<understand>> the thing , not just copy it

>what are uint8_t and size_t commands ?
> what does malloc take as arguments ?
>>
>>60301619
You should consider fornication as a method of reducing your frustration levels.
>>
>>60301936
/* 
* uint8_t is a c type that takes up 8 bits
* (uint16_t takes 16, uint32_t uses 32 bits)
* *byte_array means that I have a pointer named
* byte_array. Until I make the malloc call below it
* points to nothing (NULL)
*/
uint8_t *byte_array
/*
* size_t is another c type. You can use any kind of
* int here and it will still work (i.e. int array_size)
*/
size_t array_size = 1024;
/*
* This is the dynamic allocation part.
* To better understand just type 'man malloc' into
* your terminal. If windows kys.
*/
byte_array = malloc(array_size);
>>
>>60302070
Thanks

But let's say I want a dynamic string, would it be ok if I just created a linked list where each elemt would contain a char and point to another or is there a better way to make a dynamic string

I need the user to manually input the string and the size would be determined until let's say the user hits enter

So should I put in a for loop and just keep creating more linked elements pointing to nex until the user hits enter. Then just point the last one to null ? Is there a more elegant way to do this
>>
Don't worry. If you are new to c and or programming dynamic memory can seem odd.

The memory is anything and everything. A 2D array can be represented by a single block of memory.

>>60302070

Don't be like this guy though.

uint8_t *byte_array

That isn't NULL. That is uninitialized. It isn't NULL so if you check that it is null your code will think that you've assigned it to allocated memory already and you will crash.

uint8_t *byte_array = NULL;

It is like putting on a seatbelt. Do it all the time and you won't have to worry about it.
>>
File: Selection_001.png (700KB, 809x803px) Image search: [Google]
Selection_001.png
700KB, 809x803px
>>60302165
http://ideone.com/zAur7P
>>
File: 1406381379442.jpg (34KB, 640x480px) Image search: [Google]
1406381379442.jpg
34KB, 640x480px
>>60301403
>>>/wsr/
>using normie
sasuga
>>
>>60302165

In order to be safe/avoid buffer overflow, you can:

1) Pre-determine a max size of input you are willing to take and use fgets/scanf

2) Read the number of chars the user typed, then dynamically allocate that space with malloc (although there is still a 'max size' you will want to set for this)

And yeah initialize your ptrs to NULL. I omitted it earlier because doing so in global space causes it to be set to NULL by default on my machine.
Thread posts: 14
Thread images: 4


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.