none
malloc no C++ RRS feed

  • Pergunta

  • estou tentando fazer uma fila dinamica e preciso usar o malloc.. eis o meu codigo...

    ps: os codigos dos botoes estao em fila estatica... eu tenho pronto ja o dinamico mas tava dando erro no malloc...

     

    #pragma once
    #define max 100
    
    namespace ProjetoProcessosBeta1 {
    
    	struct fila
    	{
    		int info;
    		int ini,fim;
    		int Fila[max];
    	};
    	fila f;

     /*struct fila { struct no *ini; struct no *fim; }; no *novo;*/ //novo = (struct no) malloc(sizeof(no)); using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; /// <summary> /// Summary for Form1 /// </summary> public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: Add the constructor code here // } protected: /// <summary> /// Clean up any resources being used. /// </summary> ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::Button^ button1; protected: private: System::Windows::Forms::ColorDialog^ colorDialog1; private: System::Windows::Forms::ImageList^ imageList1; private: System::Windows::Forms::PictureBox^ pb01; private: System::Windows::Forms::PictureBox^ pb02; private: System::Windows::Forms::PictureBox^ pb03; private: System::Windows::Forms::PictureBox^ pb04; private: System::Windows::Forms::PictureBox^ pb05; private: System::Windows::Forms::PictureBox^ pb06; private: System::Windows::Forms::PictureBox^ pb07; private: System::Diagnostics::Process^ process1; private: System::Windows::Forms::Timer^ timer; private: System::ComponentModel::BackgroundWorker^ backgroundWorker; private: System::Windows::Forms::Button^ button2; private: System::ComponentModel::IContainer^ components; private: /// <summary> /// Required designer variable. /// </summary> #pragma region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> void InitializeComponent(void) { this->components = (gcnew System::ComponentModel::Container()); this->button1 = (gcnew System::Windows::Forms::Button()); this->colorDialog1 = (gcnew System::Windows::Forms::ColorDialog()); this->imageList1 = (gcnew System::Windows::Forms::ImageList(this->components)); this->pb01 = (gcnew System::Windows::Forms::PictureBox()); this->pb02 = (gcnew System::Windows::Forms::PictureBox()); this->pb03 = (gcnew System::Windows::Forms::PictureBox()); this->pb04 = (gcnew System::Windows::Forms::PictureBox()); this->pb05 = (gcnew System::Windows::Forms::PictureBox()); this->pb06 = (gcnew System::Windows::Forms::PictureBox()); this->pb07 = (gcnew System::Windows::Forms::PictureBox()); this->process1 = (gcnew System::Diagnostics::Process()); this->timer = (gcnew System::Windows::Forms::Timer(this->components)); this->backgroundWorker = (gcnew System::ComponentModel::BackgroundWorker()); this->button2 = (gcnew System::Windows::Forms::Button()); (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pb01))->BeginInit(); (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pb02))->BeginInit(); (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pb03))->BeginInit(); (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pb04))->BeginInit(); (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pb05))->BeginInit(); (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pb06))->BeginInit(); (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pb07))->BeginInit(); this->SuspendLayout(); // // button1 // this->button1->Location = System::Drawing::Point(12, 58); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(75, 23); this->button1->TabIndex = 0; this->button1->Text = L"Iniciar"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click); // // imageList1 // this->imageList1->ColorDepth = System::Windows::Forms::ColorDepth::Depth8Bit; this->imageList1->ImageSize = System::Drawing::Size(16, 16); this->imageList1->TransparentColor = System::Drawing::Color::Transparent; // // pb01 // this->pb01->BackColor = System::Drawing::SystemColors::ActiveCaptionText; this->pb01->Location = System::Drawing::Point(117, 25); this->pb01->Name = L"pb01"; this->pb01->Size = System::Drawing::Size(43, 23); this->pb01->TabIndex = 1; this->pb01->TabStop = false; // // pb02 // this->pb02->BackColor = System::Drawing::SystemColors::ActiveCaptionText; this->pb02->Location = System::Drawing::Point(166, 25); this->pb02->Name = L"pb02"; this->pb02->Size = System::Drawing::Size(43, 23); this->pb02->TabIndex = 2; this->pb02->TabStop = false; // // pb03 // this->pb03->BackColor = System::Drawing::SystemColors::ActiveCaptionText; this->pb03->Location = System::Drawing::Point(215, 25); this->pb03->Name = L"pb03"; this->pb03->Size = System::Drawing::Size(43, 23); this->pb03->TabIndex = 3; this->pb03->TabStop = false; // // pb04 // this->pb04->BackColor = System::Drawing::SystemColors::ActiveCaptionText; this->pb04->Location = System::Drawing::Point(264, 25); this->pb04->Name = L"pb04"; this->pb04->Size = System::Drawing::Size(43, 23); this->pb04->TabIndex = 4; this->pb04->TabStop = false; // // pb05 // this->pb05->BackColor = System::Drawing::SystemColors::ActiveCaptionText; this->pb05->Location = System::Drawing::Point(313, 25); this->pb05->Name = L"pb05"; this->pb05->Size = System::Drawing::Size(43, 23); this->pb05->TabIndex = 5; this->pb05->TabStop = false; // // pb06 // this->pb06->BackColor = System::Drawing::SystemColors::ActiveCaptionText; this->pb06->Location = System::Drawing::Point(362, 25); this->pb06->Name = L"pb06"; this->pb06->Size = System::Drawing::Size(43, 23); this->pb06->TabIndex = 6; this->pb06->TabStop = false; // // pb07 // this->pb07->BackColor = System::Drawing::SystemColors::ActiveCaptionText; this->pb07->Location = System::Drawing::Point(411, 25); this->pb07->Name = L"pb07"; this->pb07->Size = System::Drawing::Size(43, 23); this->pb07->TabIndex = 7; this->pb07->TabStop = false; // // process1 // this->process1->StartInfo->Domain = L""; this->process1->StartInfo->LoadUserProfile = false; this->process1->StartInfo->Password = nullptr; this->process1->StartInfo->StandardErrorEncoding = nullptr; this->process1->StartInfo->StandardOutputEncoding = nullptr; this->process1->StartInfo->UserName = L""; this->process1->SynchronizingObject = this; // // button2 // this->button2->Location = System::Drawing::Point(12, 25); this->button2->Name = L"button2"; this->button2->Size = System::Drawing::Size(75, 23); this->button2->TabIndex = 8; this->button2->Text = L"Inicialização"; this->button2->UseVisualStyleBackColor = true; this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click); // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(485, 108); this->Controls->Add(this->button2); this->Controls->Add(this->pb07); this->Controls->Add(this->pb06); this->Controls->Add(this->pb05); this->Controls->Add(this->pb04); this->Controls->Add(this->pb03); this->Controls->Add(this->pb02); this->Controls->Add(this->pb01); this->Controls->Add(this->button1); this->MaximizeBox = false; this->Name = L"Form1"; this->Text = L"ProcessoBeta01"; (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pb01))->EndInit(); (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pb02))->EndInit(); (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pb03))->EndInit(); (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pb04))->EndInit(); (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pb05))->EndInit(); (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pb06))->EndInit(); (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pb07))->EndInit(); this->ResumeLayout(false); } #pragma endregion private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { f.info = 1; MessageBox::Show("oi","haha",MessageBoxButtons::OK,MessageBoxIcon::Exclamation); f.Fila[f.fim++] = f.info; //novo->prox = 0; pb01->BackColor = Color::Red; } private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { f.ini = 0; f.fim = 0; } }; }

     

    estava usando esta struct tbm que acabei tirando ela por estar usando estatica

     

    struct no
    	{
    		int info
    		struct no *prox
    	}

    ele nao estava aceitando o malloc.. nao sei msmo.. 

    obrigado

     

    terça-feira, 4 de maio de 2010 05:31

Todas as Respostas

  • Hawk,

    é melhor direcionar sua dúvida para o fórum de .NET/C#.

    Abraços, cox


    cox
    terça-feira, 4 de maio de 2010 15:00
  • mas estou trabalhando com C, nao entendi

     

    obrigado

    terça-feira, 4 de maio de 2010 15:26
  • Na verdade o código exibido está em C++/CLI.

    Você está usando o Visual C++ para escrever este código com Windows Forms. Sugiro mudar sua struct para uma ref struct e usar gcnew para alocar este objeto na Heap da CLR. Vc não precisará chamar delete.

    Se quiser alocar memória nativa dentro de uma classe .NET (ref class ou ref struct do C++/CLI) é possivel. Você fará isto via ponteiro nativo, e vai chamar malloc e free da mesma forma que faz em C ou C++.

     


    Fabio Galuppo
    sexta-feira, 21 de maio de 2010 14:50