Forumchem - Forum with AI(ALICE BOT & HAL9000) and TTS

More dificult for us, more easy for you
It is currently Fri Apr 19, 2024 7:11 pm

All times are UTC





Post new topic Reply to topic  Page 1 of 1
 [ 1 post ] 
Author Message
 Post subject: C - Why is my code working?
PostPosted: Tue Apr 20, 2021 3:00 pm 
Offline
User avatar

Joined: Thu Oct 15, 2009 9:25 pm
Posts: 2
Hello, Im pretty new to C.

Im allocating memory for a string of length 38 (+ null terminator) wit malloc. I then iterate over the blocks of memory Ive allocated to set the values of each char in the string.

However, no matter what number I give to malloc, I am still capable to set the value of each char in the string.

For example,

char* str = malloc(38 * sizeof(char)). // 37 character string, and null-terminator
vs
char* str2 = malloc(1); // Only enough memory for one char.

using malloc(1), Id expect that as I iterate from 1 to 38 over str2, Id get a seg-fault very quickly. But Im not, and I end up successfully initialize the 38-character string on only 1 byte of memory. Can anyone explain what is going on?

My code is in the pastebin below to perpetuate formatting:

https://pastebin.com/jNPTF7nE

Thanks for any help! Im compiling with "gcc -g -pedantic -o 1.5 1.5.c" Explanation of my code: the convert function replaces every occurrence of a space with the string "%20"



Top
 Profile      
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  Page 1 of 1
 [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: etouboudedu, vawceix and 8 guests


 
Search for:
 
Jump to:  

cron
Click me:
Powered by phpBB © 2000, 2002, 2005, 2007, 2008, 2009 phpBB Group
Chronicles phpBB3 theme by Jakob Persson. Stone textures by Patty Herford.
With special thanks to RuneVillage

This site have 4 type of tecnology in order to convert text to speech. By default you use the vozme tecnology. In order to know the other you need to sign for.


- Privacy Policy -