Initializing char arrays on the free store

This is a discussion on Initializing char arrays on the free store within the C++ Programming forums, part of the General Programming Boards category; OK. I have some code, but there has to be an easier way. I want to declare a char *-style ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    56

    Initializing char arrays on the free store

    OK. I have some code, but there has to be an easier way. I want to declare a char *-style string on the free store and initialize it at the same time. What I'd like to do, in other words, is something like this:
    Code:
    char * theString = "TEST STRING";
    But with dynamic memory, like:
    Code:
    char * theString = new char("TEST STRING");
    But that obviously does not work. Does anybody know what I'm talking about and can help me with it? Thanks!

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    char *test = new char[strlen(someotherchar) + 1];
    strcpy(test, someotherchar);
    Remember to include string.h
    Last edited by Eibro; 12-07-2002 at 09:27 AM.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Thanks for the response....
    Is there a similar solution for other types of arrays, e.g. int's?

  4. #4
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    Originally posted by roktsyntst
    Thanks for the response....
    Is there a similar solution for other types of arrays, e.g. int's?
    yeah, almost the same way

    for example

    int main(){

    int *array;

    int size;
    cout << "How Big Do You want your array?";
    cin >> size;

    array = new int[size];

    }

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Thanks, but I'm talking about initializing...

    i.e. on the stack you can say:
    Code:
    int array[5] = { 1, 5, 9, 12, 13 }; // Initialize it with a set of numbers right away
    But on the heap you have to say:
    Code:
    array = new int[5];
    Is there some place to put the initializer list or do you have to go one by one and copy it from some other array? Thanks again!

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You have to copy them in. You may be able to do more than one at a time, depending on where they are coming from.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    In C:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       char *msg = strdup("Hello world in C\n");
       if(msg) printf(msg);
       free(msg);
       return 0;
    }
    In C++:
    Code:
    #include <iostream>
    #include <string>
    
    int main(void)
    {
       string msg = "Hello world in C++\n";
       std::cout << msg;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Free Store of memory
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 11-12-2007, 01:27 PM
  4. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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