allocation dynamic

This is a discussion on allocation dynamic within the C Programming forums, part of the General Programming Boards category; hi. my ask is this: when i alloc memory in my code with function malloc of library <stdlib.h> and i ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    18

    allocation dynamic

    hi.
    my ask is this:
    when i alloc memory in my code with function malloc of library <stdlib.h>
    and i going use him...this not work...look my code testing malloc:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main(){
            int *p,
            num;
            char *entrada;
            p = (int *)malloc(sizeof(int));
            printf("\nquantos caracters deseja entrar? ");
            scanf("%d",&num);
            entrada = (char *)malloc(sizeof(char)*num);
            if (p == NULL)
            {
                    printf("ERRO: Sem memoria!\n");
                    return 1;
            }
            printf("Insira um valor:");
            scanf("%d",p);
            printf("Digite uma string:");
            fgets(entrada,num,stdin);
            printf("Voce Entrou com o valor %d\n",*p);
            printf("Voce digitou a string %s\n",entrada);
            free(p);
            free(entrada);
            return(0);
    }
    if anyone know, please answerme.

    One kiss in your asses.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    sorry here go the code correct that generate error.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main(){
            int *p,
            num;
            char *entrada;
            p = (int *)malloc(sizeof(int));
            printf("\nquantos caracters deseja entrar? ");
            scanf("%d",&num);
            entrada = (char *)malloc(sizeof(char)*num);
            if (p == NULL)
            {
                    printf("ERRO: Sem memoria!\n");
                    return 1;
            }
            printf("Insira um valor:");
            scanf("%d",p);
            printf("Digite uma string:");
            fgets(entrada,num,stdin);
            printf("Voce Entrou com o valor %d\n",*p);
            printf("Voce digitou a string %s\n",*entrada);
            free(p);
            free(entrada);
            return(0);
    }
    Last edited by k4z1nh0; 03-06-2005 at 08:20 AM. Reason: error in tag

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Wrong:
    Code:
            printf("Voce digitou a string %s\n",*entrada);
    Here *entrada is a single character, but %s expects a pointer to a string.
    Code:
            printf("Voce digitou a string %s\n",entrada);
    You may want to visit the FAQ.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    There is no need to cast the return of malloc.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    1. Unlike p, you do not check if the allocation of memory for entrada was a success.

    2. You ask the user how many characters they wish to enter, you allocate this num bytes of memory for entrada, then you use fgets which only reads num - 1 characters (it has to save the last one for the NULL terminator). Is this a problem?

    3.
    Quote Originally Posted by k4z1nh0
    One kiss in your asses.
    Uhhh... same to you buddy?!?!
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    thks, but the problem persists and problem not is in NULL TERMINATOR, the point is:
    when the program askme "Digite uma String: ", the correct will be if he showme a cursor posicioned right of this string waiting what i want to do.
    ------------------
    When he showme "Digite uma String: ", the next instruction will be fgets(entrada,sizeof(char)*80-1,stdin); ok??
    when he showme the string above he jump to next function that is printf("Voce Entrou com o valor %d\n",*p);
    he jump the function that i waited.WHY?? if there are one tutorial of Allocation Dynamid please post here pls.

    Any help me?

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by k4z1nh0
    he jump the function that i waited.WHY??
    Because scanf("%d",...) doesn't do anything with the newline in the input buffer, so it is still there and immediately satisifies the subsequent call to fgets.

    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic allocation from 1 instead of zero
    By cfdprogrammer in forum C Programming
    Replies: 27
    Last Post: 04-28-2009, 08:21 AM
  2. pointer to array with dynamic allocation
    By cfdprogrammer in forum C Programming
    Replies: 22
    Last Post: 04-07-2009, 09:56 AM
  3. Difference between straight and dynamic allocation?
    By darsunt in forum C++ Programming
    Replies: 10
    Last Post: 06-04-2008, 05:47 PM
  4. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  5. Dynamic allocation (I thought it would crash)
    By Baaaah! in forum C Programming
    Replies: 16
    Last Post: 11-30-2005, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21