dynamically allocate a char

This is a discussion on dynamically allocate a char within the C++ Programming forums, part of the General Programming Boards category; Suppose I have a data member for a struct which is a pointer to char Code: struct Sample { char* ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    dynamically allocate a char

    Suppose I have a data member for a struct which is a pointer to char


    Code:
    struct Sample
    {
            char* sPtr;
    // more code.........
    };
    
    
    int main()
    {
            // Now how do you dynamically allocate a string for char* ??
            // My try
            
            // Sample a;
            // a.sPtr = new char "Hello World!";
            // but this doesnt works whats the correct syntax ?
    
            return 0;
    }

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    You need to allocate memory - by specifying the number of chars you need / want.
    char* p = new char[n];
    If you need enough chars to store "Hello World!", then you can use sizeof:
    char* p = new char[sizeof("Hello World!")];
    After that, you actually need to copy the string into the storage with strcpy.
    But a better way in C++ is just to make a std::string and assign "Hello World!" directly.
    No messing with new/delete and it's simple.

    Don't forget that all you allocate with new must be freed with delete.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    ok thanks i know std::string


    but i doing book exercises thats why Im restricted to use char pointers only


    thanks any way

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    Alright, that's fine. Just so that you are aware.
    Good luck.
    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.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    struct Sample
    {
    	int one;
    	int two;
    	char* sPtr;
    };
    
    int main()
    {
    	Sample a;
    	Sample b;
    
    	a.one = 5;
    	a.two = 10;
    	a.sPtr = new char[sizeof("Hello World!")];
    	a.sPtr = "Hello World!";
    
    
    	b = a;
    
    	cout << a.one << "  " << a.two << "  " << a.sPtr;
    
    	cout << endl << b.one << "  " << b.two << "  " << b.sPtr <<  endl;
    
    	// Changing the content of a.sPtr
    	a.sPtr[0] = 'h'; // Error why???????
    
    
    	cout << a.one << "  " << a.two << "  " << a.sPtr;
    
    	cout << endl << b.one << "  " << b.two << "  " << b.sPtr <<  endl;
    
    	return EXIT_SUCCESS;
    }

    I have allocated the char successfully but now im getting errors when modifying it

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because after you allocate the memory, you change it to point to the constant memory area that holds "Hello, World!", which you can not write to. You need to make a copy of the string into your allocated memory.

    --
    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.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    http://cpwiki.sourceforge.net/Common...kes_and_errors
    I told you to use strcpy.
    You are assigning a string literals, which means:
    1) It's const char*, not char*, hence NOT supposed to be modifiable.
    2) All the memory you allocated before is lost.
    And for the sake of it:
    3) FREE your memory with delete before you exit!
    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.

  8. #8
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Quote Originally Posted by manzoor View Post
    Code:
    	a.sPtr = new char[sizeof("Hello World!")];
    	a.sPtr = "Hello World!";
    This is bad. You allocated enough memory on the first line and then you leaked it on the second line by pointing to a "string literal" instead. Look into strcpy() for copying the contents of the literal into the allocated.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    Thanks Elysia, matsp and FillYourBrain. You all are programming masters! How the hell do you guys know every thing?
    you're a genius thanks.
    no idea how many thanks im giving you right now... THANKS THANKS THANKS..
    thx for the help Elysia, matsp and FillYourBrain, appreciate it alot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  2. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 01:06 PM
  3. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  4. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 11:25 AM
  5. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM

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