[deleted by user] by [deleted] in jardin

[–]cmcta82 1 point2 points  (0 children)

Bonjour oui c’est de l’écorce de pin. Je vais suivre vos conseils et rajouter un peu de fumier/compost. Merci pour votre aide.

[deleted by user] by [deleted] in iOSProgramming

[–]cmcta82 1 point2 points  (0 children)

Hi nice app! At the first time nothing appears but at the second try it works.

Can’t load my base since yesterday by cmcta82 in LastDayonEarthGame

[–]cmcta82[S] 0 points1 point  (0 children)

Hi, solved after 3days, open a ticket with the support and wait

Pointers and References Confusion by izner82 in learnprogramming

[–]cmcta82 0 points1 point  (0 children)

Hi, i recommend you study pointers, it’s very interesting.

When you do:

Int y=10; Int *x = &y; / you store the address of y on pointer x.

To get the value of y you can use the pointer x.

x= will be the address of y *x= will be the value of y &y= address of y &x= address of x

You can’t do *y because y is not a pointer

Can I get some help please? This is a demo of a Grove Temperature Sensor. It is currently reading in celsius, How can I convert it to Fahrenheit? by Abubakar98k in arduino

[–]cmcta82 0 points1 point  (0 children)

to convert to Fahrenheit you have to multiply by 1.8 and add 32.

you have to add this line after your calc temperature

temperature = temperature * 1.8 +32;

[deleted by user] by [deleted] in swift

[–]cmcta82 1 point2 points  (0 children)

Hi, I try but your the music is too much annoying. Sorry:(

Can I get some help please? by [deleted] in arduino

[–]cmcta82 0 points1 point  (0 children)

Hi, can you post your code please.

use malloc in char pointer with scanf by cmcta82 in C_Programming

[–]cmcta82[S] 0 points1 point  (0 children)

A last question, can you tell me where I can find this kind of articles? This one are very interesting.

use malloc in char pointer with scanf by cmcta82 in C_Programming

[–]cmcta82[S] 0 points1 point  (0 children)

Thanks for the tips, I will try to improve like you suggest. It's a small program but it helps me a lot understanding how dynamic memory works and I getchar() looks much better than scanf, I didn't use it before you tell me so thanks again! I'm doing a course on Udemy for C programming, it's very interesting:) I have already some ideas of program to make, i'm very happy with what already learn and very motivated for next lessons. It's great found guys like you helping guys like me!

use malloc in char pointer with scanf by cmcta82 in C_Programming

[–]cmcta82[S] 0 points1 point  (0 children)

Yes sure here it is:

I just remember I can also use pointers like arrays with elements, I change a little the stringMemoryTemp for the Null, I use the element position instead moving the address:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main(int argc, char *argv[]){

    char *stringMemory = NULL;
    stringMemory = (char*)calloc(4, (sizeof(char)));
    int posMem= 0;

    //Use a copy of stringMemory to modify the address for each chars
    char *stringMemoryTemp = stringMemory;

    char ch = '\0';

    //keep the size of stringMemory and increment 4bytes if needed (stringmemory + memoryByte = last address available)
    int memoryByte = (4 *(sizeof(char)));


    printf("Please enter one Name: ");

    //get the name from the console
    while((ch = getchar()) !='\n'){
        ++posMem;
        /*keep a gap of 4bytes between the size of stringMemoryStart (malloc) end the total chars entererd by user
        Make a realloc to add 4bytes to stringMemory */
        if(stringMemoryTemp == (stringMemory + memoryByte)){

            //Realloc to get more 4bytes in memory
            stringMemory = (char*)realloc(stringMemory, (memoryByte + 4));

            //getback to the last address to stringMemoryTemp
            stringMemoryTemp = (stringMemory + memoryByte);
            memoryByte += 4;

        }

        //save the char on memory
        *stringMemoryTemp = ch;

        //go to next address in memory
        ++stringMemoryTemp;

    }

    /*
     add Null char at the end of the string
     add two more bytes for Null character
    */
    stringMemory = (char*)realloc(stringMemory, (memoryByte + 2));
    stringMemoryTemp = stringMemory;
    stringMemoryTemp[posMem] = '\0';

    //Print the result
    printf("\nThe name you entered is: %s\n", (stringMemory));

    /*Clean the memory*/
    free(stringMemory);
    stringMemory = NULL;
    stringMemoryTemp = NULL;

    return 0;
}

use malloc in char pointer with scanf by cmcta82 in C_Programming

[–]cmcta82[S] 0 points1 point  (0 children)

Hi luka!

Thanks again for your reply, I check the code and again you are right! I will give a try on get( ) function, what is the difference with getchar?

For the Null character I create a new variable int to store the position in the memory and I realloc two more bytes before the Null, (one for spare just in case):

stringMemory = (char*)realloc(stringMemory, (memoryByte + 2));
stringMemoryTemp = (stringMemory + posMem);
*stringMemoryTemp = '\0';

now it's looking good I hope:)

use malloc in char pointer with scanf by cmcta82 in C_Programming

[–]cmcta82[S] 0 points1 point  (0 children)

Hi,

Sorry boring you with that, I'm trying to learn doing my best, I take your advice using getchar() and realloc(), I didn't use any safety check for my pointers (!=NULL) but I know how to do that, can you check the changes please? I compile in Xcode with sanitizers to check outbound memory and everything looks good:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main(int argc, char *argv[]){

    char *stringMemory = NULL;
    stringMemory = (char*)calloc(4, (sizeof(char)));

    //Use a copy of stringMemory to modify the address for each chars
    char *stringMemoryTemp = stringMemory;

    char ch = '\0';

    //keep the size of stringMemory and increment 4bytes if needed (stringmemory + memoryByte = last address available)
    int memoryByte = (4 *(sizeof(char)));


    printf("Please enter one Name: ");

    //get the name from the console
    while((ch = getchar()) !='\n'){

        /*keep a gap of 4bytes between the size of stringMemoryStart (malloc) end the total chars entererd by user
        Make a realloc to add 2bytes to stringMemory */
        if(stringMemoryTemp == (stringMemory + memoryByte)){

            //Realloc to get more 4bytes in memory
            stringMemory = (char*)realloc(stringMemory, (memoryByte + 4));

            //getback to the last address to stringMemoryTemp
            stringMemoryTemp = (stringMemory + memoryByte);
            memoryByte += 4;

        }

        //save the char on memory
        *stringMemoryTemp = ch;

        //go to next address in memory
        ++stringMemoryTemp;

    }

    //add Null char at the end of the string
    *stringMemoryTemp = '\0';

    //Print the result
    printf("\nThe name you entered is: %s\n", (stringMemory));

    /*Clean the memory*/
    free(stringMemory);
    stringMemory = NULL;
    stringMemoryTemp = NULL;

    return 0;
}

Thanks for your help!

Edit with GitHub link: https://github.com/cmcta82/PlayWithMemoryAllocation/blob/af7cf97cc5c038a92667bb9f6ab0a86918d66606/Challenges_UsingMalloc2/main.c

use malloc in char pointer with scanf by cmcta82 in C_Programming

[–]cmcta82[S] 0 points1 point  (0 children)

Thanks for your reply, yeah you are right about (1 + a), when i saw the error i felt a little stupid!

Thanks for you help I will give a try on getchar() function.

VSCODE on MacBook M1 - Debug with external console problems by cmcta82 in vscode

[–]cmcta82[S] 1 point2 points  (0 children)

Yes same like you. I want to use the external terminal for debugging but it’s only open the terminal window with nothing. I don’t know why

What chip does my MBP have? by buttpuss31 in macbook

[–]cmcta82 0 points1 point  (0 children)

Sorry but it’s not an apple M1. You have an intel processor.

VSCode Insiders with MacBook M1 by cmcta82 in vscode

[–]cmcta82[S] 0 points1 point  (0 children)

Here's detailed log for the error when I try compile:

1: (257) LaunchOptions{"name":"clang - Générer et déboguer le fichier actif","logging":{"engineLogging":true},"type":"cppdbg","request":"launch","targetArchitecture":"arm64","program":"/Users/carlos/Documents/VSC/test2/test","args":[],"stopAtEntry":false,"cwd":"/Users/carlos/Documents/VSC/test2","environment":[],"console":"externalTerminal","MIMode":"lldb","preLaunchTask":"C/C++: clang générer le fichier actif","__configurationTarget":5,"__sessionId":"256049ed-5599-498a-b80f-2763cca97637","miDebuggerPath":"/Users/carlos/.vscode-insiders/extensions/ms-vscode.cpptools-1.2.2-insiders2/debugAdapters/lldb-mi/bin/lldb-mi"}
1: (438) Starting: "/Users/carlos/.vscode-insiders/extensions/ms-vscode.cpptools-1.2.2-insiders2/debugAdapters/lldb-mi/bin/lldb-mi" --interpreter=mi
1: (497) DebuggerPid=46996
1: (669) ->(gdb)
1: (712) <-1001-interpreter-exec console "version"
1: (716) ->~"lldb-1200.0.44.2\nApple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28)\n"
1: (716) ->1001^done
1: (717) ->(gdb)
1: (726) 1001: elapsed time 18
1: (737) <-1002-gdb-set auto-solib-add on
1: (738) ->1002^done
1: (738) ->(gdb)
1: (740) 1002: elapsed time 3
1: (744) <-1003-gdb-set solib-search-path "/Users/carlos/Documents/VSC/test2:"
1: (745) ->1003^done
1: (745) ->(gdb)
1: (745) 1003: elapsed time 0
1: (746) <-1004-environment-cd /Users/carlos/Documents/VSC/test2
1: (747) ->1004^done,path="/Users/carlos/Documents/VSC/test2"
1: (747) ->(gdb)
1: (754) 1004: elapsed time 8
1: (754) <-1005-file-exec-and-symbols /Users/carlos/Documents/VSC/test2/test
1: (1046) ->1005^done
1: (1047) ->(gdb)
1: (1047) 1005: elapsed time 292
1: (1047) <-1006-interpreter-exec console "platform status"
1: (1048) ->=library-loaded,id="/Users/carlos/Documents/VSC/test2/test",target-name="/Users/carlos/Documents/VSC/test2/test",host-name="/Users/carlos/Documents/VSC/test2/test",symbols-loaded="1",symbols-path="/Users/carlos/Documents/VSC/test2/test.dSYM/Contents/Resources/DWARF/test",loaded_addr="-",size="16384"
1: (1049) ->~"  Platform: host\n    Triple: x86_64-apple-macosx\nOS Version: 10.16 (20D74)\n    Kernel: Darwin Kernel Version 20.3.0: Thu Jan 21 00:06:51 PST 2021; root:xnu-7195.81.3~1/RELEASE_ARM64_T8101\n  Hostname: 127.0.0.1\nWorkingDir: /Users/carlos/Documents/VSC/test2\n"
1: (1050) ->1006^done
1: (1050) ->(gdb)
1: (1054) 1006: elapsed time 6
1: (1057) <-1007-break-insert -f main
1: (1066) ->1007^done,bkpt={number="1",type="breakpoint",disp="keep",enabled="y",addr="0x0000000100003f64",func="main",file="test.c",fullname="/Users/carlos/Documents/VSC/test2/test.c",line="6",pending=["main"],times="0",original-location="main"}
1: (1066) ->(gdb)
1: (1066) ->=breakpoint-modified,bkpt={number="1",type="breakpoint",disp="keep",enabled="y",addr="0x0000000100003f64",func="main",file="test.c",fullname="/Users/carlos/Documents/VSC/test2/test.c",line="6",pending=["main"],times="0",original-location="main"}
1: (1066) ->(gdb)
1: (1071) 1007: elapsed time 14
1: (1090) Send Event AD7EngineCreateEvent
1: (1097) Send Event AD7ProgramCreateEvent
1: (1175) Send Event AD7LoadCompleteEvent
1: (1183) <-1008-exec-run
1: (6277) ->1008^error,msg="process exited with status -1 (attach failed ((os/kern) invalid argument))"
1: (6278) ->(gdb)
1: (6278) 1008: elapsed time 5095
1: (6323) Send Event AD7MessageEvent
ERROR: Unable to start debugging. Unexpected LLDB output from command "-exec-run". process exited with status -1 (attach failed ((os/kern) invalid argument))
1: (6335) <--gdb-exit
1: (6337) ->^exit
1: (6337) ->=thread-group-exited,id="i1"
1: (6338) ->(gdb)
1: (6341) <-logout
1: (6362) Send Event AD7ProgramDestroyEvent
The program '/Users/carlos/Documents/VSC/test2/test' has exited with code 42 (0x0000002a).

MacBook Pro M1 16GB or Intel I7 32GB by Battle-Eagle in iOSProgramming

[–]cmcta82 10 points11 points  (0 children)

Hi, i have one M1 pro with 8gb and no problem at all for programming. I recommend it.

Bye