Thread: Memory Allocation Using String (Please Help)

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    15

    Memory Allocation Using String (Please Help)

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #define size 10
    void main()
    {
    	int i,s;
    	char **names;
    	printf("Enter Size Of Array..\n");
    	scanf("%d",&s);
    	fflush(stdin);
    	names=(char **)malloc(s*sizeof(char));
    	for(i=0;i<s;i++)
    	{
    		names[i]=(char *)malloc(size*sizeof(char));
    	}
    	printf("\nEnter Data..\n");
    	for(i=0;i<s;i++)
    	{
    		scanf("%s",&names[i][0]);
    	}
    	printf("\nDisplaying Data..\n");
    	for(i=0;i<s;i++)
    	{
    		printf("%s",names[i][0]);
    	}
    	free(names);
    	getch();
    }
    I Don't Know What Seems To Be The Problem, What I'm trying to do is to read a set of names using memory allocation and print them and this doesn't work!

    Help Would Be Really Appreciated.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    #include<stdio.h>
    #include<conio.h> //delete this
    #include<stdlib.h>
    #define size 10
    void main() //see faq void main
    {
        int i,s;
        char **names;
        printf("Enter Size Of Array..\n");
        scanf("%d",&s);
        fflush(stdin); //see faq fflushing stdin
        names=(char **)malloc(s*sizeof(char)); //see faq casting malloc
        for(i=0;i<s;i++)
        {
            names[i]=(char *)malloc(size*sizeof(char)); //you don't free these
        }
        printf("\nEnter Data..\n");
        for(i=0;i<s;i++)
        {
            scanf("%s",&names[i][0]);
        }
        printf("\nDisplaying Data..\n");
        for(i=0;i<s;i++)
        {
            printf("%s",names[i][0]); //this is a character, not a string. you want names[i]
        }
        free(names);
        getch(); //use getchar. it's standard, getch is not.
    }

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #define size 10
    void main()
    {
    	int i,s;
    	char **names;
    	printf("Enter Size Of Array..\n");
    	scanf("%d",&s);
    	fflush(stdin);
    	names=(char **)malloc(s*sizeof(char));
    	for(i=0;i<s;i++)
    	{
    		names[i]=(char *)malloc(size*sizeof(char));
    	}
    	printf("\nEnter Data..\n");
    	for(i=0;i<s;i++)
    	{
    		scanf("%s",names[i]);
    	}
    	printf("\nDisplaying Data..\n");
    	for(i=0;i<s;i++)
    	{
    		printf("%s",names[i]);
    	}
    	free(names);
    	getch();
    }
    There is nothing wrong with pointer casting. Your problem is about printf, scanf string implementation. Examine and train it.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > There is nothing wrong with pointer casting.
    Only that it hides mistakes which the compiler might otherwise be able to tell you about.
    For example, hiding the fact that you didn't prototype malloc properly because you didn't include stdlib.h (for example), which is also mentioned in the FAQ.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Another problem:
    Code:
    names=(char **)malloc(s*sizeof(char));
    You are allocating the size of chars (1 byte), not a pointer to char (typically 4 bytes).

    And to top it off, you are only freeing the pointer names, and not the pointers in the array names.
    Plus you are reading your strings in an extremely unsafe way. You shouldn't. You have some reading to do:
    http://cpwiki.sourceforge.net/buffer_overrun
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Salem View Post
    > There is nothing wrong with pointer casting.
    Only that it hides mistakes which the compiler might otherwise be able to tell you about.
    For example, hiding the fact that you didn't prototype malloc properly because you didn't include stdlib.h (for example), which is also mentioned in the FAQ.
    And on certain compilers/processor architectures, this could have extreme consequences. In Motorola 68K processor family, registers are either "data" or "address" registers. Pointers are returned in address registers, integer return values are in data registers. An undefined function will return an integer, so if you don't include stdlib.h, the compiler will assume that you get an integer (that is later cast to an pointer) - so it will use the wrong register, and random bad things will happen [it is quite possible that the malloc function itself is temporarily doing math on the allocated memory, thus keeping something akin to the allocated location in register D0 which is used by the compiler to return an integer value, so it may not even crash on the initial instance!].

    Similarly, in an x86-64 architecture, the return value type of integer is 32-bit. A pointer is 64-bit, and the consequence would be "lost the upper 32-bits".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Allocation In A String Class
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 09-18-2007, 10:34 AM
  2. Relate memory allocation in struct->variable
    By Niara in forum C Programming
    Replies: 4
    Last Post: 03-23-2007, 03:06 PM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM